Skip to content

Commit

Permalink
Deprecated AM::Errors#to_h:
Browse files Browse the repository at this point in the history
- In ef4d321 I made a change to
  pass `AM::Error` object in case the arity of the block passed to
  `each` accepted less than 2 arguments.

  This is causing one issue for `to_h` as it expects the argument
  passed to the block to be an Array (and were are passing it an
  instance of `AM::Error`).

  There is no real reason to use `to_h` anymore since `to_hash` exists

  Deprecating `to_h` inf favor of `to_hash`

Co-Authored-By: Rafael França <[email protected]>
  • Loading branch information
Edouard-chin and rafaelfranca committed Jul 23, 2019
1 parent 9ad8068 commit 35ac22d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activemodel/lib/active_model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ def to_hash(full_messages = false)
hash
end

def to_h
deprecation_rename_warning(:to_h, :to_hash)

to_hash
end

def messages
DeprecationHandlingMessageHash.new(self)
end
Expand Down
10 changes: 10 additions & 0 deletions activemodel/test/cases/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ def test_no_key
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")
Expand Down

0 comments on commit 35ac22d

Please sign in to comment.