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.



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

def logger
  @logger
end

Instance Method Details

#collection_arel(preloader) ⇒ Object



30
31
32
33
34
# File 'lib/predictive_load/preload_log.rb', line 30

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



36
37
38
# File 'lib/predictive_load/preload_log.rb', line 36

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

#preload(association) ⇒ Object



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

def preload(association)
  grouped_records(association).each do |reflection, klasses|
    klasses.each do |klass, records|
      preload_scope = (ActiveRecord::VERSION::MAJOR == 3 ? options : self.preload_scope)
      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

      scope = (ActiveRecord::VERSION::MAJOR == 3 ? preloader.scoped : preloader.scope)
      preload_sql = scope.where(collection_arel(preloader)).to_sql

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