Skip to content

Commit dd120ed

Browse files
author
David Heinemeier Hansson
committed
Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39 state:resolved]
1 parent c83f758 commit dd120ed

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

activerecord/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*SVN*
22

3+
* Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39]
4+
35
* Fixed that pessimistic locking you reference the quoted table name (Josh Susser) [#67]
46

57
* Fixed that change_column should be able to use :null => true on a field that formerly had false [Nate Wiger] [#26]

activerecord/lib/active_record/base.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,13 +603,25 @@ def exists?(id_or_conditions)
603603
# ==== Examples
604604
# # Create a single new object
605605
# User.create(:first_name => 'Jamie')
606+
#
606607
# # Create an Array of new objects
607608
# 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)
609620
if attributes.is_a?(Array)
610-
attributes.collect { |attr| create(attr) }
621+
attributes.collect { |attr| create(attr, &block) }
611622
else
612623
object = new(attributes)
624+
yield(object) if block_given?
613625
object.save
614626
object
615627
end

activerecord/lib/active_record/validations.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,11 +873,12 @@ def validates_numericality_of(*attr_names)
873873

874874
# Creates an object just like Base.create but calls save! instead of save
875875
# so an exception is raised if the record is invalid.
876-
def create!(attributes = nil)
876+
def create!(attributes = nil, &block)
877877
if attributes.is_a?(Array)
878-
attributes.collect { |attr| create!(attr) }
878+
attributes.collect { |attr| create!(attr, &block) }
879879
else
880880
object = new(attributes)
881+
yield(object) if block_given?
881882
object.save!
882883
object
883884
end

activerecord/test/cases/base_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,27 @@ def test_create_through_factory
251251
topic = Topic.create("title" => "New Topic")
252252
topicReloaded = Topic.find(topic.id)
253253
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
254275
end
255276

256277
def test_update

activerecord/test/cases/validations_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ def test_exception_on_create_bang_many
133133
Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
134134
end
135135
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
136152

137153
def test_scoped_create_without_attributes
138154
Reply.with_scope(:create => {}) do

0 commit comments

Comments
 (0)