#primary_key (private)
[ GitHub ]# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 55
def primary_key @association.primary_key end
123456789_123456789_123456789_123456789_123456789_
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Inherits: | Object |
| Defined in: | lib/mongoid/association/eager_load/polymorphic_targets.rb |
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.
Resolve every polymorphic target for the foreign keys grouped by type.
Whether the type's model shares the root's database (and client): exactly what a $lookup from the root collection can reach.
The raw documents instantiated and indexed by their primary key.
PolymorphicTargets
# 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
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.
# 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
Whether the type's model shares the root's database (and client): exactly what a $lookup from the root collection can reach.
# 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
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 49
def fetch raise NotImplementedError end
The raw documents instantiated and indexed by their primary key.
# 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
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 59
def model_for(type) @association.resolver.model_for(type) end
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 55
def primary_key @association.primary_key end