Module: NoBrainer::Document::Association::EagerLoader

Extended by:
EagerLoader
Included in:
EagerLoader
Defined in:
lib/no_brainer/document/association/eager_loader.rb

Defined Under Namespace

Modules: Generic

Instance Method Summary collapse

Instance Method Details

#eager_load(docs, what) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/no_brainer/document/association/eager_loader.rb', line 59

def eager_load(docs, what)
  case what
  when Hash then what.each do |k,v|
    if v.is_a?(NoBrainer::Criteria)
      v = v.dup
      nested_preloads, v.options[:eager_load] = v.options[:eager_load], []
      eager_load(eager_load_association(docs, k, v), nested_preloads)
    else
      eager_load(eager_load_association(docs, k), v)
    end
  end
  when Array then what.each { |v| eager_load(docs, v) }
  when nil then;
  else eager_load_association(docs, what) # String and Symbol
  end
  true
end

#eager_load_association(docs, association_name, criteria = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/no_brainer/document/association/eager_loader.rb', line 46

def eager_load_association(docs, association_name, criteria=nil)
  docs = docs.compact
  return [] if docs.empty?

  meta = docs.first.class.
  root_meta = docs.first.root_class.
  association = meta[association_name.to_sym] || root_meta[association_name.to_sym] ||
    meta[association_name.to_s.singularize.to_sym] || root_meta[association_name.to_s.singularize.to_sym]

  raise "Unknown association #{association_name}" unless association
  association.eager_load(docs, criteria)
end