Skip to content
Browse files

access preloaders independently of model mutations

  • Loading branch information...
1 parent 31744e4 commit ec7cc6aa176e94c79eba7a1b04360f13eaa328c0 @tenderlove tenderlove committed Sep 20, 2013
Showing with 12 additions and 12 deletions.
  1. +12 −12 activerecord/lib/active_record/associations/preloader.rb
View
24 activerecord/lib/active_record/associations/preloader.rb
@@ -86,24 +86,29 @@ def initialize(records, associations, preload_scope = nil)
@records = Array.wrap(records).compact.uniq
@associations = Array.wrap(associations)
@preload_scope = preload_scope || NULL_RELATION
+ @preloaders = nil
end
NULL_RELATION = Struct.new(:values).new({})
def run
- run_preload associations, records
+ preloaders.each(&:run)
end
- private
+ def preloaders
+ return @preloaders if @preloaders
- def run_preload(associations, records)
- unless records.empty?
- associations.flat_map { |association|
+ if records.empty?
+ @preloaders = []
+ else
+ @preloaders = associations.flat_map { |association|
preload(association, records)
- }.each(&:run)
+ }
end
end
+ private
+
def preload(association, records)
case association
when Hash
@@ -123,12 +128,7 @@ def preloaders_for_hash(association, records)
loaders = preloaders_for_one parent, records
recs = loaders.flat_map(&:target_records).uniq
- lls = Array.wrap(child).flat_map { |assoc| preload assoc, recs }
- loaders + lls
- end
-
- def preload_hash(association, records)
- preloaders_for_hash(association, records).each(&:run)
+ loaders.concat Array.wrap(child).flat_map { |assoc| preload assoc, recs }
end
# Not all records have the same class, so group then preload group on the reflection

0 comments on commit ec7cc6a

Please sign in to comment.
Something went wrong with that request. Please try again.