public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Create through associations can now work with blocks.

Signed-off-by: Michael Koziarski <michael@koziarski.com>

[#248 state:resolved]
ryanb (author)
Fri May 23 14:57:11 -0700 2008
NZKoz (committer)
Fri May 23 23:26:13 -0700 2008
commit  6cba97d2a449faf21aec9fe9d4434067e414226f
tree    3c00a108230fe3471882678f362f4c138e6567df
parent  b88ceb7dc8d31bdbea95ab4242bbdee17178cda9
...
166
167
168
169
 
 
 
 
170
171
172
173
174
 
 
 
 
175
176
177
...
166
167
168
 
169
170
171
172
173
174
175
176
 
177
178
179
180
181
182
183
0
@@ -166,12 +166,18 @@ module ActiveRecord
0
         if attrs.is_a?(Array)
0
           attrs.collect { |attr| create(attr) }
0
         else
0
-          create_record(attrs) { |record| record.save }
0
+          create_record(attrs) do |record|
0
+            yield(record) if block_given?
0
+            record.save
0
+          end
0
         end
0
       end
0
 
0
       def create!(attrs = {})
0
-        create_record(attrs) { |record| record.save! }
0
+        create_record(attrs) do |record|
0
+          yield(record) if block_given?
0
+          record.save!
0
+        end
0
       end
0
 
0
       # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and
...
161
162
163
164
165
166
167
168
169
 
 
 
 
 
 
 
 
 
170
171
172
...
161
162
163
 
 
 
 
 
 
164
165
166
167
168
169
170
171
172
173
174
175
0
@@ -161,12 +161,15 @@ class AssociationProxyTest < ActiveRecord::TestCase
0
   end
0
 
0
   def test_create_via_association_with_block
0
-    post1 = Post.create(:title => "setting body with a block") {|p| p.body = "will work"}
0
-    assert_equal post1.body, "will work"
0
-    assert_nothing_raised do
0
-      post2 = authors(:david).posts.create(:title => "setting body with a block") {|p| p.body = "won't work"}
0
-    end
0
-    assert_equal post2.body, "won't work"
0
+    post = authors(:david).posts.create(:title => "New on Edge") {|p| p.body = "More cool stuff!"}
0
+    assert_equal post.title, "New on Edge"
0
+    assert_equal post.body, "More cool stuff!"
0
+  end
0
+
0
+  def test_create_with_bang_via_association_with_block
0
+    post = authors(:david).posts.create!(:title => "New on Edge") {|p| p.body = "More cool stuff!"}
0
+    assert_equal post.title, "New on Edge"
0
+    assert_equal post.body, "More cool stuff!"
0
   end
0
 
0
   def test_failed_reload_returns_nil

Comments