Fix deprecation on AM::Errors when each is called indirectly:

- `AM::Errors#each` is implemented for the `Enumerator` module and
  get called indirectly by a bunch of method in the ruby land
  (map, first, select ...)

  These methods have a `-1` arity as they are written in C and they
  wrongly trigger a deprecation warning.

  This commit fixes that and correctectly return a `AM::Error` object
  when `each` is called with a negative arity.
This commit is contained in:
Edouard CHIN 2019-07-20 21:26:43 +02:00
parent 400b210354
commit ab21db050d
2 changed files with 9 additions and 1 deletions

@ -220,7 +220,7 @@ def [](attribute)
# # then yield :name and "must be specified"
# end
def each(&block)
if block.arity == 1
if block.arity <= 1
@errors.each(&block)
else
ActiveSupport::Deprecation.warn(<<~MSG)

@ -44,6 +44,14 @@ def test_include?
assert_includes errors, "foo", "errors should include 'foo' as :foo"
end
def test_each_when_arity_is_negative
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :blank)
errors.add(:gender, :blank)
assert_equal([:name, :gender], errors.map(&:attribute))
end
def test_any?
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name)