diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-23 17:10:23 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-23 17:10:23 -0700 |
commit | 9e60f0f630f5e122296d05da0cf2d3b3d55526a5 (patch) | |
tree | 593ceadd9ef062164e03d12ea9ea1dffe292715b /activerecord/lib | |
parent | f34d46ab824e3db121d0e02c7859b6aceb8093d9 (diff) | |
download | rails-9e60f0f630f5e122296d05da0cf2d3b3d55526a5.tar.gz rails-9e60f0f630f5e122296d05da0cf2d3b3d55526a5.tar.bz2 rails-9e60f0f630f5e122296d05da0cf2d3b3d55526a5.zip |
pass the preloader down so we only have to construct one
Diffstat (limited to 'activerecord/lib')
7 files changed, 21 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb index e8abea3bcb..b99ae35809 100644 --- a/activerecord/lib/active_record/associations/preloader.rb +++ b/activerecord/lib/active_record/associations/preloader.rb @@ -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 diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index a57284fd06..9607afeea2 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -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 diff --git a/activerecord/lib/active_record/associations/preloader/collection_association.rb b/activerecord/lib/active_record/associations/preloader/collection_association.rb index e6cd35e7a1..5adffcd831 100644 --- a/activerecord/lib/active_record/associations/preloader/collection_association.rb +++ b/activerecord/lib/active_record/associations/preloader/collection_association.rb @@ -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) diff --git a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb index fce3cb78e0..bd72bbc56f 100644 --- a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +++ b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb @@ -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 = {} diff --git a/activerecord/lib/active_record/associations/preloader/has_many_through.rb b/activerecord/lib/active_record/associations/preloader/has_many_through.rb index 39a938c266..341e73f127 100644 --- a/activerecord/lib/active_record/associations/preloader/has_many_through.rb +++ b/activerecord/lib/active_record/associations/preloader/has_many_through.rb @@ -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 diff --git a/activerecord/lib/active_record/associations/preloader/singular_association.rb b/activerecord/lib/active_record/associations/preloader/singular_association.rb index 44e804d785..2b5cfda8ce 100644 --- a/activerecord/lib/active_record/associations/preloader/singular_association.rb +++ b/activerecord/lib/active_record/associations/preloader/singular_association.rb @@ -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) diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb index be3e179543..60f8c754f6 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -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| |