1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove deprecated enumeration of ActiveModel::Errors instances as a Hash

This commit is contained in:
Rafael Mendonça França 2021-11-15 23:53:00 +00:00
parent 10bd5e59c3
commit 05b18d2694
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948
4 changed files with 8 additions and 50 deletions

View file

@ -1,3 +1,7 @@
* Remove deprecated enumeration of `ActiveModel::Errors` instances as a Hash.
*Rafael Mendonça França*
* Clear secure password cache if password is set to `nil`
Before:
@ -32,7 +36,7 @@
* Fix dirty check for Float::NaN and BigDecimal::NaN.
Float::NaN and BigDecimal::NaN in Ruby are [special values](https://bugs.ruby-lang.org/issues/1720)
Float::NaN and BigDecimal::NaN in Ruby are [special values](https://bugs.ruby-lang.org/issues/1720)
and can't be compared with `==`.
*Marcelo Lauxen*

View file

@ -215,46 +215,8 @@ module ActiveModel
# # Will yield <#ActiveModel::Error attribute=name, type=too_short,
# options={:count=>3}>
# end
#
# To be backward compatible with past deprecated hash-like behavior,
# when block accepts two parameters instead of one, it
# iterates through each error key, value pair in the error messages hash.
# Yields the attribute and the error for that attribute. If the attribute
# has more than one error message, yields once for each error message.
#
# person.errors.add(:name, :blank, message: "can't be blank")
# person.errors.each do |attribute, message|
# # Will yield :name and "can't be blank"
# end
#
# person.errors.add(:name, :not_specified, message: "must be specified")
# person.errors.each do |attribute, message|
# # Will yield :name and "can't be blank"
# # then yield :name and "must be specified"
# end
def each(&block)
if block.arity <= 1
@errors.each(&block)
else
ActiveSupport::Deprecation.warn(<<~MSG)
Enumerating ActiveModel::Errors as a hash has been deprecated.
In Rails 6.1, `errors` is an array of Error objects,
therefore it should be accessed by a block with a single block
parameter like this:
person.errors.each do |error|
attribute = error.attribute
message = error.message
end
You are passing a block expecting two parameters,
so the old hash behavior is simulated. As this is deprecated,
this will result in an ArgumentError in Rails 7.0.
MSG
@errors.
sort { |a, b| a.attribute <=> b.attribute }.
each { |error| yield error.attribute, error.message }
end
@errors.each(&block)
end
# Returns all message values.

View file

@ -49,16 +49,6 @@ class ValidationsTest < ActiveModel::TestCase
assert_equal 2, r.errors.count
end
def test_single_error_per_attr_iteration
r = Reply.new
r.valid?
errors = assert_deprecated { r.errors.collect { |attr, messages| [attr.to_s, messages] } }
assert_includes errors, ["title", "is Empty"]
assert_includes errors, ["content", "is Empty"]
end
def test_multiple_errors_per_attr_iteration_with_full_error_composition
r = Reply.new
r.title = ""

View file

@ -122,6 +122,8 @@ Please refer to the [Changelog][active-model] for detailed changes.
### Removals
* Remove deprecated enumeration of `ActiveModel::Errors` instances as a Hash.
### Deprecations
### Notable changes