mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
2c6747f858
commit
0a84624bd7
4 changed files with 26 additions and 6 deletions
|
@ -1,5 +1,14 @@
|
||||||
*SVN*
|
*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]
|
* Basic validation support [Rick Olson]
|
||||||
|
|
||||||
Parses the xml response of ActiveRecord::Errors#to_xml with a similar interface to ActiveRecord::Errors.
|
Parses the xml response of ActiveRecord::Errors#to_xml with a similar interface to ActiveRecord::Errors.
|
||||||
|
|
|
@ -17,14 +17,12 @@ module ActiveResource
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ClientError < ConnectionError
|
class ClientError < ConnectionError; end # 4xx Client Error
|
||||||
end
|
class ResourceNotFound < ClientError; end # 404 Not Found
|
||||||
|
class ResourceConflict < ClientError; end # 409 Conflict
|
||||||
|
|
||||||
class ServerError < ConnectionError
|
class ServerError < ConnectionError; end # 5xx Server Error
|
||||||
end
|
|
||||||
|
|
||||||
class ResourceNotFound < ClientError
|
|
||||||
end
|
|
||||||
|
|
||||||
class Connection
|
class Connection
|
||||||
attr_accessor :site
|
attr_accessor :site
|
||||||
|
@ -73,6 +71,8 @@ module ActiveResource
|
||||||
raise(ResourceNotFound.new(response))
|
raise(ResourceNotFound.new(response))
|
||||||
when 400
|
when 400
|
||||||
raise(ResourceInvalid.new(response))
|
raise(ResourceInvalid.new(response))
|
||||||
|
when 409
|
||||||
|
raise(ResourceConflict.new(response))
|
||||||
when 401...500
|
when 401...500
|
||||||
raise(ClientError.new(response))
|
raise(ClientError.new(response))
|
||||||
when 500...600
|
when 500...600
|
||||||
|
|
|
@ -133,6 +133,14 @@ class BaseTest < Test::Unit::TestCase
|
||||||
addy.save
|
addy.save
|
||||||
end
|
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
|
def test_destroy
|
||||||
assert Person.find(1).destroy
|
assert Person.find(1).destroy
|
||||||
ActiveResource::HttpMock.respond_to do |mock|
|
ActiveResource::HttpMock.respond_to do |mock|
|
||||||
|
|
|
@ -20,6 +20,9 @@ class ConnectionTest < Test::Unit::TestCase
|
||||||
# 400 is a validation error
|
# 400 is a validation error
|
||||||
assert_response_raises ActiveResource::ResourceInvalid, 400
|
assert_response_raises ActiveResource::ResourceInvalid, 400
|
||||||
|
|
||||||
|
# 409 is an optimistic locking error
|
||||||
|
assert_response_raises ActiveResource::ResourceConflict, 409
|
||||||
|
|
||||||
# 4xx are client errors.
|
# 4xx are client errors.
|
||||||
[401, 499].each do |code|
|
[401, 499].each do |code|
|
||||||
assert_response_raises ActiveResource::ClientError, code
|
assert_response_raises ActiveResource::ClientError, code
|
||||||
|
|
Loading…
Reference in a new issue