Module: Squeel::Adapters::ActiveRecord::PreloaderExtensions

Defined in:
lib/squeel/adapters/active_record/preloader_extensions.rb,
lib/squeel/adapters/active_record/4.1/preloader_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


6
7
8
9
10
# File 'lib/squeel/adapters/active_record/preloader_extensions.rb', line 6

def self.included(base)
  base.class_eval do
    alias_method_chain :run, :squeel
  end
end

Instance Method Details

#preload_with_squeel(records, associations, preload_scope = nil) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/squeel/adapters/active_record/4.1/preloader_extensions.rb', line 12

def preload_with_squeel(records, associations, preload_scope = nil)
  records       = Array.wrap(records).compact.uniq
  associations  = Array.wrap(associations)
  preload_scope = preload_scope || ::ActiveRecord::Associations::Preloader::NULL_RELATION

  if records.empty?
    []
  else
    Visitors::PreloadVisitor.new.accept(associations).flat_map do |association|
      preloaders_on(association, records, preload_scope)
    end
  end
end

#run_with_squeelObject


12
13
14
15
16
# File 'lib/squeel/adapters/active_record/preloader_extensions.rb', line 12

def run_with_squeel
  unless records.empty?
    Visitors::PreloadVisitor.new.accept(associations).each { |association| preload(association) }
  end
end