Permalink
Browse files

Ensure NamedScope#build/create/create!/new works as expected when nam…

…ed scope has hash conditions. [Daniel Guettler, Pratik Naik] [#419 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent 124d101 commit 84af99e78dbf65b7faa92313acf8457cb0c2b510 @dguettler dguettler committed with lifo Jul 9, 2008
@@ -150,7 +150,8 @@ def method_missing(method, *args, &block)
if scopes.include?(method)
scopes[method].call(self, *args)
else
- with_scope :find => proxy_options do
+ with_scope :find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {} do
+ method = :new if method == :build
proxy_scope.send(method, *args, &block)
end
end
@@ -183,4 +183,30 @@ def test_empty_should_not_load_results
topics.empty? # use loaded (no query)
end
end
+
+ def test_should_build_with_proxy_options
+ topic = Topic.approved.build({})
+ assert topic.approved
+ end
+
+ def test_should_build_new_with_proxy_options
+ topic = Topic.approved.new
+ assert topic.approved
+ end
+
+ def test_should_create_with_proxy_options
+ topic = Topic.approved.create({})
+ assert topic.approved
+ end
+
+ def test_should_create_with_bang_with_proxy_options
+ topic = Topic.approved.create!({})
+ assert topic.approved
+ end
+
+ def test_should_build_with_proxy_options_chained
+ topic = Topic.approved.by_lifo.build({})
+ assert topic.approved
+ assert_equal 'lifo', topic.author_name
+ end
end
@@ -4,6 +4,8 @@ class Topic < ActiveRecord::Base
{ :conditions => ['written_on < ?', time] }
}
named_scope :approved, :conditions => {:approved => true}
+ named_scope :by_lifo, :conditions => {:author_name => 'lifo'}
+
named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
named_scope 'approved_as_string', :conditions => {:approved => true}
named_scope :replied, :conditions => ['replies_count > 0']

0 comments on commit 84af99e

Please sign in to comment.