diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-09 00:11:12 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-09 00:11:12 +0000 |
commit | 0a84624bd78643f2161d162f68669de03fe2bac8 (patch) | |
tree | 5f4f21f814a6be2db296fbad2fe7658d62f7c4f3 | |
parent | 2c6747f858481efa1bc3612e955861c13dc1c95b (diff) | |
download | rails-0a84624bd78643f2161d162f68669de03fe2bac8.tar.gz rails-0a84624bd78643f2161d162f68669de03fe2bac8.tar.bz2 rails-0a84624bd78643f2161d162f68669de03fe2bac8.zip |
Optimistic locking: raise ActiveResource::ResourceConflict on 409 Conflict response.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5078 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activeresource/CHANGELOG | 9 | ||||
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 12 | ||||
-rw-r--r-- | activeresource/test/base_test.rb | 8 | ||||
-rw-r--r-- | activeresource/test/connection_test.rb | 3 |
4 files changed, 26 insertions, 6 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index ef3f3f6a17..c81f7daacb 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,14 @@ *SVN* +* Optimistic locking: raise ActiveResource::ResourceConflict on 409 Conflict response. [Jeremy Kemper] + + # Example controller action + def update + @person.save! + rescue ActiveRecord::StaleObjectError + render :xml => @person.reload.to_xml, :status => '409 Conflict' + end + * Basic validation support [Rick Olson] Parses the xml response of ActiveRecord::Errors#to_xml with a similar interface to ActiveRecord::Errors. diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 132e861292..21573a34cb 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -17,14 +17,12 @@ module ActiveResource end end - class ClientError < ConnectionError - end + class ClientError < ConnectionError; end # 4xx Client Error + class ResourceNotFound < ClientError; end # 404 Not Found + class ResourceConflict < ClientError; end # 409 Conflict - class ServerError < ConnectionError - end + class ServerError < ConnectionError; end # 5xx Server Error - class ResourceNotFound < ClientError - end class Connection attr_accessor :site @@ -73,6 +71,8 @@ module ActiveResource raise(ResourceNotFound.new(response)) when 400 raise(ResourceInvalid.new(response)) + when 409 + raise(ResourceConflict.new(response)) when 401...500 raise(ClientError.new(response)) when 500...600 diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 97f2ea3d01..d8c654350b 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -133,6 +133,14 @@ class BaseTest < Test::Unit::TestCase addy.save end + def test_update_conflict + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/people/2.xml", @david + mock.put "/people/2", nil, 409 + end + assert_raises(ActiveResource::ResourceConflict) { Person.find(2).save } + end + def test_destroy assert Person.find(1).destroy ActiveResource::HttpMock.respond_to do |mock| diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index d72c1c612d..0744e327e6 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -20,6 +20,9 @@ class ConnectionTest < Test::Unit::TestCase # 400 is a validation error assert_response_raises ActiveResource::ResourceInvalid, 400 + # 409 is an optimistic locking error + assert_response_raises ActiveResource::ResourceConflict, 409 + # 4xx are client errors. [401, 499].each do |code| assert_response_raises ActiveResource::ClientError, code |