diff options
author | jvoorhis <jvoorhis@jeremy-voorhiss-macbook.local> | 2009-03-06 16:47:50 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-03-06 16:47:50 +0000 |
commit | 6a13376525f34a00e013fc3a6022838329dfe856 (patch) | |
tree | b34b909463c39f25de97cf8351ab8452ddfb9d84 | |
parent | c67d25e3c4e636d7c94909a7398231a634accf46 (diff) | |
download | rails-6a13376525f34a00e013fc3a6022838329dfe856.tar.gz rails-6a13376525f34a00e013fc3a6022838329dfe856.tar.bz2 rails-6a13376525f34a00e013fc3a6022838329dfe856.zip |
Methods invoked within named scope Procs should respect the scope stack. [#1267 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
-rw-r--r-- | activerecord/lib/active_record/named_scope.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/models/topic.rb | 2 |
3 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 8a7449f95b..43411dfb55 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -89,7 +89,12 @@ module ActiveRecord when Hash options when Proc - options.call(*args) + case parent_scope + when Scope + with_scope(:find => parent_scope.proxy_options) { options.call(*args) } + else + options.call(*args) + end end, &block) end (class << self; self end).instance_eval do diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index f28285faaf..9f3a3848e2 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -306,6 +306,10 @@ class NamedScopeTest < ActiveRecord::TestCase # Nested hash conditions with different keys assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).all.uniq end + + def test_methods_invoked_within_scopes_should_respect_scope + assert_equal [], Topic.approved.by_rejected_ids.proxy_options[:conditions][:id] + end end class DynamicScopeMatchTest < ActiveRecord::TestCase diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 08bb24ed03..2fe846db48 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -33,6 +33,8 @@ class Topic < ActiveRecord::Base end named_scope :named_extension, :extend => NamedExtension named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] + + named_scope :by_rejected_ids, lambda {{ :conditions => { :id => all(:conditions => {:approved => false}).map(&:id) } }} has_many :replies, :dependent => :destroy, :foreign_key => "parent_id" serialize :content |