public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
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>
dguettler (author)
Wed Jul 09 06:08:04 -0700 2008
lifo (committer)
Wed Jul 09 06:09:17 -0700 2008
commit  84af99e78dbf65b7faa92313acf8457cb0c2b510
tree    0d65994dd3bd447f50abb98da88752869836b8d2
parent  124d1016fa212c008e33853912493fa9ac15d086
...
150
151
152
153
 
 
154
155
156
...
150
151
152
 
153
154
155
156
157
0
@@ -150,7 +150,8 @@ module ActiveRecord
0
         if scopes.include?(method)
0
           scopes[method].call(self, *args)
0
         else
0
- with_scope :find => proxy_options do
0
+ with_scope :find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {} do
0
+ method = :new if method == :build
0
             proxy_scope.send(method, *args, &block)
0
           end
0
         end
...
183
184
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
0
@@ -183,4 +183,30 @@ class NamedScopeTest < ActiveRecord::TestCase
0
       topics.empty? # use loaded (no query)
0
     end
0
   end
0
+
0
+ def test_should_build_with_proxy_options
0
+ topic = Topic.approved.build({})
0
+ assert topic.approved
0
+ end
0
+
0
+ def test_should_build_new_with_proxy_options
0
+ topic = Topic.approved.new
0
+ assert topic.approved
0
+ end
0
+
0
+ def test_should_create_with_proxy_options
0
+ topic = Topic.approved.create({})
0
+ assert topic.approved
0
+ end
0
+
0
+ def test_should_create_with_bang_with_proxy_options
0
+ topic = Topic.approved.create!({})
0
+ assert topic.approved
0
+ end
0
+
0
+ def test_should_build_with_proxy_options_chained
0
+ topic = Topic.approved.by_lifo.build({})
0
+ assert topic.approved
0
+ assert_equal 'lifo', topic.author_name
0
+ end
0
 end
...
4
5
6
 
 
7
8
9
...
4
5
6
7
8
9
10
11
0
@@ -4,6 +4,8 @@ class Topic < ActiveRecord::Base
0
     { :conditions => ['written_on < ?', time] }
0
   }
0
   named_scope :approved, :conditions => {:approved => true}
0
+ named_scope :by_lifo, :conditions => {:author_name => 'lifo'}
0
+
0
   named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
0
   named_scope 'approved_as_string', :conditions => {:approved => true}
0
   named_scope :replied, :conditions => ['replies_count > 0']

Comments

    No one has commented yet.