Created
March 26, 2020 19:11
-
-
Save unixmonkey/296f7c46f9982895516350d244994311 to your computer and use it in GitHub Desktop.
Deprecation Handler for Rails that ignores deprecations emitted more than once, or custom message regular expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Override Deprecation warnings to only show each unique deprecation message | |
# once instead of repeating it every time the deprecated code is run. | |
# This can help prevent the "wall of deprecations" when one commonly-called | |
# action produces a deprecation over and over again. | |
# | |
# You can also add regular expressions to the `@@ignored` array to silence them. | |
# | |
if Rails.env.test? | |
module ActiveSupport | |
class Deprecation | |
module Reporting | |
# Use Regexes to match against ignored deprecations. | |
@@ignored = [/halt_callback_chains_on_return_false/] | |
@@seen = [] | |
def warn(message = nil, callstack = nil) | |
return if silenced || | |
@@ignored.detect { |regex| message.match?(regex) } || | |
@@seen.include?(message) | |
# Add to seen list, so we don't warn again. | |
@@seen << message | |
# This is the normal Rails deprecation handling code. | |
callstack ||= caller_locations(2) | |
deprecation_message(callstack, message).tap do |m| | |
behavior.each do |b| | |
if Rails.version > '6.0.0' | |
b.call(m, callstack, deprecation_horizon, gem_name) | |
else | |
b.call(m, callstack) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment