Class: PredictiveLoad::PreloadLog

Inherits:
ActiveRecord::Associations::Preloader
  • Object
show all
Defined in:
lib/predictive_load/preload_log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/predictive_load/preload_log.rb', line 5

def logger
  @logger
end

Instance Method Details

#collection_arel(preloader) ⇒ Object



27
28
29
30
31
# File 'lib/predictive_load/preload_log.rb', line 27

def collection_arel(preloader)
  owners_map = preloader.owners_by_key
  owner_keys = owners_map.keys.compact
  preloader.association_key.in(owner_keys)
end

#log(message) ⇒ Object



33
34
35
# File 'lib/predictive_load/preload_log.rb', line 33

def log(message)
  ActiveRecord::Base.logger.info("predictive_load: #{message}")
end

#preload(association) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/predictive_load/preload_log.rb', line 7

def preload(association)
  grouped_records(association).each do |reflection, klasses|
    klasses.each do |klass, records|
      preloader = preloader_for(reflection).new(klass, records, reflection, preload_scope)

      if preloader.respond_to?(:through_reflection)
        log("encountered :through association for #{association}. Requires loading records to generate query, so skipping for now.")
        next
      end

      preload_sql = preloader.scope.where(collection_arel(preloader)).to_sql

      log("would preload with: #{preload_sql}")
      klass.connection.explain(preload_sql).each_line do |line|
        log(line)
      end
    end
  end
end