Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Rails Lighthouse ticket #6713
From db27581f30e07ab3ccce8609a9fc31e174ab2fd5 Mon Sep 17 00:00:00 2001
From: Brian Durand <brian@embellishedvisions.com>
Date: Mon, 18 Apr 2011 10:22:53 -0500
Subject: [PATCH] Disambiguate attributes with similar names when deserializing errors from an array. [#6713 state:resolved]
---
activeresource/lib/active_resource/validations.rb | 2 +-
activeresource/test/cases/base_errors_test.rb | 13 +++++++++++++
2 files changed, 14 insertions(+), 1 deletions(-)
diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb
index a373e53..f8d1959 100644
--- a/activeresource/lib/active_resource/validations.rb
+++ b/activeresource/lib/active_resource/validations.rb
@@ -15,7 +15,7 @@ module ActiveResource
clear unless save_cache
humanized_attributes = Hash[@base.attributes.keys.map { |attr_name| [attr_name.humanize, attr_name] }]
messages.each do |message|
- attr_message = humanized_attributes.keys.detect do |attr_name|
+ attr_message = humanized_attributes.keys.sort{|a,b| b.to_s.size <=> a.to_s.size}.detect do |attr_name|
if message[0, attr_name.size + 1] == "#{attr_name} "
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
end
diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb
index 5063916..c9a1c99 100644
--- a/activeresource/test/cases/base_errors_test.rb
+++ b/activeresource/test/cases/base_errors_test.rb
@@ -48,6 +48,19 @@ class BaseErrorsTest < Test::Unit::TestCase
end
end
+ def test_should_disambiguate_attributes_when_parsing_errors
+ person = Person.new(:name => "name", :name_length => 4, :name_source => "public")
+ name_errors = [
+ "Name has error!",
+ "Name length is wrong",
+ "Name source is also wrong"
+ ]
+ person.errors.from_array(name_errors)
+ assert_equal ["has error!"], person.errors[:name]
+ assert_equal ["is wrong"], person.errors[:name_length]
+ assert_equal ["is also wrong"], person.errors[:name_source]
+ end
+
def test_should_iterate_over_errors
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
--
1.7.3.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.