diff options
author | taryn <teast@globalpersonals.co.uk> | 2009-08-19 12:25:05 +0100 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-08-19 09:05:02 -0500 |
commit | 328ba3b333777bbc1269cbe0e9f590c845006c9d (patch) | |
tree | e3bfecf2acd9c5270ceb0bcf5f59c28f685e2f64 /activeresource/lib/active_resource | |
parent | 4dc05bc8a9824b9404cebecaba28f9f248f9995e (diff) | |
download | rails-328ba3b333777bbc1269cbe0e9f590c845006c9d.tar.gz rails-328ba3b333777bbc1269cbe0e9f590c845006c9d.tar.bz2 rails-328ba3b333777bbc1269cbe0e9f590c845006c9d.zip |
Added save! which raises ResourceInvalid unless valid?
Similar to Active Record - it will raise ActiveResouce::ResourceInvalid if
the resource is not valid (ie if <tt>valid?</tt> returns false)
However - does not raise ActiveResource::ResourceNotFound if the callbacks
fail (callbacks have not yet been implemented) - it will just try to save
and raise if the callbacks all fail.
This is not ideal behaviour - but will do until we decide to change the
behaviour of save_with_validations to actually raise (rather than catch) the
ResourceInvalid exception.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activeresource/lib/active_resource')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 17 |
1 files changed, 17 insertions, 0 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. # |