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, use_lookup = false, pipeline = []) ⇒ 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.
-
#prepare_criteria_for_loaded_documents(cls, keys)
private
Prepares the criteria to retrieve 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, use_lookup = false, pipeline = []) ⇒ Base
Instantiate the eager load class.
# File 'lib/mongoid/association/eager.rb', line 23
def initialize(associations, docs, use_lookup = false, pipeline = []) @associations = associations @docs = docs @grouped_docs = {} @use_lookup = use_lookup @pipeline = pipeline 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 95
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 = prepare_criteria_for_loaded_documents(cls, keys) criteria.each do |doc| yield doc end end
#prepare_criteria_for_loaded_documents(cls, keys) (private)
Prepares the criteria to retrieve 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 189
def prepare_criteria_for_loaded_documents(cls, keys) criteria = cls.criteria criteria = criteria.apply_scope(@association.scope) criteria = criteria.any_in(key => keys) criteria.inclusions = criteria.inclusions - [@association] criteria end
#run ⇒ Array
Run the preloader.
# File 'lib/mongoid/association/eager.rb', line 37
def run @loaded = [] if @use_lookup preload_with_lookup @loaded = @docs return @loaded.flatten end 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 180
def shift_association @association = @associations.shift end