Class: ActiveRecord::SmartPreloader
- Inherits:
-
Object
- Object
- ActiveRecord::SmartPreloader
- Defined in:
- lib/active_record/smart_preloader.rb
Constant Summary collapse
- VERSION =
'0.1.0'
Class Method Summary collapse
Instance Method Summary collapse
- #call(records) ⇒ Object
-
#initialize(preloads) ⇒ SmartPreloader
constructor
A new instance of SmartPreloader.
Constructor Details
#initialize(preloads) ⇒ SmartPreloader
Returns a new instance of SmartPreloader.
35 36 37 |
# File 'lib/active_record/smart_preloader.rb', line 35 def initialize(preloads) @preloads = preloads end |
Class Method Details
.call(records, association) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/active_record/smart_preloader.rb', line 7 def self.call(records, association) case association when Hash association.each do |key, value| preloaded_records = call(records, key) call(preloaded_records, value) end when Array association.each do |key| call(records, key) end when Symbol, String ActiveRecord::Associations::Preloader.new.preload(records, association) records.flat_map(&association.to_sym.to_proc).compact when CompositeKey ActiveRecord::CompositeKeyPreloader.(records, association.association, association.key) records.flat_map(&association.association.to_sym.to_proc).compact when Proc records.select(&association) else if association.instance_of?(Class) && association.ancestors.include?(ApplicationRecord) return records.grep(association) end raise ArgumentError, "#{association.inspect} was not recognized for preload" end end |
Instance Method Details
#call(records) ⇒ Object
39 40 41 |
# File 'lib/active_record/smart_preloader.rb', line 39 def call(records) self.class.call(records, @preloads) end |