class ArLazyPreload::Contexts::BaseContext
This is a base context class, which is responsible for holding a connection between a list of ActiveRecord::Base objects which have been loaded by the same instance of ActiveRecord::Relation.
Attributes
records[R]
Public Class Methods
new(records:)
click to toggle source
:records - array of ActiveRecord instances
# File lib/ar_lazy_preload/contexts/base_context.rb, line 15 def initialize(records:) @records = records.dup @records.compact! @records.uniq! @records.each { |record| record.lazy_preload_context = self } end
Public Instance Methods
association_tree()
click to toggle source
@api
# File lib/ar_lazy_preload/contexts/base_context.rb, line 23 def association_tree; nil; end
auto_preload?()
click to toggle source
# File lib/ar_lazy_preload/contexts/base_context.rb, line 34 def auto_preload? false end
try_preload_lazily(association_name)
click to toggle source
This method checks if the association should be loaded and preloads it for all objects in the context it if needed.
# File lib/ar_lazy_preload/contexts/base_context.rb, line 27 def try_preload_lazily(association_name) return if association_loaded?(association_name) || !association_needs_preload?(association_name) perform_preloading(association_name) end
Protected Instance Methods
association_needs_preload?(_association_name)
click to toggle source
# File lib/ar_lazy_preload/contexts/base_context.rb, line 40 def association_needs_preload?(_association_name) raise NotImplementedError end
Private Instance Methods
association_loaded?(association_name)
click to toggle source
# File lib/ar_lazy_preload/contexts/base_context.rb, line 70 def association_loaded?(association_name) loaded_association_names.include?(association_name) end
loaded_association_names()
click to toggle source
# File lib/ar_lazy_preload/contexts/base_context.rb, line 74 def loaded_association_names @loaded_association_names ||= Set.new end
perform_preloading(association_name)
click to toggle source
# File lib/ar_lazy_preload/contexts/base_context.rb, line 46 def perform_preloading(association_name) filtered_records = records.select do |record| reflection_names_cache[record.class].include?(association_name) end preload_records(association_name, filtered_records) loaded_association_names.add(association_name) AssociatedContextBuilder.prepare( parent_context: self, association_name: association_name ) end
preload_records(association_name, records)
click to toggle source
Method preloads associations for the specific sets of the records and provides automatically provides context for the records loaded using `includes` inside Relation#preload_associations
with the help of the TemporaryPreloadConfig
# File lib/ar_lazy_preload/contexts/base_context.rb, line 64 def preload_records(association_name, records) TemporaryPreloadConfig.within_context do preloader.preload(records, association_name) end end
preloader()
click to toggle source
# File lib/ar_lazy_preload/contexts/base_context.rb, line 78 def preloader @preloader ||= ActiveRecord::Associations::Preloader.new end
reflection_names_cache()
click to toggle source
# File lib/ar_lazy_preload/contexts/base_context.rb, line 82 def reflection_names_cache @reflection_names_cache ||= Hash.new do |hash, klass| hash[klass] = klass.reflect_on_all_associations.map(&:name) end end