Class: ArLazyPreload::Contexts::BaseContext

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

Overview

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.

Direct Known Subclasses

AutoPreloadContext, LazyPreloadContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records:) ⇒ BaseContext

:records - array of ActiveRecord instances



15
16
17
18
19
20
# 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

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



12
13
14
# File 'lib/ar_lazy_preload/contexts/base_context.rb', line 12

def records
  @records
end

Instance Method Details

#association_treeObject



23
# File 'lib/ar_lazy_preload/contexts/base_context.rb', line 23

def association_tree; nil; end

#auto_preload?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ar_lazy_preload/contexts/base_context.rb', line 34

def auto_preload?
  false
end

#try_preload_lazily(association_name) ⇒ Object

This method checks if the association should be loaded and preloads it for all objects in the context it if needed.



27
28
29
30
31
32
# 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