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, use_lookup = false, pipeline = []) ⇒ Base

Instantiate the eager load class.

Examples:

Create the new belongs to eager load preloader.

BelongsTo.new(association, parent_docs)

Parameters:

  • associations (Array<Mongoid::Association::Relatable>)

    Associations to eager load

  • docs (Array<Document>)

    Documents to preload the associations

  • use_lookup (Boolean) (defaults to: false)

    Whether to use $lookup aggregation for eager loading. This is used in Criteria#eager_load.

  • pipeline (Array<Hash>) (defaults to: [])

    The aggregation pipeline to use when using $lookup for eager loading.

[ GitHub ]

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

[ GitHub ]

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

[ GitHub ]

  
# 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

#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 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_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 180

def shift_association
  @association = @associations.shift
end