123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Referenced::HasAndBelongsToMany::Eager

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

Overview

Eager class for has_and_belongs_to_many 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

#group_by_key (private)

[ GitHub ]

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

def group_by_key
  @association.foreign_key
end

#key (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb', line 47

def key
  @association.primary_key
end

#keys_from_docs (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb', line 31

def keys_from_docs
  keys = Set.new
  @docs.each do |d|
    keys += d.send(group_by_key)
  end
  keys.to_a
end

#preload (private)

[ GitHub ]

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

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

  entries = {}
  each_loaded_document do |doc|
    entries[doc.send(key)] = doc
  end

  @docs.each do |d|
    keys = d.send(group_by_key)
    docs = entries.values_at(*keys).compact
    set_relation(d, docs)
  end
end

#set_relation(doc, element) (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb', line 39

def set_relation(doc, element)
  doc.__build__(@association.name, element, @association) unless doc.blank?
end