From 628838ed9349252eccd86b531959c111e285893e Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Mon, 2 Oct 2006 22:14:15 +0000 Subject: Make #save behavior mimic AR::Base#save (true on success, false on failure) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5220 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activeresource/CHANGELOG | 2 ++ activeresource/lib/active_resource/base.rb | 7 ++++--- activeresource/lib/active_resource/validations.rb | 5 +++-- activeresource/test/base_errors_test.rb | 7 ++----- activeresource/test/base_test.rb | 4 ++-- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index e24462a750..94481ae9ef 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Make #save behavior mimic AR::Base#save (true on success, false on failure). [Jamis Buck] + * Add Basic HTTP Authentication to ActiveResource (closes #6305). [jonathan] * Extracted #id_from_response as an entry point for customizing how a created resource gets its own ID. diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index cb520eef5c..656698fb25 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -169,12 +169,13 @@ module ActiveResource def update connection.put(self.class.element_path(id, prefix_options), to_xml) + true end def create - returning connection.post(self.class.collection_path(prefix_options), to_xml) do |resp| - self.id = id_from_response(resp) - end + resp = connection.post(self.class.collection_path(prefix_options), to_xml) + self.id = id_from_response(resp) + true end # takes a response from a typical create post and pulls the ID out diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index f1995a6070..3011ba3295 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -109,8 +109,9 @@ module ActiveResource def save_with_validation save_without_validation - rescue ResourceInvalid - errors.from_xml($!.response.body) + rescue ResourceInvalid => error + errors.from_xml(error.response.body) + return false end def valid? diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index 1aa677b1ba..c6b840ebe1 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -6,11 +6,8 @@ class BaseErrorsTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to do |mock| mock.post "/people.xml", {}, "Age can't be blankName can't be blankName must start with a letterPerson quota full for today.", 400 end - @exception = nil - @person = Person.new(:name => '', :age => '') - @person.save - rescue ActiveResource::ResourceInvalid - @exception = $! + @person = Person.new(:name => '', :age => '') + assert_equal @person.save, false end def test_should_mark_as_invalid diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index f3fd3b43e2..dd017b71fa 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -109,7 +109,7 @@ class BaseTest < Test::Unit::TestCase def test_create rick = Person.new - rick.save + assert_equal true, rick.save assert_equal '5', rick.id end @@ -133,7 +133,7 @@ class BaseTest < Test::Unit::TestCase matz.name = "David" assert_kind_of Person, matz assert_equal "David", matz.name - matz.save + assert_equal true, matz.save end def test_update_with_custom_prefix -- cgit v1.2.3