diff options
author | Matt Jones <al2o3cr@gmail.com> | 2012-02-04 13:41:12 -0600 |
---|---|---|
committer | Matt Jones <al2o3cr@gmail.com> | 2012-02-04 13:41:12 -0600 |
commit | 7c307c2da3e0e01131a892b30c040832edbfacb5 (patch) | |
tree | 1df04792144038729266f5ca7446babe5afcac92 /activeresource | |
parent | d709b124d1815340f4a2229b05362ea37298288f (diff) | |
parent | b09b2a8401c18d1efff21b3919ac280470a6eb8b (diff) | |
download | rails-7c307c2da3e0e01131a892b30c040832edbfacb5.tar.gz rails-7c307c2da3e0e01131a892b30c040832edbfacb5.tar.bz2 rails-7c307c2da3e0e01131a892b30c040832edbfacb5.zip |
Merge remote-tracking branch 'odorcicd/fix_activeresource_json_errors' into compatible_json_errors
Diffstat (limited to 'activeresource')
-rw-r--r-- | activeresource/lib/active_resource/validations.rb | 18 | ||||
-rw-r--r-- | activeresource/test/cases/base_errors_test.rb | 4 |
2 files changed, 18 insertions, 4 deletions
diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index a63f02cb57..c11908eb86 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -25,10 +25,24 @@ module ActiveResource end end + def from_hash(messages, save_cache = false) + clear unless save_cache + + messages.each do |(key,errors)| + errors.each do |error| + if @base.attributes.keys.include?(key) + add key, error + else + self[:base] << "#{key.humanize} #{error}" + end + end + end + end + # Grabs errors from a json response. def from_json(json, save_cache = false) - array = Array.wrap(ActiveSupport::JSON.decode(json)['errors']) rescue [] - from_array array, save_cache + hash = ActiveSupport::JSON.decode(json)['errors'] || {} rescue {} + from_hash hash, save_cache end # Grabs errors from an XML response. diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb index aacbeeb83c..98fef5fa73 100644 --- a/activeresource/test/cases/base_errors_test.rb +++ b/activeresource/test/cases/base_errors_test.rb @@ -5,7 +5,7 @@ class BaseErrorsTest < ActiveSupport::TestCase def setup ActiveResource::HttpMock.respond_to do |mock| mock.post "/people.xml", {}, %q(<?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>), 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'} + mock.post "/people.json", {}, %q({"errors":{"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}}), 422, {'Content-Type' => 'application/json; charset=utf-8'} end end @@ -83,7 +83,7 @@ class BaseErrorsTest < ActiveSupport::TestCase def test_should_mark_as_invalid_when_content_type_is_unavailable_in_response_header ActiveResource::HttpMock.respond_to do |mock| mock.post "/people.xml", {}, %q(<?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>), 422, {} - 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, {} + mock.post "/people.json", {}, %q({"errors":{"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}}), 422, {} end [ :json, :xml ].each do |format| |