aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activeresource/lib/active_resource/base.rb17
-rw-r--r--activeresource/test/cases/base_test.rb8
-rw-r--r--activeresource/test/cases/validations_test.rb8
3 files changed, 31 insertions, 2 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 293ba75ee0..f27febb7ef 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -893,6 +893,23 @@ module ActiveResource
def save
new? ? create : update
end
+
+ # Saves the resource.
+ #
+ # If the resource is new, it is created via +POST+, otherwise the
+ # existing resource is updated via +PUT+.
+ #
+ # With <tt>save!</tt> validations always run. If any of them fail
+ # ActiveResource::ResourceInvalid gets raised, and nothing is POSTed to
+ # the remote system.
+ # See ActiveResource::Validations for more information.
+ #
+ # There's a series of callbacks associated with <tt>save!</tt>. If any
+ # of the <tt>before_*</tt> callbacks return +false+ the action is
+ # cancelled and <tt>save!</tt> raises ActiveResource::ResourceInvalid.
+ def save!
+ save || raise(ResourceInvalid.new(self))
+ end
# Deletes the resource from the remote service.
#
diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb
index 969e3142f8..8c0217aad6 100644
--- a/activeresource/test/cases/base_test.rb
+++ b/activeresource/test/cases/base_test.rb
@@ -654,7 +654,13 @@ class BaseTest < Test::Unit::TestCase
def test_save
rick = Person.new
- assert_equal true, rick.save
+ assert rick.save
+ assert_equal '5', rick.id
+ end
+
+ def test_save!
+ rick = Person.new
+ assert rick.save!
assert_equal '5', rick.id
end
diff --git a/activeresource/test/cases/validations_test.rb b/activeresource/test/cases/validations_test.rb
index f5a43b1ac1..a8ab7d64e7 100644
--- a/activeresource/test/cases/validations_test.rb
+++ b/activeresource/test/cases/validations_test.rb
@@ -23,9 +23,15 @@ class ValidationsTest < ActiveModel::TestCase
assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
end
+
+ def test_fails_save!
+ p = new_project(:name => nil)
+ assert_raise(ActiveResource::ResourceInvalid) { p.save! }
+ end
+
def test_validate_callback
- # we have a callback ensuring the description is longer thn three letters
+ # we have a callback ensuring the description is longer than three letters
p = new_project(:description => 'a')
assert !p.valid?, "should not be a valid record when it fails a validation callback"
assert !p.save, "should not have saved an invalid record"