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



16
17
18
19
20
21
# File 'lib/ar_lazy_preload/contexts/base_context.rb', line 16

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.



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

def records
  @records
end

Instance Method Details

#association_treeObject



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

def association_tree; nil; end

#auto_preload?Boolean

Returns:

  • (Boolean)


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

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.



28
29
30
31
32
33
# File 'lib/ar_lazy_preload/contexts/base_context.rb', line 28

def try_preload_lazily(association_name)
  return if association_loaded?(association_name) ||
            !association_needs_preload?(association_name)

  perform_preloading(association_name)
end