aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/errors.rb10
-rw-r--r--activemodel/test/cases/errors_test.rb6
2 files changed, 15 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index ac19b4625e..a9af426fb1 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -318,7 +318,7 @@ module ActiveModel
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 @@ module ActiveModel
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.
#
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb
index 1e4a6ddc00..89c3d1b7f4 100644
--- a/activemodel/test/cases/errors_test.rb
+++ b/activemodel/test/cases/errors_test.rb
@@ -576,6 +576,12 @@ class ErrorsTest < ActiveModel::TestCase
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)