Class: ArLazyPreload::AssociatedContextBuilder
- Inherits:
-
Object
- Object
- ArLazyPreload::AssociatedContextBuilder
- 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
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#parent_context ⇒ Object
readonly
Returns the value of attribute parent_context.
Class Method Summary collapse
-
.prepare(**args) ⇒ Object
Initiates lazy preload context the records loaded lazily.
Instance Method Summary collapse
-
#initialize(parent_context:, association_name:) ⇒ AssociatedContextBuilder
constructor
:parent_context - root context :association_name - lazily preloaded association name.
-
#perform ⇒ Object
Takes all the associated records for the records, attached to the :parent_context and creates a preloading context for them rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
Constructor Details
#initialize(parent_context:, association_name:) ⇒ AssociatedContextBuilder
:parent_context - root context :association_name - lazily preloaded association name
19 20 21 22 |
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 19 def initialize(parent_context:, association_name:) @parent_context = parent_context @association_name = association_name end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
15 16 17 |
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 15 def association_name @association_name end |
#parent_context ⇒ Object (readonly)
Returns the value of attribute parent_context.
15 16 17 |
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 15 def parent_context @parent_context end |
Class Method Details
.prepare(**args) ⇒ Object
Initiates lazy preload context the records loaded lazily
11 12 13 |
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 11 def self.prepare(**args) new(**args).perform end |
Instance Method Details
#perform ⇒ Object
Takes all the associated records for the records, attached to the :parent_context and creates a preloading context for them rubocop:disable Metrics/MethodLength, Metrics/AbcSize
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ar_lazy_preload/associated_context_builder.rb', line 27 def perform associated_records = parent_context.records.flat_map do |record| next if record.nil? reflection = reflection_cache[record.class] next if reflection.nil? record_association = record.association(association_name) reflection.collection? ? record_association.target : record_association.reader end Context.register( records: associated_records, association_tree: child_association_tree, auto_preload: parent_context.auto_preload? ) end |