diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 901d4c2..2e8d625 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -90,6 +90,8 @@ module ActiveRecord Scope.new(parent_scope, case options when Hash options + when Scope + options.proxy_options when Proc if self.model_name != parent_scope.model_name options.bind(parent_scope).call(*args) @@ -118,6 +120,7 @@ module ActiveRecord def initialize(proxy_scope, options, &block) options ||= {} + options = options.proxy_options if options.class == ActiveRecord::NamedScope::Scope [options[:extend]].flatten.each { |extension| extend extension } if options[:extend] extend Module.new(&block) if block_given? unless Scope === proxy_scope diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 0a04821..c99e6fa 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -364,3 +364,14 @@ class DynamicScopeTest < ActiveRecord::TestCase assert_equal Post.scoped_by_author_id_and_title(1, "Welcome to the weblog").first, Post.find(:first, :conditions => { :author_id => 1, :title => "Welcome to the weblog"}) end end + +class NamedScopeCallingAnotherNamedScopeTest < ActiveRecord::TestCase + def test_named_scope_can_call_another_named_scope + assert_nothing_raised do + Post.calling_containing_the_letter_a + Post.title_starts_with_w + Post.title_starts_with_w_limit_one + end + assert_equal Post.find(:all, :conditions => ["title LIKE ?", "W%"], :limit => 1), Post.title_starts_with_w_limit_one + end +end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index cf69d04..8815861 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -2,12 +2,17 @@ class Post < ActiveRecord::Base named_scope :with_type_self, lambda{{:conditions => ["type=?", self.name]}} named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'" named_scope :ranked_by_comments, :order => "comments_count DESC" + named_scope :calling_containing_the_letter_a, containing_the_letter_a + named_scope :limit, lambda {|limit| {:limit => limit} } named_scope :with_authors_at_address, lambda { |address| { :conditions => [ 'authors.author_address_id = ?', address.id ], :joins => 'JOIN authors ON authors.id = posts.author_id' } } + named_scope :title_starts_with, lambda {|letter| {:conditions => ["title LIKE ?", letter + "%"]}} + named_scope :title_starts_with_w, title_starts_with("W") + named_scope :title_starts_with_w_limit_one, title_starts_with("W").limit(1) belongs_to :author do def greeting