From 4b919d3b6fda8c384a1d0f68dff64695a32aec80 Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Thu, 19 Mar 2009 09:55:12 -0400 Subject: [PATCH] Through Associations respect default_scoping --- .../associations/association_collection.rb | 2 +- .../associations/association_proxy.rb | 16 +++++++++++++--- .../associations/belongs_to_association.rb | 2 +- .../belongs_to_polymorphic_association.rb | 2 +- .../associations/has_many_through_association.rb | 2 +- .../associations/has_one_association.rb | 2 +- .../associations/has_one_through_association.rb | 2 +- 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 3aef1b2..a3a4dd3 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -391,7 +391,7 @@ module ActiveRecord @target = Array.new end - def find_target + def target_finder records = if @reflection.options[:finder_sql] @reflection.klass.find_by_sql(@finder_sql) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 676c4ac..80bac44 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -225,9 +225,6 @@ module ActiveRecord # Loads the \target if needed and returns it. # - # This method is abstract in the sense that it relies on +find_target+, - # which is expected to be provided by descendants. - # # If the \target is already \loaded it is just returned. Thus, you can call # +load_target+ unconditionally to get the \target. # @@ -274,6 +271,19 @@ module ActiveRecord def owner_quoted_id @owner.quoted_id end + + # This method is abstract in the sense that it relies on +target_finder+, + # which is expected to be provided by descendants. + def find_target + return target_finder unless (scoping = through_default_scoping) + @reflection.klass.send(:with_scope, scoping) { target_finder } + end + + def through_default_scoping + if @reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) + @reflection.through_reflection.klass.send(:eval_default_scoping) + end + end end end end diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index f05c6be..9be4690 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -40,7 +40,7 @@ module ActiveRecord end private - def find_target + def target_finder @reflection.klass.find( @owner[@reflection.primary_key_name], :select => @reflection.options[:select], diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index d8146da..6118a7c 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -22,7 +22,7 @@ module ActiveRecord end private - def find_target + def target_finder return nil if association_class.nil? if @reflection.options[:conditions] diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 1c091e7..89b553d 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -68,7 +68,7 @@ module ActiveRecord end end - def find_target + def target_finder return [] unless target_reflection_has_associated_record? @reflection.klass.find(:all, :select => construct_select, diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index b92cbbd..cb7b22e 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -73,7 +73,7 @@ module ActiveRecord end private - def find_target + def target_finder @reflection.klass.find(:first, :conditions => @finder_sql, :select => @reflection.options[:select], diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 8073eba..8a25384 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -19,7 +19,7 @@ module ActiveRecord super(args.merge(:limit => 1)) end - def find_target + def target_finder super.first end -- 1.5.5