123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::EagerLoad::PolymorphicTargets Private

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/mongoid/association/eager_load/polymorphic_targets.rb

Overview

The targets of a polymorphic belongs_to, indexed as { type => { primary_key => document } }. Each subclass reaches the types that live in one place (the root's database or elsewhere); .for resolves the whole set, routing each type to the subclass that can reach it.

Class Method Summary

Instance Method Summary

Constructor Details

.new(association, keys_by_type) ⇒ PolymorphicTargets

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 43

def initialize(association, keys_by_type)
  @association = association
  @keys_by_type = keys_by_type
end

Class Method Details

.for(association, keys_by_type, root_class) ⇒ Hash

Resolve every polymorphic target for the foreign keys grouped by type. The types whose documents share the root's database are fetched together in one $facet; those living elsewhere are read through their own models.

Parameters:

  • association (Mongoid::Association::Relatable)

    The polymorphic inclusion.

  • keys_by_type (Hash)

    The foreign keys grouped by type.

  • root_class (Class)

    The class being queried.

Returns:

  • (Hash)

    The targets, as { type => { primary_key => document } }.

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 23

def for(association, keys_by_type, root_class)
  here, elsewhere = keys_by_type.partition do |type, _keys|
    in_root_database?(association, type, root_class)
  end
  same_database = SameDatabaseTargets.new(association, here.to_h, root_class)
  other_databases = OtherDatabaseTargets.new(association, elsewhere.to_h)
  same_database.fetch.merge(other_databases.fetch)
end

.in_root_database?(association, type, root_class) ⇒ Boolean (private)

Whether the type's model shares the root's database (and client): exactly what a $lookup from the root collection can reach.

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 36

def in_root_database?(association, type, root_class)
  model = association.resolver.model_for(type)
  model.client_name == root_class.client_name &&
    model.database_name == root_class.database_name
end

Instance Method Details

#fetchHash

Returns:

  • (Hash)

    The targets, as { type => { primary_key => document } }.

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 49

def fetch
  raise NotImplementedError
end

#indexed(documents, model) (private)

The raw documents instantiated and indexed by their primary key.

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 64

def indexed(documents, model)
  documents.map { |document| Factory.from_db(model, document) }
           .index_by { |document| document.send(primary_key) }
end

#model_for(type) (private)

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 59

def model_for(type)
  @association.resolver.model_for(type)
end

#primary_key (private)

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 55

def primary_key
  @association.primary_key
end