Class: Mongoid::Association::Eager
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | lib/mongoid/association/eager.rb |
Overview
Base class for eager load preload functions.
Class Method Summary
-
.new(associations, docs) ⇒ Base
constructor
Instantiate the eager load class.
Instance Method Summary
-
#run ⇒ Array
Run the preloader.
-
#each_loaded_document_of_class(cls, keys)
private
Retrieves the documents of the specified class, that have the foreign key included in the specified list of keys.
-
#shift_association ⇒ Mongoid::Association::Relatable
private
Shift the current association metadata.
Constructor Details
.new(associations, docs) ⇒ Base
Instantiate the eager load class.
# File 'lib/mongoid/association/eager.rb', line 19
def initialize(associations, docs) @associations = associations @docs = docs @grouped_docs = {} end
Instance Method Details
#each_loaded_document_of_class(cls, keys) (private)
Retrieves the documents of the specified class, that have the foreign key included in the specified list of keys.
When the documents are retrieved, the set of inclusions applied is the set of inclusions applied to the host document minus the association that is being eagerly loaded.
# File 'lib/mongoid/association/eager.rb', line 67
private def each_loaded_document_of_class(cls, keys) # Note: keys should not include nil elements. # Upstream code is responsible for eliminating nils from keys. return cls.none if keys.empty? criteria = cls.criteria criteria = criteria.apply_scope(@association.scope) criteria = criteria.any_in(key => keys) criteria.inclusions = criteria.inclusions - [@association] criteria.each do |doc| yield doc end end
#run ⇒ Array
Run the preloader.
# File 'lib/mongoid/association/eager.rb', line 31
def run @loaded = [] while shift_association preload @loaded << @docs.collect { |d| d.send(@association.name) if d.respond_to?(@association.name) } end @loaded.flatten end
#shift_association ⇒ Mongoid::Association::Relatable (private)
Shift the current association metadata
# File 'lib/mongoid/association/eager.rb', line 155
def shift_association @association = @associations.shift end