1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

pass the preloader down so we only have to construct one

This commit is contained in:
Aaron Patterson 2013-09-23 17:10:23 -07:00
parent f34d46ab82
commit 9e60f0f630
7 changed files with 21 additions and 21 deletions

View file

@ -93,7 +93,7 @@ module ActiveRecord
else
associations.flat_map { |association|
preloaders_on association, records, preload_scope
}.each(&:run)
}
end
end
@ -133,7 +133,9 @@ module ActiveRecord
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

View file

@ -20,13 +20,13 @@ module ActiveRecord
@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 @@ module ActiveRecord
end
def preloaded_records
associated_records_by_owner.values.flatten
@associated_records_by_owner.values.flatten
end
def loaded?
@ -82,7 +82,7 @@ module ActiveRecord
private
def associated_records_by_owner
def associated_records_by_owner(preloader)
@loaded = true
return @associated_records_by_owner if @associated_records_by_owner

View file

@ -9,8 +9,8 @@ module ActiveRecord
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)

View file

@ -34,7 +34,7 @@ module ActiveRecord
# 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 = {}

View file

@ -4,7 +4,7 @@ module ActiveRecord
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

View file

@ -5,8 +5,8 @@ module ActiveRecord
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)

View file

@ -11,15 +11,14 @@ module ActiveRecord
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 @@ module ActiveRecord
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|