Permalink
Browse files

pass the preloader down so we only have to construct one

  • Loading branch information...
1 parent f34d46a commit 9e60f0f630f5e122296d05da0cf2d3b3d55526a5 @tenderlove tenderlove committed Sep 24, 2013
@@ -93,7 +93,7 @@ def preload(records, associations, preload_scope = nil)
else
associations.flat_map { |association|
preloaders_on association, records, preload_scope
- }.each(&:run)
+ }
end
end
@@ -133,7 +133,9 @@ def preloaders_for_hash(association, records, scope)
def preloaders_for_one(association, records, scope)
grouped_records(association, records).flat_map do |reflection, klasses|
klasses.map do |rhs_klass, rs|
- preloader_for(reflection).new(rhs_klass, rs, reflection, scope)
+ loader = preloader_for(reflection).new(rhs_klass, rs, reflection, scope)
+ loader.run self
+ loader
end
end
end
@@ -20,13 +20,13 @@ def initialize(klass, owners, reflection, preload_scope)
@loaded = false
end
- def run
+ def run(preloader)
unless owners.first.association(reflection.name).loaded?
- preload
+ preload(preloader)
end
end
- def preload
+ def preload(preloader)
raise NotImplementedError
end
@@ -73,7 +73,7 @@ def options
end
def preloaded_records
- associated_records_by_owner.values.flatten
+ @associated_records_by_owner.values.flatten
end
def loaded?
@@ -82,7 +82,7 @@ def loaded?
private
- def associated_records_by_owner
+ def associated_records_by_owner(preloader)
@loaded = true
return @associated_records_by_owner if @associated_records_by_owner
@@ -9,8 +9,8 @@ def build_scope
super.order(preload_scope.values[:order] || reflection_scope.values[:order])
end
- def preload
- associated_records_by_owner.each do |owner, records|
+ def preload(preloader)
+ associated_records_by_owner(preloader).each do |owner, records|
association = owner.association(reflection.name)
association.loaded!
association.target.concat(records)
@@ -34,7 +34,7 @@ def association_key
# Once we have used the join table column (in super), we manually instantiate the
# actual records, ensuring that we don't create more than one instances of the same
# record
- def associated_records_by_owner
+ def associated_records_by_owner(preloader)
return @associated_records_by_owner if @associated_records_by_owner
records = {}
@@ -4,7 +4,7 @@ class Preloader
class HasManyThrough < CollectionAssociation #:nodoc:
include ThroughAssociation
- def associated_records_by_owner
+ def associated_records_by_owner(preloader)
return @associated_records_by_owner if @associated_records_by_owner
records_by_owner = super
@@ -5,8 +5,8 @@ class SingularAssociation < Association #:nodoc:
private
- def preload
- associated_records_by_owner.each do |owner, associated_records|
+ def preload(preloader)
+ associated_records_by_owner(preloader).each do |owner, associated_records|
record = associated_records.first
association = owner.association(reflection.name)
@@ -11,15 +11,14 @@ def source_reflection
reflection.source_reflection
end
- def associated_records_by_owner
+ def associated_records_by_owner(preloader)
@loaded = true
return @associated_records_by_owner if @associated_records_by_owner
- left_loader = Preloader.new
- left_loader.preload(owners,
- through_reflection.name,
- through_scope)
+ preloader.preload(owners,
+ through_reflection.name,
+ through_scope)
should_reset = (through_scope != through_reflection.klass.unscoped) ||
(reflection.options[:source_type] && through_reflection.collection?)
@@ -37,10 +36,9 @@ def associated_records_by_owner
middle_records = through_records.map { |(_,rec,_)| rec }.flatten
- preloader = Preloader.new
preloaders = preloader.preload(middle_records,
- source_reflection.name,
- reflection_scope)
+ source_reflection.name,
+ reflection_scope)
middle_to_pl = preloaders.each_with_object({}) do |pl,h|
pl.owners.each { |middle|

0 comments on commit 9e60f0f

Please sign in to comment.