Module: Miscellany::GoldiloadValue

Defined in:
lib/miscellany/active_record/goldiload_value.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#goldi_valuesObject

Returns the value of attribute goldi_values.



3
4
5
# File 'lib/miscellany/active_record/goldiload_value.rb', line 3

def goldi_values
  @goldi_values
end

Class Method Details

.installObject



17
18
19
# File 'lib/miscellany/active_record/goldiload_value.rb', line 17

def self.install
  ::ActiveRecord::Base.include(GoldiloadValue)
end

Instance Method Details

#goldiload_value(key, &blk) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/miscellany/active_record/goldiload_value.rb', line 5

def goldiload_value(key, &blk)
  return goldi_values[key] if goldi_values && goldi_values.key?(key)

  models = auto_include_context.models
  loaded = blk.call(models)
  models.each do |m|
    (m.goldi_values ||= {})[key] = loaded.key?(m) ? loaded[m] : loaded[m&.id]
  end

  goldi_values[key]
end