From 1afae84ab2656cd58a861ab4a4b1745d80088d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Fri, 13 Jun 2008 23:39:10 +0300 Subject: Fixed that scopes defined with a string name could not be composed --- activerecord/lib/active_record/named_scope.rb | 1 + activerecord/test/cases/named_scope_test.rb | 6 ++++++ activerecord/test/models/topic.rb | 1 + 3 files changed, 8 insertions(+) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index b0c8a8b815..eac61e9e43 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -82,6 +82,7 @@ module ActiveRecord # expected_options = { :conditions => { :colored => 'red' } } # assert_equal expected_options, Shirt.colored('red').proxy_options def named_scope(name, options = {}, &block) + name = name.to_sym scopes[name] = lambda do |parent_scope, *args| Scope.new(parent_scope, case options when Hash diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index d890cf7936..73673e0f0e 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -59,6 +59,12 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal Topic.count(:conditions => {:approved => true}), Topic.approved.count end + def test_scopes_with_string_name_can_be_composed + # NOTE that scopes defined with a string as a name worked on their own + # but when called on another scope the other scope was completely replaced + assert_equal Topic.replied.approved, Topic.replied.approved_as_string + end + def test_scopes_are_composable assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index f63e862cfd..423b6fe52b 100755 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -4,6 +4,7 @@ class Topic < ActiveRecord::Base { :conditions => ['written_on < ?', time] } } named_scope :approved, :conditions => {:approved => true} + named_scope 'approved_as_string', :conditions => {:approved => true} named_scope :replied, :conditions => ['replies_count > 0'] named_scope :anonymous_extension do def one -- cgit v1.2.3