aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/errors.rb6
-rw-r--r--activemodel/test/cases/errors_test.rb10
2 files changed, 16 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 5628a59845..e7405fb586 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -303,6 +303,12 @@ module ActiveModel
hash
end
+ def to_h
+ deprecation_rename_warning(:to_h, :to_hash)
+
+ to_hash
+ end
+
def messages
DeprecationHandlingMessageHash.new(self)
end
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb
index 60b20ab59f..79ccfe0c13 100644
--- a/activemodel/test/cases/errors_test.rb
+++ b/activemodel/test/cases/errors_test.rb
@@ -474,6 +474,16 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.to_a
end
+ test "to_h is deprecated" do
+ person = Person.new
+ person.errors.add(:name, "cannot be blank")
+
+ expected_deprecation = "ActiveModel::Errors#to_h is deprecated. Please call #to_hash instead."
+ assert_deprecated(expected_deprecation) do
+ assert_equal({ name: ["cannot be blank"] }, person.errors.to_h)
+ end
+ end
+
test "to_hash returns the error messages hash" do
person = Person.new
person.errors.add(:name, "cannot be blank")