aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/preloader
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/preloader')
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb10
-rw-r--r--activerecord/lib/active_record/associations/preloader/collection_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb2
-rw-r--r--activerecord/lib/active_record/associations/preloader/has_many_through.rb2
-rw-r--r--activerecord/lib/active_record/associations/preloader/singular_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/preloader/through_association.rb14
6 files changed, 17 insertions, 19 deletions
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|