Class: N1Loader::Goldiloader::ContextAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/n1_loader/goldiloader/context_adapter.rb

Overview

Context adapter for injected N1Loader loaders.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ContextAdapter

Returns a new instance of ContextAdapter.



9
10
11
# File 'lib/n1_loader/goldiloader/context_adapter.rb', line 9

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/n1_loader/goldiloader/context_adapter.rb', line 7

def context
  @context
end

Instance Method Details

#perform_preloading(association_name) ⇒ Object

Initialize preloader for association_name with context builder callback. The callback will be executed when records are loaded.



20
21
22
23
24
25
26
27
# File 'lib/n1_loader/goldiloader/context_adapter.rb', line 20

def perform_preloading(association_name)
  context_setup = lambda { |records|
    ar_records = records.flatten(1).select { |record| record.respond_to?(:auto_include_context=) }
    ::Goldiloader::AutoIncludeContext.register_models(ar_records) unless ar_records.empty?
  }

  N1Loader::Preloader.new(context.models, context_setup).preload(association_name)
end

#try_preload_lazily(association_name) ⇒ Object

Trigger preloading for association_name across all models in the context.



14
15
16
# File 'lib/n1_loader/goldiloader/context_adapter.rb', line 14

def try_preload_lazily(association_name)
  perform_preloading(association_name) if context
end