123456789_123456789_123456789_123456789_123456789_

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

Instance Method Summary

Constructor Details

.new(associations, docs) ⇒ Base

Instantiate the eager load class.

Examples:

Create the new belongs to eager load preloader.

BelongsTo.new(association, parent_docs)

Parameters:

[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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

#runArray

Run the preloader.

Examples:

Preload the associations into the documents.

loader.run

Returns:

  • (Array)

    The list of documents given.

[ GitHub ]

  
# 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_associationMongoid::Association::Relatable (private)

Shift the current association metadata

Examples:

Shift the current association.

loader.shift_association

Returns:

[ GitHub ]

  
# File 'lib/mongoid/association/eager.rb', line 155

def shift_association
  @association = @associations.shift
end