4
module Deprecation #:nodoc:
8
# Choose the default warn behavior according to RAILS_ENV.
9
# Ignore deprecation warnings in production.
11
'test' => Proc.new { |message, callstack|
13
$stderr.puts callstack.join("\n ") if debug
15
'development' => Proc.new { |message, callstack|
16
logger = (defined?(Rails) && Rails.logger) ? Rails.logger : Logger.new($stderr)
18
logger.debug callstack.join("\n ") if debug
23
def warn(message = nil, callstack = caller)
24
behavior.call(deprecation_message(callstack, message), callstack) if behavior && !silenced?
28
if defined?(RAILS_ENV)
29
DEFAULT_BEHAVIORS[RAILS_ENV.to_s]
31
DEFAULT_BEHAVIORS['test']
35
# Have deprecations been silenced?
37
@silenced = false unless defined?(@silenced)
41
# Silence deprecation warnings within the block.
43
old_silenced, @silenced = @silenced, true
46
@silenced = old_silenced
53
def deprecation_message(callstack, message = nil)
54
message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
55
"DEPRECATION WARNING: #{message}. #{deprecation_caller_message(callstack)}"
58
def deprecation_caller_message(callstack)
59
file, line, method = extract_callstack(callstack)
62
"(called from #{method} at #{file}:#{line})"
64
"(called from #{file}:#{line})"
69
def extract_callstack(callstack)
70
if md = callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
78
# Behavior is a block that takes a message argument.
79
mattr_accessor :behavior
80
self.behavior = default_behavior
82
# Warnings are not silenced by default.
85
module ClassMethods #:nodoc:
86
# Declare that a method has been deprecated.
87
def deprecate(*method_names)
88
options = method_names.extract_options!
89
method_names = method_names + options.keys
90
method_names.each do |method_name|
91
alias_method_chain(method_name, :deprecation) do |target, punctuation|
92
class_eval(<<-EOS, __FILE__, __LINE__)
93
def #{target}_with_deprecation#{punctuation}(*args, &block) # def generate_secret_with_deprecation(*args, &block)
94
::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn(
95
self.class.deprecated_method_warning( # self.class.deprecated_method_warning(
96
:#{method_name}, # :generate_secret,
97
#{options[method_name].inspect}), # "You should use ActiveSupport::SecureRandom.hex(64)"),
100
send(:#{target}_without_deprecation#{punctuation}, *args, &block) # send(:generate_secret_without_deprecation, *args, &block)
107
def deprecated_method_warning(method_name, message=nil)
108
warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}"
110
when Symbol then "#{warning} (use #{message} instead)"
111
when String then "#{warning} (#{message})"
116
def deprecation_horizon
121
class DeprecationProxy #:nodoc:
123
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
126
# Don't give a deprecation warning on inspect since test/unit and error
127
# logs rely on it for diagnostics.
133
def method_missing(called, *args, &block)
134
warn caller, called, args
135
target.__send__(called, *args, &block)
139
class DeprecatedObjectProxy < DeprecationProxy
140
def initialize(object, message)
150
def warn(callstack, called, args)
151
ActiveSupport::Deprecation.warn(@message, callstack)
155
# Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc.
156
# which emits deprecation warnings on any method call (except +inspect+).
157
class DeprecatedInstanceVariableProxy < DeprecationProxy #:nodoc:
158
def initialize(instance, method, var = "@#{method}")
159
@instance, @method, @var = instance, method, var
164
@instance.__send__(@method)
167
def warn(callstack, called, args)
168
ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
172
class DeprecatedConstantProxy < DeprecationProxy #:nodoc:
173
def initialize(old_const, new_const)
174
@old_const = old_const
175
@new_const = new_const
184
@new_const.to_s.constantize
187
def warn(callstack, called, args)
188
ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
195
include ActiveSupport::Deprecation::ClassMethods