Module: Goldiloader::CustomPreloads
- Included in:
- AutoIncludeContext
- Defined in:
- lib/goldiloader/custom_preloads.rb
Instance Method Summary collapse
Instance Method Details
#initialize ⇒ Object
5 6 7 8 |
# File 'lib/goldiloader/custom_preloads.rb', line 5 def initialize super @custom_preloads = nil end |
#preloaded(model, cache_name:, key:, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/goldiloader/custom_preloads.rb', line 10 def preloaded(model, cache_name:, key:, &block) unless preloaded?(cache_name) ids = models.map do |record| key_from_record(record, key) end # We're using instance_exec instead of a simple yield to make sure that the # given block does not have any references to the model instance as this might # lead to unexpected results. The block will be executed in the context of the # class of the model instead. block_context = models.first.class preloaded_hash = block_context.instance_exec(ids, &block) store_preloaded(cache_name, preloaded_hash) end fetch_preloaded(cache_name, model, key: key) end |