Skip to content

Commit

Permalink
Fix NoMethodError on custom ActiveSupport::Deprecation behavior
Browse files Browse the repository at this point in the history
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
  • Loading branch information
r7kamura committed Jul 5, 2022
1 parent 19f9922 commit 6f9bb2c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 10 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
* Fix `NoMethodError` on custom `ActiveSupport::Deprecation` behavior.

`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.

*Ryo Nakamura*

* Support `:urlsafe` option for `MessageEncryptor`.

The `MessageEncryptor` constructor now accepts a `:urlsafe` option, similar
Expand Down
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/deprecation/behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ def arity_coerce(behavior)
raise ArgumentError, "#{behavior.inspect} is not a valid deprecation behavior."
end

if behavior.arity == 4 || behavior.arity == -1
behavior
else
if behavior.respond_to?(:arity) && behavior.arity == 2
-> message, callstack, _, _ { behavior.call(message, callstack) }
else
behavior
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/deprecation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ def test_default_invalid_behavior
assert_equal ":invalid is not a valid deprecation behavior.", e.message
end

def test_custom_behavior
custom_behavior_class = Class.new do
def call(message, callstack, horizon, gem_name)
$stderr.puts message
end
end
ActiveSupport::Deprecation.behavior = custom_behavior_class.new

content = capture(:stderr) do
ActiveSupport::Deprecation.warn("foo")
end

assert_match(/foo/, content)
end

def test_deprecated_instance_variable_proxy
assert_not_deprecated { @dtc.request.size }

Expand Down

0 comments on commit 6f9bb2c

Please sign in to comment.