Skip to content

Commit

Permalink
Fix deprecation on AM::Errors when each is called indirectly:
Browse files Browse the repository at this point in the history
- `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.
  • Loading branch information
Edouard-chin committed Jul 20, 2019
1 parent 400b210 commit ab21db0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions activemodel/test/cases/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ab21db0

Please sign in to comment.