From 81bd981eca981b089fa2a57bd841c1c44cec6955 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Sun, 16 May 2010 12:21:35 +0200 Subject: [PATCH] flush AR errors before trying to save [#4169 state:resolved] --- activeresource/lib/active_resource/validations.rb | 1 + activeresource/test/base_errors_test.rb | 38 +++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index f5aa24f..fa78948 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -256,6 +256,7 @@ module ActiveResource # Validate a resource and save (POST) it to the remote web service. def save_with_validation + errors.clear save_without_validation true rescue ResourceInvalid => error diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index 7183d7a..063b706 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -3,12 +3,7 @@ require "fixtures/person" class BaseErrorsTest < Test::Unit::TestCase def setup - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.xml", {}, %q(Age can't be blankName can't be blankName must start with a letterPerson quota full for today.), 422, {'Content-Type' => 'application/xml; charset=utf-8'} - mock.post "/people.json", {}, %q({"errors":["Age can't be blank","Name can't be blank","Name must start with a letter","Person quota full for today."]}), 422, {'Content-Type' => 'application/json; charset=utf-8'} - end - @person = Person.new(:name => '', :age => '') - assert_equal @person.save, false + set_mocks_to_invalid end def test_should_mark_as_invalid @@ -19,6 +14,23 @@ class BaseErrorsTest < Test::Unit::TestCase end end + def test_should_clear_errors_when_error_is_corrected + [ :json, :xml ].each do |format| + set_mocks_to_invalid + invalid_user_using_format(format) do + assert !@person.valid? + + @person.name = "Joe Shmoe" + @person.age = 56 + + set_mocks_to_valid + + assert @person.save + assert @person.valid? + end + end + end + def test_should_parse_xml_errors [ :json, :xml ].each do |format| invalid_user_using_format(format) do @@ -95,4 +107,18 @@ class BaseErrorsTest < Test::Unit::TestCase ensure Person.format = previous_format end + + def set_mocks_to_valid + ActiveResource::HttpMock.respond_to do |mock| + mock.post "/people.xml", {}, @person.to_xml + mock.post "/people.json", {}, { "person" => @person }.to_json + end + end + + def set_mocks_to_invalid + ActiveResource::HttpMock.respond_to do |mock| + mock.post "/people.xml", {}, %q(Age can't be blankName can't be blankName must start with a letterPerson quota full for today.), 422, {'Content-Type' => 'application/xml; charset=utf-8'} + mock.post "/people.json", {}, %q({"errors":["Age can't be blank","Name can't be blank","Name must start with a letter","Person quota full for today."]}), 422, {'Content-Type' => 'application/json; charset=utf-8'} + end + end end -- 1.6.6