Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/active_resource/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ class Errors < ActiveModel::Errors
# The second parameter directs the errors cache to be cleared (default)
# or not (by passing true).
def from_array(messages, save_cache = false)
clear unless save_cache
errors = Hash.new { |hash, attr_name| hash[attr_name] = [] }
humanized_attributes = Hash[@base.known_attributes.map { |attr_name| [ attr_name.humanize, attr_name ] }]
messages.each do |message|
messages.each_with_object(errors) do |message, hash|
attr_message = humanized_attributes.keys.sort_by { |a| -a.length }.detect do |attr_name|
if message[0, attr_name.size + 1] == "#{attr_name} "
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
hash[humanized_attributes[attr_name]] << message[(attr_name.size + 1)..-1]
end
end
add(:base, message) if attr_message.nil?
hash["base"] << message if attr_message.nil?
end

from_hash errors, save_cache
end

# Grabs errors from a hash of attribute => array of errors elements
Expand Down
58 changes: 58 additions & 0 deletions test/cases/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,61 @@ def new_project(opts = {})
Project.new(VALID_PROJECT_HASH.merge(opts))
end
end

class ErrorsTest < ActiveSupport::TestCase
def test_from_xml_with_multiple_errors
errors = Project.new.errors

errors.from_xml %q(<?xml version="1.0" encoding="UTF-8"?><errors><error>Name can't be blank</error><error>Email can't be blank</error></errors>)

assert_equal [ "can't be blank" ], errors[:name]
assert_equal [ "can't be blank" ], errors[:email]
end

def test_from_xml_with_one_error
errors = Project.new.errors

errors.from_xml %q(<?xml version="1.0" encoding="UTF-8"?><errors><error>Name can't be blank</error></errors>)

assert_equal [ "can't be blank" ], errors[:name]
end

def test_from_json
errors = Project.new.errors

errors.from_json %q({"errors":{"name":["can't be blank"],"email":["can't be blank"]}})

assert_equal [ "can't be blank" ], errors[:name]
assert_equal [ "can't be blank" ], errors[:email]
end

def test_from_hash
errors = Project.new.errors

errors.from_hash(
"base" => [ "has an error" ],
"unknown" => [ "is invalid" ],
"name" => [ "can't be blank" ],
"email" => [ "can't be blank" ]
)

assert_equal [ "has an error", "Unknown is invalid" ], errors[:base]
assert_equal [ "can't be blank" ], errors[:name]
assert_equal [ "can't be blank" ], errors[:email]
end

def test_from_array
errors = Project.new.errors

errors.from_array [
"Unknown is invalid",
"Base has an error",
"Name can't be blank",
"Email can't be blank"
]

assert_equal [ "Unknown is invalid", "Base has an error" ], errors[:base]
assert_equal [ "can't be blank" ], errors[:name]
assert_equal [ "can't be blank" ], errors[:email]
end
end