2006-09-04 03:32:22 +00:00
|
|
|
require 'yaml'
|
|
|
|
|
2006-07-27 00:10:06 +00:00
|
|
|
module ActiveSupport
|
2007-01-26 21:37:38 +00:00
|
|
|
module Deprecation #:nodoc:
|
2006-09-04 03:32:22 +00:00
|
|
|
mattr_accessor :debug
|
|
|
|
self.debug = false
|
|
|
|
|
2006-08-01 11:12:38 +00:00
|
|
|
# Choose the default warn behavior according to RAILS_ENV.
|
|
|
|
# Ignore deprecation warnings in production.
|
|
|
|
DEFAULT_BEHAVIORS = {
|
2006-09-04 03:32:22 +00:00
|
|
|
'test' => Proc.new { |message, callstack|
|
|
|
|
$stderr.puts(message)
|
|
|
|
$stderr.puts callstack.join("\n ") if debug
|
|
|
|
},
|
|
|
|
'development' => Proc.new { |message, callstack|
|
2007-02-08 00:17:39 +00:00
|
|
|
logger = defined?(::RAILS_DEFAULT_LOGGER) ? ::RAILS_DEFAULT_LOGGER : Logger.new($stderr)
|
|
|
|
logger.warn message
|
|
|
|
logger.debug callstack.join("\n ") if debug
|
2006-09-04 03:32:22 +00:00
|
|
|
}
|
2006-08-01 11:12:38 +00:00
|
|
|
}
|
|
|
|
|
2006-07-27 00:10:06 +00:00
|
|
|
class << self
|
2006-08-01 11:12:38 +00:00
|
|
|
def warn(message = nil, callstack = caller)
|
2006-09-04 03:32:22 +00:00
|
|
|
behavior.call(deprecation_message(callstack, message), callstack) if behavior && !silenced?
|
2006-08-01 11:12:38 +00:00
|
|
|
end
|
2006-07-27 00:10:06 +00:00
|
|
|
|
2006-08-01 11:12:38 +00:00
|
|
|
def default_behavior
|
2006-09-03 20:37:25 +00:00
|
|
|
if defined?(RAILS_ENV)
|
|
|
|
DEFAULT_BEHAVIORS[RAILS_ENV.to_s]
|
|
|
|
else
|
|
|
|
DEFAULT_BEHAVIORS['test']
|
|
|
|
end
|
2006-07-27 00:10:06 +00:00
|
|
|
end
|
2006-08-01 11:12:38 +00:00
|
|
|
|
2006-08-14 18:15:44 +00:00
|
|
|
# Have deprecations been silenced?
|
|
|
|
def silenced?
|
2006-08-29 08:24:04 +00:00
|
|
|
@silenced = false unless defined?(@silenced)
|
2006-08-14 18:15:44 +00:00
|
|
|
@silenced
|
|
|
|
end
|
|
|
|
|
2006-11-17 14:09:46 +00:00
|
|
|
# Silence deprecation warnings within the block.
|
2006-08-14 18:15:44 +00:00
|
|
|
def silence
|
2006-11-17 14:09:46 +00:00
|
|
|
old_silenced, @silenced = @silenced, true
|
2006-08-14 18:15:44 +00:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@silenced = old_silenced
|
|
|
|
end
|
|
|
|
|
2006-09-04 05:55:55 +00:00
|
|
|
attr_writer :silenced
|
|
|
|
|
|
|
|
|
2006-08-01 11:12:38 +00:00
|
|
|
private
|
|
|
|
def deprecation_message(callstack, message = nil)
|
2006-09-04 00:17:01 +00:00
|
|
|
message ||= "You are using deprecated behavior which will be removed from Rails 2.0."
|
2006-11-16 06:40:05 +00:00
|
|
|
"DEPRECATION WARNING: #{message} See http://www.rubyonrails.org/deprecation for details. #{deprecation_caller_message(callstack)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def deprecation_caller_message(callstack)
|
|
|
|
file, line, method = extract_callstack(callstack)
|
|
|
|
if file
|
|
|
|
if line && method
|
|
|
|
"(called from #{method} at #{file}:#{line})"
|
|
|
|
else
|
|
|
|
"(called from #{file}:#{line})"
|
|
|
|
end
|
|
|
|
end
|
2006-08-01 11:12:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def extract_callstack(callstack)
|
2006-11-16 06:40:05 +00:00
|
|
|
if md = callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
|
|
|
|
md.captures
|
|
|
|
else
|
|
|
|
callstack.first
|
|
|
|
end
|
2006-07-27 00:10:06 +00:00
|
|
|
end
|
|
|
|
end
|
2006-08-01 11:12:38 +00:00
|
|
|
|
|
|
|
# Behavior is a block that takes a message argument.
|
|
|
|
mattr_accessor :behavior
|
|
|
|
self.behavior = default_behavior
|
|
|
|
|
2006-09-04 08:28:44 +00:00
|
|
|
# Warnings are not silenced by default.
|
|
|
|
self.silenced = false
|
|
|
|
|
2007-01-26 21:37:38 +00:00
|
|
|
module ClassMethods #:nodoc:
|
2006-08-01 11:12:38 +00:00
|
|
|
# Declare that a method has been deprecated.
|
|
|
|
def deprecate(*method_names)
|
2007-07-24 16:48:57 +00:00
|
|
|
options = method_names.extract_options!
|
2006-10-24 16:56:59 +00:00
|
|
|
method_names = method_names + options.keys
|
2006-08-01 11:12:38 +00:00
|
|
|
method_names.each do |method_name|
|
2006-09-15 03:42:54 +00:00
|
|
|
alias_method_chain(method_name, :deprecation) do |target, punctuation|
|
|
|
|
class_eval(<<-EOS, __FILE__, __LINE__)
|
|
|
|
def #{target}_with_deprecation#{punctuation}(*args, &block)
|
2006-10-24 16:56:59 +00:00
|
|
|
::ActiveSupport::Deprecation.warn(self.class.deprecated_method_warning(:#{method_name}, #{options[method_name].inspect}), caller)
|
2006-09-15 03:42:54 +00:00
|
|
|
#{target}_without_deprecation#{punctuation}(*args, &block)
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
2006-07-27 00:10:06 +00:00
|
|
|
end
|
|
|
|
end
|
2006-09-22 01:16:09 +00:00
|
|
|
|
2006-10-24 16:56:59 +00:00
|
|
|
def deprecated_method_warning(method_name, message=nil)
|
|
|
|
warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}"
|
|
|
|
case message
|
|
|
|
when Symbol then "#{warning} (use #{message} instead)"
|
|
|
|
when String then "#{warning} (#{message})"
|
|
|
|
else warning
|
|
|
|
end
|
2006-09-22 01:16:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def deprecation_horizon
|
|
|
|
'2.0'
|
|
|
|
end
|
2006-07-27 00:10:06 +00:00
|
|
|
end
|
2006-08-01 11:12:38 +00:00
|
|
|
|
2007-01-26 21:37:38 +00:00
|
|
|
module Assertions #:nodoc:
|
2006-08-05 06:33:28 +00:00
|
|
|
def assert_deprecated(match = nil, &block)
|
2006-09-15 05:54:18 +00:00
|
|
|
result, warnings = collect_deprecations(&block)
|
2006-09-15 03:56:25 +00:00
|
|
|
assert !warnings.empty?, "Expected a deprecation warning within the block but received none"
|
2006-08-08 21:21:04 +00:00
|
|
|
if match
|
|
|
|
match = Regexp.new(Regexp.escape(match)) unless match.is_a?(Regexp)
|
2006-09-15 03:56:25 +00:00
|
|
|
assert warnings.any? { |w| w =~ match }, "No deprecation warning matched #{match}: #{warnings.join(', ')}"
|
2006-08-08 21:21:04 +00:00
|
|
|
end
|
2006-09-15 05:54:18 +00:00
|
|
|
result
|
2006-08-01 11:12:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_deprecated(&block)
|
2006-09-15 05:54:18 +00:00
|
|
|
result, deprecations = collect_deprecations(&block)
|
2006-08-08 21:21:04 +00:00
|
|
|
assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}"
|
2006-09-15 05:54:18 +00:00
|
|
|
result
|
2006-08-01 11:12:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2006-08-08 21:21:04 +00:00
|
|
|
def collect_deprecations
|
2006-08-01 11:12:38 +00:00
|
|
|
old_behavior = ActiveSupport::Deprecation.behavior
|
2006-08-08 21:21:04 +00:00
|
|
|
deprecations = []
|
2006-09-04 03:32:22 +00:00
|
|
|
ActiveSupport::Deprecation.behavior = Proc.new do |message, callstack|
|
2006-08-08 21:21:04 +00:00
|
|
|
deprecations << message
|
|
|
|
end
|
2006-09-15 05:54:18 +00:00
|
|
|
result = yield
|
|
|
|
[result, deprecations]
|
2006-08-01 11:12:38 +00:00
|
|
|
ensure
|
|
|
|
ActiveSupport::Deprecation.behavior = old_behavior
|
|
|
|
end
|
|
|
|
end
|
2006-08-05 06:33:28 +00:00
|
|
|
|
2006-12-20 21:28:31 +00:00
|
|
|
# Stand-in for @request, @attributes, @params, etc which emits deprecation
|
|
|
|
# warnings on any method call (except #inspect).
|
2007-01-26 21:37:38 +00:00
|
|
|
class DeprecatedInstanceVariableProxy #:nodoc:
|
2006-08-05 06:33:28 +00:00
|
|
|
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
|
|
|
|
|
|
|
|
def initialize(instance, method, var = "@#{method}")
|
|
|
|
@instance, @method, @var = instance, method, var
|
|
|
|
end
|
|
|
|
|
2006-12-20 21:28:31 +00:00
|
|
|
# Don't give a deprecation warning on inspect since test/unit and error
|
|
|
|
# logs rely on it for diagnostics.
|
|
|
|
def inspect
|
|
|
|
target.inspect
|
|
|
|
end
|
|
|
|
|
2006-08-05 06:33:28 +00:00
|
|
|
private
|
|
|
|
def method_missing(called, *args, &block)
|
2006-08-08 07:03:44 +00:00
|
|
|
warn caller, called, args
|
2006-11-17 14:09:46 +00:00
|
|
|
target.__send__(called, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def target
|
|
|
|
@instance.__send__(@method)
|
|
|
|
end
|
|
|
|
|
|
|
|
def warn(callstack, called, args)
|
|
|
|
ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
|
2006-08-05 06:33:28 +00:00
|
|
|
end
|
|
|
|
end
|
2006-07-27 00:10:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-09-03 20:29:43 +00:00
|
|
|
class Module
|
2006-08-01 11:12:38 +00:00
|
|
|
include ActiveSupport::Deprecation::ClassMethods
|
|
|
|
end
|
|
|
|
|
2006-12-18 20:59:40 +00:00
|
|
|
require 'test/unit/error'
|
2006-12-17 08:21:45 +00:00
|
|
|
|
2006-08-01 11:12:38 +00:00
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
class TestCase
|
|
|
|
include ActiveSupport::Deprecation::Assertions
|
|
|
|
end
|
2006-12-17 08:21:45 +00:00
|
|
|
|
|
|
|
class Error # :nodoc:
|
|
|
|
# Silence warnings when reporting test errors.
|
|
|
|
def message_with_silenced_deprecation
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
message_without_silenced_deprecation
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method_chain :message, :silenced_deprecation
|
|
|
|
end
|
2006-08-01 11:12:38 +00:00
|
|
|
end
|
|
|
|
end
|