Module: Mongoid::Relations::Eager
- Included in:
- Contextual::Memory, Contextual::Mongo
- Defined in:
- lib/mongoid/relations/eager.rb,
lib/mongoid/relations/eager/base.rb,
lib/mongoid/relations/eager/has_one.rb,
lib/mongoid/relations/eager/has_many.rb,
lib/mongoid/relations/eager/belongs_to.rb,
lib/mongoid/relations/eager/has_and_belongs_to_many.rb
Defined Under Namespace
Classes: Base, BelongsTo, HasAndBelongsToMany, HasMany, HasOne
Instance Method Summary
collapse
Instance Method Details
#eager_load(docs) ⇒ Object
16
17
18
19
20
21
22
|
# File 'lib/mongoid/relations/eager.rb', line 16
def eager_load(docs)
docs.tap do |d|
if eager_loadable?
preload(criteria.inclusions, d)
end
end
end
|
#eager_loadable? ⇒ Boolean
12
13
14
|
# File 'lib/mongoid/relations/eager.rb', line 12
def eager_loadable?
!criteria.inclusions.empty?
end
|
#preload(relations, docs) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/mongoid/relations/eager.rb', line 24
def preload(relations, docs)
grouped_relations = relations.group_by do |metadata|
metadata.inverse_class_name
end
grouped_relations.keys.each do |_klass|
grouped_relations[_klass] = grouped_relations[_klass].group_by do |metadata|
metadata.relation
end
end
grouped_relations.each do |_klass, associations|
docs = associations.collect do |_relation, association|
_relation.eager_load_klass.new(association, docs).run
end.flatten
end
end
|