Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NoMethodError on custom ActiveSupport::Deprecation behavior #45521

Merged
merged 1 commit into from Jul 6, 2022

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Jul 5, 2022

Summary

ActiveSupport::Deprecation.behavior= was supposed to accept any object that responds to call, but in fact its internal implementation assumed that this object could respond to arity, so it was restricted to only Proc objects.

This change removes this arity restriction of custom behaviors.

Other Information

The specification of custom behavior is mentioned in the following comment:

# Sets the behavior to the specified value. Can be a single value, array,
# or an object that responds to +call+.

Without this change, the test I added would fail as follows.

$ bundle exec ruby test/deprecation_test.rb
Running 61 tests in parallel using 12 processes
Run options: --seed 48446

# Running:

.................................................E

Error:
DeprecationTest#test_custom_behavior:
NoMethodError: undefined method `arity' for #<#<Class:0x00007f406a20d418>:0x00007f406a20d2d8>
    /home/r7kamura/ghq/github.com/rails/rails/activesupport/lib/active_support/deprecation/behaviors.rb:117:in `arity_coerce'
    /home/r7kamura/ghq/github.com/rails/rails/activesupport/lib/active_support/deprecation/behaviors.rb:100:in `block in behavior='
    /home/r7kamura/ghq/github.com/rails/rails/activesupport/lib/active_support/deprecation/behaviors.rb:100:in `map'
    /home/r7kamura/ghq/github.com/rails/rails/activesupport/lib/active_support/deprecation/behaviors.rb:100:in `behavior='
    /home/r7kamura/ghq/github.com/rails/rails/activesupport/lib/active_support/deprecation/instance_delegator.rb:21:in `behavior='
    test/deprecation_test.rb:220:in `test_custom_behavior'


rails test test/deprecation_test.rb:213

...........

Finished in 0.203086s, 300.3660 runs/s, 1240.8561 assertions/s.
61 runs, 252 assertions, 0 failures, 1 errors, 0 skips

@r7kamura r7kamura force-pushed the feature/fix-custom-behavior branch from 013f5cb to 48b6d94 Compare July 5, 2022 21:34
with some additional changes made later:

1. Flip the condition for backward compatibility
   Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

2. Improve custom behavior test
   Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

3. Fix indentation
@r7kamura r7kamura force-pushed the feature/fix-custom-behavior branch from 48b6d94 to 6f9bb2c Compare July 5, 2022 21:42
@jonathanhefner jonathanhefner merged commit 54e3f8c into rails:main Jul 6, 2022
@jonathanhefner
Copy link
Member

jonathanhefner commented Jul 6, 2022

Thank you, @r7kamura! 😃

(Backported to 7-0-stable.)

@r7kamura r7kamura deleted the feature/fix-custom-behavior branch July 6, 2022 19:33
jonathanhefner added a commit that referenced this pull request Jul 24, 2022
Fix NoMethodError on custom ActiveSupport::Deprecation behavior

(cherry picked from commit 54e3f8c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants