Skip to content

Commit

Permalink
Fixed that scopes defined with a string name could not be composed
Browse files Browse the repository at this point in the history
  • Loading branch information
tarmo authored and jeremy committed Jun 23, 2008
1 parent 43cbcb1 commit 1afae84
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/named_scope.rb
Expand Up @@ -82,6 +82,7 @@ def scopes
# 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
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -59,6 +59,12 @@ def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specifie
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
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/topic.rb
Expand Up @@ -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
Expand Down

0 comments on commit 1afae84

Please sign in to comment.