mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure NamedScope#build/create/create!/new works as expected when named scope has hash conditions. [Daniel Guettler, Pratik Naik] [#419 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
124d1016fa
commit
84af99e78d
3 changed files with 30 additions and 1 deletions
|
@ -150,7 +150,8 @@ module ActiveRecord
|
|||
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 @@ class NamedScopeTest < ActiveRecord::TestCase
|
|||
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']
|
||||
|
|
Loading…
Reference in a new issue