Set default array to details

maintaining behavior errors.details[:foo].any?
This commit is contained in:
lulalala 2019-03-17 15:57:00 +08:00
parent f7f42a2d0e
commit aaa0c32797
2 changed files with 15 additions and 1 deletions

@ -318,7 +318,7 @@ def details
group_by_attribute.each do |attribute, errors|
hash[attribute] = errors.map(&:detail)
end
hash
DeprecationHandlingDetailsHash.new(hash)
end
def group_by_attribute
@ -672,6 +672,14 @@ def <<(message)
end
end
class DeprecationHandlingDetailsHash < SimpleDelegator
def initialize(details)
details.default = []
details.freeze
super(details)
end
end
# Raised when a validation cannot be corrected by end users and are considered
# exceptional.
#

@ -576,6 +576,12 @@ def test_no_key
assert_equal [:name], person.errors.details.keys
end
test "details returns empty array when accessed with non-existent attribute" do
errors = ActiveModel::Errors.new(Person.new)
assert_equal [], errors.details[:foo]
end
test "copy errors" do
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :invalid)