aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-10-09 01:52:24 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-10-09 01:52:24 +0000
commit55aab5b8acb72967228dc38f6432e0f126a94d77 (patch)
tree6e0e028919bf4ad819d9c5d48f5b664704a282d3 /activerecord
parent9fd88d793998ea3a15a916ce7175f1bb2cd264fe (diff)
downloadrails-55aab5b8acb72967228dc38f6432e0f126a94d77.tar.gz
rails-55aab5b8acb72967228dc38f6432e0f126a94d77.tar.bz2
rails-55aab5b8acb72967228dc38f6432e0f126a94d77.zip
Added update_attributes! which uses save! to raise an exception if a validation error prevents saving (closes #6192) [jonathan]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5256 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb6
-rwxr-xr-xactiverecord/test/base_test.rb39
3 files changed, 46 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 75cc342f90..6ebfc9e8a4 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added update_attributes! which uses save! to raise an exception if a validation error prevents saving #6192 [jonathan]
+
* Deprecated add_on_boundary_breaking (use validates_length_of instead) #6292 [BobSilva]
* The has_many create method works with polymorphic associations. #6361 [Dan Peterson]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 5d7c30caf9..1cdebd93d9 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1576,6 +1576,12 @@ module ActiveRecord #:nodoc:
self.attributes = attributes
save
end
+
+ # Updates an object just like Base.update_attributes but calls save! instead of save so an exception is raised if the record is invalid.
+ def update_attributes!(attributes)
+ self.attributes = attributes
+ save!
+ end
# Initializes the +attribute+ to zero if nil and adds one. Only makes sense for number-based attributes. Returns self.
def increment(attribute)
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 9a67426ba2..e5eabc57ab 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -149,8 +149,11 @@ class BasicsTest < Test::Unit::TestCase
def test_save!
topic = Topic.new(:title => "New Topic")
assert topic.save!
- end
+ reply = Reply.new
+ assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
+ end
+
def test_hashes_not_mangled
new_topic = { :title => "New Topic" }
new_topic_values = { :title => "AnotherTopic" }
@@ -646,6 +649,40 @@ class BasicsTest < Test::Unit::TestCase
assert !Topic.find(1).approved?
end
+ def test_update_attributes
+ topic = Topic.find(1)
+ assert !topic.approved?
+ assert_equal "The First Topic", topic.title
+
+ topic.update_attributes("approved" => true, "title" => "The First Topic Updated")
+ topic.reload
+ assert topic.approved?
+ assert_equal "The First Topic Updated", topic.title
+
+ topic.update_attributes(:approved => false, :title => "The First Topic")
+ topic.reload
+ assert !topic.approved?
+ assert_equal "The First Topic", topic.title
+ end
+
+ def test_update_attributes!
+ reply = Reply.find(2)
+ assert_equal "The Second Topic's of the day", reply.title
+ assert_equal "Have a nice day", reply.content
+
+ reply.update_attributes!("title" => "The Second Topic's of the day updated", "content" => "Have a nice evening")
+ reply.reload
+ assert_equal "The Second Topic's of the day updated", reply.title
+ assert_equal "Have a nice evening", reply.content
+
+ reply.update_attributes!(:title => "The Second Topic's of the day", :content => "Have a nice day")
+ reply.reload
+ assert_equal "The Second Topic's of the day", reply.title
+ assert_equal "Have a nice day", reply.content
+
+ assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") }
+ end
+
def test_mass_assignment_protection
firm = Firm.new
firm.attributes = { "name" => "Next Angle", "rating" => 5 }