Class: ArLazyPreload::AssociatedContextBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_lazy_preload/associated_context_builder.rb

Overview

This class is responsible for building context for associated records. Given a list of records belonging to the same context and association name it will create and attach a new context to the associated records based on the parent association tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_context:, association_name:) ⇒ AssociatedContextBuilder

Returns a new instance of AssociatedContextBuilder.



12
13
14
15
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 12

def initialize(parent_context:, association_name:)
  @parent_context = parent_context
  @association_name = association_name
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



10
11
12
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 10

def association_name
  @association_name
end

#parent_contextObject (readonly)

Returns the value of attribute parent_context.



10
11
12
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 10

def parent_context
  @parent_context
end

Instance Method Details

#performObject

Takes all the associated records for the records, attached to the :parent_context and creates a preloading context for them



21
22
23
24
25
26
27
28
29
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 21

def perform
  return if child_association_tree.blank? || associated_records.blank?

  Context.new(
    model: reflection.klass,
    records: associated_records,
    association_tree: child_association_tree
  )
end