public this repo is viewable by everyone
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added block-setting of attributes for Base.create like Base.new already 
has (Adam Meehan) [#39 state:resolved]
dhh (author)
11 days ago
commit  dd120ede53eaf71dee76894998a81626b7a689fc
tree    f9bcc98eba07ebfdb662489321b4e1d184020f57
parent  c83f75812ef89aea1b8d138aebec25de8057f156
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39]
0
+
0
 * Fixed that pessimistic locking you reference the quoted table name (Josh Susser) [#67]
0
 
0
 * Fixed that change_column should be able to use :null => true on a field that formerly had false [Nate Wiger] [#26]
...
603
604
605
 
606
607
608
 
 
 
 
 
 
 
 
 
 
 
609
610
 
611
612
 
613
614
615
...
603
604
605
606
607
608
 
609
610
611
612
613
614
615
616
617
618
619
620
 
621
622
623
624
625
626
627
0
@@ -603,13 +603,25 @@ module ActiveRecord #:nodoc:
0
       # ==== Examples
0
       # # Create a single new object
0
       # User.create(:first_name => 'Jamie')
0
+ #
0
       # # Create an Array of new objects
0
       # User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}])
0
- def create(attributes = nil)
0
+ #
0
+ # # Create a single object and pass it into a block to set other attributes.
0
+ # User.create(:first_name => 'Jamie') do |u|
0
+ # u.is_admin = false
0
+ # end
0
+ #
0
+ # # Creating an Array of new objects using a block, where the block is executed for each object:
0
+ # User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}]) do |u|
0
+ # u.is_admin = false
0
+ # end
0
+ def create(attributes = nil, &block)
0
         if attributes.is_a?(Array)
0
- attributes.collect { |attr| create(attr) }
0
+ attributes.collect { |attr| create(attr, &block) }
0
         else
0
           object = new(attributes)
0
+ yield(object) if block_given?
0
           object.save
0
           object
0
         end
...
873
874
875
876
 
877
878
 
879
880
 
881
882
883
...
873
874
875
 
876
877
 
878
879
880
881
882
883
884
0
@@ -873,11 +873,12 @@ module ActiveRecord
0
 
0
       # Creates an object just like Base.create but calls save! instead of save
0
       # so an exception is raised if the record is invalid.
0
- def create!(attributes = nil)
0
+ def create!(attributes = nil, &block)
0
         if attributes.is_a?(Array)
0
- attributes.collect { |attr| create!(attr) }
0
+ attributes.collect { |attr| create!(attr, &block) }
0
         else
0
           object = new(attributes)
0
+ yield(object) if block_given?
0
           object.save!
0
           object
0
         end
...
251
252
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
255
256
...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
0
@@ -251,6 +251,27 @@ class BasicsTest < ActiveRecord::TestCase
0
     topic = Topic.create("title" => "New Topic")
0
     topicReloaded = Topic.find(topic.id)
0
     assert_equal(topic, topicReloaded)
0
+ end
0
+
0
+ def test_create_through_factory_with_block
0
+ topic = Topic.create("title" => "New Topic") do |t|
0
+ t.author_name = "David"
0
+ end
0
+ topicReloaded = Topic.find(topic.id)
0
+ assert_equal("New Topic", topic.title)
0
+ assert_equal("David", topic.author_name)
0
+ end
0
+
0
+ def test_create_many_through_factory_with_block
0
+ topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t|
0
+ t.author_name = "David"
0
+ end
0
+ assert_equal 2, topics.size
0
+ topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id)
0
+ assert_equal "first", topic1.title
0
+ assert_equal "David", topic1.author_name
0
+ assert_equal "second", topic2.title
0
+ assert_equal "David", topic2.author_name
0
   end
0
 
0
   def test_update
...
133
134
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
137
138
...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
0
@@ -133,6 +133,22 @@ class ValidationsTest < ActiveRecord::TestCase
0
       Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
0
     end
0
   end
0
+
0
+ def test_exception_on_create_bang_with_block
0
+ assert_raises(ActiveRecord::RecordInvalid) do
0
+ Reply.create!({ "title" => "OK" }) do |r|
0
+ r.content = nil
0
+ end
0
+ end
0
+ end
0
+
0
+ def test_exception_on_create_bang_many_with_block
0
+ assert_raises(ActiveRecord::RecordInvalid) do
0
+ Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
0
+ r.content = nil
0
+ end
0
+ end
0
+ end
0
 
0
   def test_scoped_create_without_attributes
0
     Reply.with_scope(:create => {}) do

Comments

    No one has commented yet.