Class: NoBrainer::Document::Association::HasManyThrough::Metadata

Inherits:
Object
  • Object
show all
Includes:
Core::Metadata
Defined in:
lib/no_brainer/document/association/has_many_through.rb

Constant Summary collapse

VALID_OPTIONS =
[:through, :scope]

Instance Attribute Summary

Attributes included from Core::Metadata

#options, #owner_model, #target_name

Instance Method Summary collapse

Methods included from Core::Metadata

#add_callback_for, #association_model, #delegate, #get_model_by_name, #hook, #initialize, #new

Instance Method Details

#eager_load(docs, additional_criteria = nil) ⇒ Object



17
18
19
20
21
22
# File 'lib/no_brainer/document/association/has_many_through.rb', line 17

def eager_load(docs, additional_criteria=nil)
  criteria = target_model.instance_exec(&options[:scope]) if options[:scope]
  criteria = criteria ? criteria.merge(additional_criteria) : additional_criteria if additional_criteria
  NoBrainer::Document::Association::EagerLoader
    .eager_load_association(through_association.eager_load(docs), target_name, criteria)
end

#target_modelObject



24
25
26
27
28
# File 'lib/no_brainer/document/association/has_many_through.rb', line 24

def target_model
  meta = through_association.target_model.
  association = meta[target_name.to_sym] || meta[target_name.to_s.singularize.to_sym]
  association.target_model
end

#through_associationObject



12
13
14
15
# File 'lib/no_brainer/document/association/has_many_through.rb', line 12

def through_association
  owner_model.[through_association_name] or
    raise "#{through_association_name} association not found"
end

#through_association_nameObject



8
9
10
# File 'lib/no_brainer/document/association/has_many_through.rb', line 8

def through_association_name
  options[:through].to_sym
end