Skip to content

Instantly share code, notes, and snippets.

@rafaelfranca
Last active March 12, 2024 14:33
Show Gist options
  • Save rafaelfranca/5492531 to your computer and use it in GitHub Desktop.
Save rafaelfranca/5492531 to your computer and use it in GitHub Desktop.
Examples of `ActiveSupport::Deprecation`
require 'active_support'
class Foo
def foo
"foo"
end
def bar
ActiveSupport::Deprecation.warn("bar is deprecated")
foo
end
def bar1
ActiveSupport::Deprecation.warn("bar1 is deprecated", caller)
foo
end
def bar2
ActiveSupport::Deprecation.deprecation_warning("bar2 is deprecated")
foo
end
def bar3
ActiveSupport::Deprecation.deprecation_warning("bar3 is deprecated", :bar)
foo
end
def bar4
ActiveSupport::Deprecation.deprecation_warning("bar4 is deprecated", "use bar instead")
foo
end
def bar5
ActiveSupport::Deprecation.deprecation_warning("bar5 is deprecated", "use bar instead", caller)
foo
end
def bar5
ActiveSupport::Deprecation.deprecation_warning("bar5 is deprecated", "use bar instead", caller)
foo
end
def bar6
deprecator = ActiveSupport::Deprecation.new("1.0", "foo")
deprecator.deprecation_warning("bar6 is deprecated")
foo
end
end
Foo1 = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Foo1', 'Foo')
class Bar
def initialize
deprecator = ActiveSupport::Deprecation.new("1.0", "foo")
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
@_request = :my_request
end
def request
@_request
end
def old_request
@request
end
end
@old_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!")
module Fred
extend self
def foo; end
def bar; end
def baz; end
end
ActiveSupport::Deprecation.deprecate_methods(Fred, :foo, bar: :qux, baz: 'use Bar#baz instead')
class Baz
class Deprecator
def deprecation_warning(deprecated_method_name, message, callstack = nil)
message = "#{deprecated_method_name} is deprecated and will be removed from MyLibrary | #{message}"
Kernel.warn message
end
end
def self.deprecator
Deprecator.new
end
def foo
true
end
def bar
true
end
deprecate foo: :bar, deprecator: deprecator
end
class MyCustomHandler
def self.call(message, callstack)
true
end
end
ActiveSupport::Deprecation.behavior = :stderr
# ActiveSupport::Deprecation.behavior = [:stderr, :log]
# ActiveSupport::Deprecation.behavior = MyCustomHandler
# ActiveSupport::Deprecation.behavior = proc { |message, callstack|
# # custom stuff
# }
#
# https://github.com/rails/rails/commit/3f04785f77899b286d02a0a95dd7fbfc341dc999
# https://github.com/rails/rails/commit/e4637530cd181fb03b35ce5fa2e95508f4bc1e1c
@wagenet
Copy link

wagenet commented Apr 13, 2018

The examples for deprecation_warning have more than just a method name as the first argument, e.g. deprecation_warning("bar2 is deprecated"). However, I believe it should just be the method name: deprecation_warning("bar2") as the "is deprecated" is added by the default behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment