aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb16
-rwxr-xr-xactiverecord/lib/active_record/validations.rb5
-rwxr-xr-xactiverecord/test/cases/base_test.rb21
-rwxr-xr-xactiverecord/test/cases/validations_test.rb16
5 files changed, 56 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 154b282130..f72d8aceaa 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39]
+
* Fixed that pessimistic locking you reference the quoted table name (Josh Susser) [#67]
* Fixed that change_column should be able to use :null => true on a field that formerly had false [Nate Wiger] [#26]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 63306644fb..4f4ba83a47 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -603,13 +603,25 @@ module ActiveRecord #:nodoc:
# ==== Examples
# # Create a single new object
# User.create(:first_name => 'Jamie')
+ #
# # Create an Array of new objects
# User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}])
- def create(attributes = nil)
+ #
+ # # Create a single object and pass it into a block to set other attributes.
+ # User.create(:first_name => 'Jamie') do |u|
+ # u.is_admin = false
+ # end
+ #
+ # # Creating an Array of new objects using a block, where the block is executed for each object:
+ # User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}]) do |u|
+ # u.is_admin = false
+ # end
+ def create(attributes = nil, &block)
if attributes.is_a?(Array)
- attributes.collect { |attr| create(attr) }
+ attributes.collect { |attr| create(attr, &block) }
else
object = new(attributes)
+ yield(object) if block_given?
object.save
object
end
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 1d12ea8ad7..5ca51c014c 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -873,11 +873,12 @@ module ActiveRecord
# Creates an object just like Base.create but calls save! instead of save
# so an exception is raised if the record is invalid.
- def create!(attributes = nil)
+ def create!(attributes = nil, &block)
if attributes.is_a?(Array)
- attributes.collect { |attr| create!(attr) }
+ attributes.collect { |attr| create!(attr, &block) }
else
object = new(attributes)
+ yield(object) if block_given?
object.save!
object
end
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 93719c710a..e07ec50c46 100755
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -251,6 +251,27 @@ class BasicsTest < ActiveRecord::TestCase
topic = Topic.create("title" => "New Topic")
topicReloaded = Topic.find(topic.id)
assert_equal(topic, topicReloaded)
+ end
+
+ def test_create_through_factory_with_block
+ topic = Topic.create("title" => "New Topic") do |t|
+ t.author_name = "David"
+ end
+ topicReloaded = Topic.find(topic.id)
+ assert_equal("New Topic", topic.title)
+ assert_equal("David", topic.author_name)
+ end
+
+ def test_create_many_through_factory_with_block
+ topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t|
+ t.author_name = "David"
+ end
+ assert_equal 2, topics.size
+ topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id)
+ assert_equal "first", topic1.title
+ assert_equal "David", topic1.author_name
+ assert_equal "second", topic2.title
+ assert_equal "David", topic2.author_name
end
def test_update
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index ca36ad3581..e3ca8660ac 100755
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -133,6 +133,22 @@ class ValidationsTest < ActiveRecord::TestCase
Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
end
end
+
+ def test_exception_on_create_bang_with_block
+ assert_raises(ActiveRecord::RecordInvalid) do
+ Reply.create!({ "title" => "OK" }) do |r|
+ r.content = nil
+ end
+ end
+ end
+
+ def test_exception_on_create_bang_many_with_block
+ assert_raises(ActiveRecord::RecordInvalid) do
+ Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
+ r.content = nil
+ end
+ end
+ end
def test_scoped_create_without_attributes
Reply.with_scope(:create => {}) do