File tree Expand file tree Collapse file tree 5 files changed +56
-4
lines changed Expand file tree Collapse file tree 5 files changed +56
-4
lines changed Original file line number Diff line number Diff line change 1
1
*SVN*
2
2
3
+ * Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39]
4
+
3
5
* Fixed that pessimistic locking you reference the quoted table name (Josh Susser) [#67]
4
6
5
7
* Fixed that change_column should be able to use :null => true on a field that formerly had false [Nate Wiger] [#26]
Original file line number Diff line number Diff line change @@ -603,13 +603,25 @@ def exists?(id_or_conditions)
603
603
# ==== Examples
604
604
# # Create a single new object
605
605
# User.create(:first_name => 'Jamie')
606
+ #
606
607
# # Create an Array of new objects
607
608
# User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}])
608
- def create ( attributes = nil )
609
+ #
610
+ # # Create a single object and pass it into a block to set other attributes.
611
+ # User.create(:first_name => 'Jamie') do |u|
612
+ # u.is_admin = false
613
+ # end
614
+ #
615
+ # # Creating an Array of new objects using a block, where the block is executed for each object:
616
+ # User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}]) do |u|
617
+ # u.is_admin = false
618
+ # end
619
+ def create ( attributes = nil , &block )
609
620
if attributes . is_a? ( Array )
610
- attributes . collect { |attr | create ( attr ) }
621
+ attributes . collect { |attr | create ( attr , & block ) }
611
622
else
612
623
object = new ( attributes )
624
+ yield ( object ) if block_given?
613
625
object . save
614
626
object
615
627
end
Original file line number Diff line number Diff line change @@ -873,11 +873,12 @@ def validates_numericality_of(*attr_names)
873
873
874
874
# Creates an object just like Base.create but calls save! instead of save
875
875
# so an exception is raised if the record is invalid.
876
- def create! ( attributes = nil )
876
+ def create! ( attributes = nil , & block )
877
877
if attributes . is_a? ( Array )
878
- attributes . collect { |attr | create! ( attr ) }
878
+ attributes . collect { |attr | create! ( attr , & block ) }
879
879
else
880
880
object = new ( attributes )
881
+ yield ( object ) if block_given?
881
882
object . save!
882
883
object
883
884
end
Original file line number Diff line number Diff line change @@ -251,6 +251,27 @@ def test_create_through_factory
251
251
topic = Topic . create ( "title" => "New Topic" )
252
252
topicReloaded = Topic . find ( topic . id )
253
253
assert_equal ( topic , topicReloaded )
254
+ end
255
+
256
+ def test_create_through_factory_with_block
257
+ topic = Topic . create ( "title" => "New Topic" ) do |t |
258
+ t . author_name = "David"
259
+ end
260
+ topicReloaded = Topic . find ( topic . id )
261
+ assert_equal ( "New Topic" , topic . title )
262
+ assert_equal ( "David" , topic . author_name )
263
+ end
264
+
265
+ def test_create_many_through_factory_with_block
266
+ topics = Topic . create ( [ { "title" => "first" } , { "title" => "second" } ] ) do |t |
267
+ t . author_name = "David"
268
+ end
269
+ assert_equal 2 , topics . size
270
+ topic1 , topic2 = Topic . find ( topics [ 0 ] . id ) , Topic . find ( topics [ 1 ] . id )
271
+ assert_equal "first" , topic1 . title
272
+ assert_equal "David" , topic1 . author_name
273
+ assert_equal "second" , topic2 . title
274
+ assert_equal "David" , topic2 . author_name
254
275
end
255
276
256
277
def test_update
Original file line number Diff line number Diff line change @@ -133,6 +133,22 @@ def test_exception_on_create_bang_many
133
133
Reply . create! ( [ { "title" => "OK" } , { "title" => "Wrong Create" } ] )
134
134
end
135
135
end
136
+
137
+ def test_exception_on_create_bang_with_block
138
+ assert_raises ( ActiveRecord ::RecordInvalid ) do
139
+ Reply . create! ( { "title" => "OK" } ) do |r |
140
+ r . content = nil
141
+ end
142
+ end
143
+ end
144
+
145
+ def test_exception_on_create_bang_many_with_block
146
+ assert_raises ( ActiveRecord ::RecordInvalid ) do
147
+ Reply . create! ( [ { "title" => "OK" } , { "title" => "Wrong Create" } ] ) do |r |
148
+ r . content = nil
149
+ end
150
+ end
151
+ end
136
152
137
153
def test_scoped_create_without_attributes
138
154
Reply . with_scope ( :create => { } ) do
You can’t perform that action at this time.
0 commit comments