From 3e39cf06057ffdce7de6c2f024a0bb9381a61e5d Mon Sep 17 00:00:00 2001 From: jvoorhis Date: Fri, 24 Oct 2008 09:51:06 -0700 Subject: [PATCH] Methods invoked within named scope Procs should respect the scope stack. --- activerecord/lib/active_record/named_scope.rb | 9 ++++++++- activerecord/test/cases/named_scope_test.rb | 6 ++++++ activerecord/test/models/topic.rb | 2 ++ 3 files changed, 16 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 83043c2..195928a 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -88,7 +88,14 @@ module ActiveRecord when Hash options when Proc - options.call(*args) + case parent_scope + when Scope + with_scope :find => parent_scope.proxy_options do + options.call(*args) + end + 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 64e8997..d9fbcfe 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -277,4 +277,10 @@ class NamedScopeTest < ActiveRecord::TestCase post = Post.find(1) assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size end + + def test_methods_invoked_within_scopes_should_respect_scope + assert_sql /SELECT .?id.? FROM .?topics.? WHERE \(.?topics.?\..?approved.? = / do + Topic.approved.by_find_ids + end + end end diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 39ca1bf..47dbca8 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -31,6 +31,8 @@ class Topic < ActiveRecord::Base end named_scope :named_extension, :extend => NamedExtension named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] + + named_scope :by_find_ids, lambda { { :conditions => { :id => find(:all, :select => 'id').map { |t| t.id } } } } has_many :replies, :dependent => :destroy, :foreign_key => "parent_id" serialize :content -- 1.5.6.4