diff options
author | lulalala <mark@goodlife.tw> | 2019-03-17 15:57:00 +0800 |
---|---|---|
committer | lulalala <mark@goodlife.tw> | 2019-03-31 22:59:13 +0800 |
commit | aaa0c3279745e3405bc3279924e41cb641e1af8e (patch) | |
tree | cfa563eee1a914dc7f85705e7142d1f236d1880c | |
parent | f7f42a2d0e7154f30d3f1f6cbedf14fc2c3f5b52 (diff) | |
download | rails-aaa0c3279745e3405bc3279924e41cb641e1af8e.tar.gz rails-aaa0c3279745e3405bc3279924e41cb641e1af8e.tar.bz2 rails-aaa0c3279745e3405bc3279924e41cb641e1af8e.zip |
Set default array to details
maintaining behavior errors.details[:foo].any?
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 10 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 6 |
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) |