Skip to content

Instantly share code, notes, and snippets.

@unixmonkey
Created March 26, 2020 19:11
Show Gist options
  • Save unixmonkey/296f7c46f9982895516350d244994311 to your computer and use it in GitHub Desktop.
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
# 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