Class: PredictiveLoad::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/predictive_load/loader.rb

Overview

Predictive loader

Usage: ActiveRecord::Relation.collection_observer = LazyLoader

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ Loader

Returns a new instance of Loader.



12
13
14
# File 'lib/predictive_load/loader.rb', line 12

def initialize(records)
  @records = records
end

Class Method Details

.observe(records) ⇒ Object



8
9
10
# File 'lib/predictive_load/loader.rb', line 8

def self.observe(records)
  new(records).observe
end

Instance Method Details

#loading_association(record, association) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/predictive_load/loader.rb', line 22

def loading_association(record, association)
  association_name = association.reflection.name

  if all_records_will_likely_load_association?(association_name) && supports_preload?(association)
    PredictiveLoad.callback&.call(record, association)
    preload(association_name)
  end
end

#observeObject



16
17
18
19
20
# File 'lib/predictive_load/loader.rb', line 16

def observe
  records.each do |record|
    record.collection_observer = self
  end
end