123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Referenced::BelongsTo::Eager

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Mongoid::Association::Eager
Defined in: lib/mongoid/association/referenced/belongs_to/eager.rb

Overview

Eager class for belongs_to associations.

Class Method Summary

::Mongoid::Association::Eager - Inherited

.new

Instantiate the eager load class.

Instance Method Summary

::Mongoid::Association::Eager - Inherited

#run

Run the preloader.

#each_loaded_document_of_class

Retrieves the documents of the specified class, that have the foreign key included in the specified list of keys.

#shift_association

Shift the current association metadata.

Constructor Details

This class inherits a constructor from Mongoid::Association::Eager

Instance Method Details

#each_loaded_document(&block) (private)

Retrieves the documents referenced by the association, and yields each one sequentially to the provided block. If the association is not polymorphic, all documents are retrieved in a single query. If the association is polymorphic, one query is issued per association target class.

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/eager.rb', line 30

def each_loaded_document(&block)
  if @association.polymorphic?
    keys_by_type_from_docs.each do |type, keys|
      each_loaded_document_of_class(Object.const_get(type), keys, &block)
    end
  else
    super
  end
end

#group_by_key (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/eager.rb', line 63

def group_by_key
  @association.foreign_key
end

#key (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/eager.rb', line 67

def key
  @association.primary_key
end

#keys_by_type_from_docs (private)

Returns a map from association target class name to foreign key values for the documents of that association target class, as referenced by this association.

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/eager.rb', line 43

def keys_by_type_from_docs
  inverse_type_field = @association.inverse_type

  @docs.each_with_object({}) do |doc, keys_by_type|
    next unless doc.respond_to?(inverse_type_field) && doc.respond_to?(group_by_key)
    inverse_type_name = doc.send(inverse_type_field)
    # If a particular document does not have a value for this
    # association, inverse_type_name will be nil.
    next if inverse_type_name.nil?

    key_value = doc.send(group_by_key)
    # If a document has the *_type field set but the corresponding
    # *_id field not set, the key value here will be nil.
    next unless key_value

    keys_by_type[inverse_type_name] ||= []
    keys_by_type[inverse_type_name].push(key_value)
  end
end

#preload (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/eager.rb', line 14

def preload
  @docs.each do |d|
    set_relation(d, nil)
  end

  each_loaded_document do |doc|
    id = doc.send(key)
    set_on_parent(id, doc)
  end
end