提交 628838ed 编写于 作者: J Jamis Buck

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
上级 48d4d43f
*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.
......
......@@ -169,12 +169,13 @@ def connection(refresh = false)
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
......
......@@ -109,8 +109,9 @@ def self.included(base) # :nodoc:
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?
......
......@@ -6,11 +6,8 @@ def setup
ActiveResource::HttpMock.respond_to do |mock|
mock.post "/people.xml", {}, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 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
......
......@@ -109,7 +109,7 @@ def test_find_by_id_not_found
def test_create
rick = Person.new
rick.save
assert_equal true, rick.save
assert_equal '5', rick.id
end
......@@ -133,7 +133,7 @@ def test_update
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册