Class: ArLazyPreload::Context
- Inherits:
-
Object
- Object
- ArLazyPreload::Context
- Defined in:
- lib/ar_lazy_preload/context.rb
Overview
This class is responsible for holding a connection between a list of ActiveRecord::Base objects which have been loaded by the same instance of ActiveRecord::Relation. It also contains a tree of associations, which were requested to be loaded lazily. Calling #preload_association method will cause loading of ALL associated objects for EACH ecord when requested association is found in the association tree.
Instance Attribute Summary collapse
-
#association_tree ⇒ Object
readonly
Returns the value of attribute association_tree.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Class Method Summary collapse
-
.register(records:, association_tree:) ⇒ Object
Initiates lazy preload context for given records.
Instance Method Summary collapse
-
#initialize(records:, association_tree:) ⇒ Context
constructor
:records - array of ActiveRecord instances :association_tree - list of symbols or hashes representing a tree of preloadable associations.
-
#try_preload_lazily(association_name) ⇒ Object
This method checks if the association is present in the association_tree and preloads for all objects in the context it if needed.
Constructor Details
#initialize(records:, association_tree:) ⇒ Context
:records - array of ActiveRecord instances :association_tree - list of symbols or hashes representing a tree of preloadable associations
23 24 25 26 27 28 |
# File 'lib/ar_lazy_preload/context.rb', line 23 def initialize(records:, association_tree:) @records = records.compact @association_tree = association_tree @records.each { |record| record.lazy_preload_context = self } end |
Instance Attribute Details
#association_tree ⇒ Object (readonly)
Returns the value of attribute association_tree.
19 20 21 |
# File 'lib/ar_lazy_preload/context.rb', line 19 def association_tree @association_tree end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
19 20 21 |
# File 'lib/ar_lazy_preload/context.rb', line 19 def records @records end |
Class Method Details
.register(records:, association_tree:) ⇒ Object
Initiates lazy preload context for given records
13 14 15 16 17 |
# File 'lib/ar_lazy_preload/context.rb', line 13 def self.register(records:, association_tree:) return if records.empty? || association_tree.empty? && !ArLazyPreload.config.auto_preload? ArLazyPreload::Context.new(records: records, association_tree: association_tree) end |
Instance Method Details
#try_preload_lazily(association_name) ⇒ Object
This method checks if the association is present in the association_tree and preloads for all objects in the context it if needed.
32 33 34 35 36 37 |
# File 'lib/ar_lazy_preload/context.rb', line 32 def try_preload_lazily(association_name) return unless association_needs_preload?(association_name) preloader.preload(records, association_name) AssociatedContextBuilder.prepare(parent_context: self, association_name: association_name) end |