aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRobert Pankowecki <robert.pankowecki@gmail.com>2011-07-25 21:05:06 +0200
committerPiotr Niełacny <piotr.nielacny@gmail.com>2012-09-13 08:42:00 +0200
commit2c690a0f5b36896da9b003d4e24159a27ebd7f71 (patch)
tree8d9aacd7f0f4a22f30b8a3bc7bd779838d23bf67 /activesupport
parentc4b857299b3322572b006d825bb9dc9912101e99 (diff)
downloadrails-2c690a0f5b36896da9b003d4e24159a27ebd7f71.tar.gz
rails-2c690a0f5b36896da9b003d4e24159a27ebd7f71.tar.bz2
rails-2c690a0f5b36896da9b003d4e24159a27ebd7f71.zip
extend ActiveSupport::Deprecation with self, allow other objects to extend/include it also.
test local deprecation deprecator object Test ActiveSupport::Deprecation when included
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/deprecation.rb2
-rw-r--r--activesupport/lib/active_support/deprecation/behaviors.rb64
-rw-r--r--activesupport/lib/active_support/deprecation/method_wrappers.rb5
-rw-r--r--activesupport/lib/active_support/deprecation/proxy_wrappers.rb19
-rw-r--r--activesupport/lib/active_support/deprecation/reporting.rb117
-rw-r--r--activesupport/test/deprecation_test.rb127
6 files changed, 232 insertions, 102 deletions
diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb
index e3b4a7240e..968ee1eaf8 100644
--- a/activesupport/lib/active_support/deprecation.rb
+++ b/activesupport/lib/active_support/deprecation.rb
@@ -16,4 +16,4 @@ module ActiveSupport
self.silenced = false
self.debug = false
end
-end \ No newline at end of file
+end
diff --git a/activesupport/lib/active_support/deprecation/behaviors.rb b/activesupport/lib/active_support/deprecation/behaviors.rb
index fc962dcb57..c956560a99 100644
--- a/activesupport/lib/active_support/deprecation/behaviors.rb
+++ b/activesupport/lib/active_support/deprecation/behaviors.rb
@@ -2,46 +2,44 @@ require "active_support/notifications"
module ActiveSupport
module Deprecation
- class << self
- # Whether to print a backtrace along with the warning.
- attr_accessor :debug
+ # Whether to print a backtrace along with the warning.
+ attr_accessor :debug
- # Returns the current behavior or if one isn't set, defaults to +:stderr+
- def behavior
- @behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
- end
+ # Returns the current behavior or if one isn't set, defaults to +:stderr+
+ def behavior
+ @behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
+ end
- # Sets the behavior to the specified value. Can be a single value, array, or
- # an object that responds to +call+.
- #
- # Available behaviors:
- #
- # [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
- # [+log+] Log all deprecation warnings to +Rails.logger+.
- # [+notify+] Use <tt>ActiveSupport::Notifications</tt> to notify +deprecation.rails+.
- # [+silence+] Do nothing.
- #
- # Setting behaviors only affects deprecations that happen after boot time.
- # Deprecation warnings raised by gems are not affected by this setting because
- # they happen before Rails boots up.
- #
- # ActiveSupport::Deprecation.behavior = :stderr
- # ActiveSupport::Deprecation.behavior = [:stderr, :log]
- # ActiveSupport::Deprecation.behavior = MyCustomHandler
- # ActiveSupport::Deprecation.behavior = proc { |message, callstack|
- # # custom stuff
- # }
- def behavior=(behavior)
- @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
- end
+ # Sets the behavior to the specified value. Can be a single value, array, or
+ # an object that responds to +call+.
+ #
+ # Available behaviors:
+ #
+ # [+stderr+] Log all deprecation warnings to +$stderr+.
+ # [+log+] Log all deprecation warnings to +Rails.logger+.
+ # [+notify] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
+ # [+silence+] Do nothing.
+ #
+ # Setting behaviors only affects deprecations that happen after boot time.
+ # Deprecation warnings raised by gems are not affected by this setting because
+ # they happen before Rails boots up.
+ #
+ # ActiveSupport::Deprecation.behavior = :stderr
+ # ActiveSupport::Deprecation.behavior = [:stderr, :log]
+ # ActiveSupport::Deprecation.behavior = MyCustomHandler
+ # ActiveSupport::Deprecation.behavior = proc { |message, callstack|
+ # # custom stuff
+ # }
+ def behavior=(behavior)
+ @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
end
# Default warning behaviors per Rails.env.
DEFAULT_BEHAVIORS = {
:stderr => Proc.new { |message, callstack|
- $stderr.puts(message)
- $stderr.puts callstack.join("\n ") if debug
- },
+ $stderr.puts(message)
+ $stderr.puts callstack.join("\n ") if debug
+ },
:log => Proc.new { |message, callstack|
logger =
if defined?(Rails) && Rails.logger
diff --git a/activesupport/lib/active_support/deprecation/method_wrappers.rb b/activesupport/lib/active_support/deprecation/method_wrappers.rb
index 257b70e34a..f4ad15cc63 100644
--- a/activesupport/lib/active_support/deprecation/method_wrappers.rb
+++ b/activesupport/lib/active_support/deprecation/method_wrappers.rb
@@ -32,8 +32,9 @@ module ActiveSupport
target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
target_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
def #{target}_with_deprecation#{punctuation}(*args, &block)
- ::ActiveSupport::Deprecation.warn(
- ::ActiveSupport::Deprecation.deprecated_method_warning(
+ deprecator = respond_to?(:deprecator) ? deprecator() : ActiveSupport::Deprecation
+ deprecator.warn(
+ deprecator.deprecated_method_warning(
:#{method_name},
#{options[method_name].inspect}),
caller
diff --git a/activesupport/lib/active_support/deprecation/proxy_wrappers.rb b/activesupport/lib/active_support/deprecation/proxy_wrappers.rb
index a65fcafb44..382f2e88f0 100644
--- a/activesupport/lib/active_support/deprecation/proxy_wrappers.rb
+++ b/activesupport/lib/active_support/deprecation/proxy_wrappers.rb
@@ -26,9 +26,10 @@ module ActiveSupport
end
class DeprecatedObjectProxy < DeprecationProxy #:nodoc:
- def initialize(object, message)
+ def initialize(object, message, deprecator = ActiveSupport::Deprecation)
@object = object
@message = message
+ @deprecator = deprecator
end
private
@@ -37,15 +38,18 @@ module ActiveSupport
end
def warn(callstack, called, args)
- ActiveSupport::Deprecation.warn(@message, callstack)
+ @deprecator.warn(@message, callstack)
end
end
# Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc.
# which emits deprecation warnings on any method call (except +inspect+).
class DeprecatedInstanceVariableProxy < DeprecationProxy #:nodoc:
- def initialize(instance, method, var = "@#{method}")
- @instance, @method, @var = instance, method, var
+ def initialize(instance, method, var = :"@#{method}", deprecator = nil)
+ @instance = instance
+ @method = method
+ @var = var
+ @deprecator = deprecator || (@instance.respond_to?(:deprecator) ? @instance.deprecator : ActiveSupport::Deprecation)
end
private
@@ -54,14 +58,15 @@ module ActiveSupport
end
def warn(callstack, called, args)
- ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
+ @deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
end
end
class DeprecatedConstantProxy < DeprecationProxy #:nodoc:all
- def initialize(old_const, new_const)
+ def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation)
@old_const = old_const
@new_const = new_const
+ @deprecator = deprecator
end
def class
@@ -74,7 +79,7 @@ module ActiveSupport
end
def warn(callstack, called, args)
- ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
+ @deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
end
end
end
diff --git a/activesupport/lib/active_support/deprecation/reporting.rb b/activesupport/lib/active_support/deprecation/reporting.rb
index a1e9618084..69de79b93c 100644
--- a/activesupport/lib/active_support/deprecation/reporting.rb
+++ b/activesupport/lib/active_support/deprecation/reporting.rb
@@ -1,74 +1,73 @@
module ActiveSupport
module Deprecation
- class << self
- attr_accessor :silenced
+ attr_accessor :silenced
- # Outputs a deprecation warning to the output configured by
- # <tt>ActiveSupport::Deprecation.behavior</tt>.
- #
- # ActiveSupport::Deprecation.warn("something broke!")
- # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
- def warn(message = nil, callstack = caller)
- return if silenced
- deprecation_message(callstack, message).tap do |m|
- behavior.each { |b| b.call(m, callstack) }
- end
+ # Outputs a deprecation warning to the output configured by
+ # <tt>ActiveSupport::Deprecation.behavior</tt>.
+ #
+ # ActiveSupport::Deprecation.warn("something broke!")
+ # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
+ def warn(message = nil, callstack = caller)
+ return if silenced
+ deprecation_message(callstack, message).tap do |m|
+ behavior.each { |b| b.call(m, callstack) }
end
+ end
- # Silence deprecation warnings within the block.
- #
- # ActiveSupport::Deprecation.warn("something broke!")
- # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
- #
- # ActiveSupport::Deprecation.silence do
- # ActiveSupport::Deprecation.warn("something broke!")
- # end
- # # => nil
- def silence
- old_silenced, @silenced = @silenced, true
- yield
- ensure
- @silenced = old_silenced
- end
+ # Silence deprecation warnings within the block.
+ #
+ # ActiveSupport::Deprecation.warn("something broke!")
+ # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
+ #
+ # ActiveSupport::Deprecation.silence do
+ # ActiveSupport::Deprecation.warn("something broke!")
+ # end
+ # # => nil
+ def silence
+ old_silenced, @silenced = @silenced, true
+ yield
+ ensure
+ @silenced = old_silenced
+ end
- 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
+ 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})"
end
+ warning
+ end
- private
- def deprecation_message(callstack, message = nil)
- message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
- message += '.' unless message =~ /\.$/
- "DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}"
- end
+ private
+
+ def deprecation_message(callstack, message = nil)
+ message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
+ message += '.' unless message =~ /\.$/
+ "DEPRECATION WARNING: #{message} #{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
+ 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
+ end
- def extract_callstack(callstack)
- rails_gem_root = File.expand_path("../../../../..", __FILE__) + "/"
- offending_line = callstack.find { |line| !line.start_with?(rails_gem_root) } || callstack.first
- if offending_line
- if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
- md.captures
- else
- offending_line
- end
- end
+ def extract_callstack(callstack)
+ rails_gem_root = File.expand_path("../../../../..", __FILE__) + "/"
+ offending_line = callstack.find { |line| !line.start_with?(rails_gem_root) } || callstack.first
+ if offending_line
+ if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
+ md.captures
+ else
+ offending_line
end
+ end
end
end
end
diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb
index e21f3efe36..308c7c3fe7 100644
--- a/activesupport/test/deprecation_test.rb
+++ b/activesupport/test/deprecation_test.rb
@@ -186,4 +186,131 @@ class DeprecationTest < ActiveSupport::TestCase
def test_deprecation_with_explicit_message
assert_deprecated(/you now need to do something extra for this one/) { @dtc.d }
end
+
+ def test_deprecation_in_other_module_does_not_interfere
+ messages = []
+
+ m = Module.new
+ m.extend ActiveSupport::Deprecation
+ m.behavior = Proc.new{|message, callstack| messages << message}
+ assert_not_deprecated do # not globally
+ assert_difference("messages.size") do # but locally
+ m.warn("warning")
+ end
+ end
+ end
+
+ def test_deprecated_method_with_deprecator_implemented
+ deprecator = deprecator_with_messages
+ def deprecator.deprecated_method_warning(method, *params)
+ "deprecator.deprecated_method_warning.#{method}"
+ end
+
+ deprecatee = Class.new() do
+ def method
+ end
+ deprecate :method
+ define_method(:deprecator){ deprecator }
+ end
+
+ deprecatee.new.method
+ assert deprecator.messages.first.match("DEPRECATION WARNING: deprecator.deprecated_method_warning.method")
+ end
+
+ def test_deprecated_constant_with_deprecator_given
+ deprecator = deprecator_with_messages
+ klass = Class.new()
+ klass.const_set(:OLD, ActiveSupport::Deprecation::DeprecatedConstantProxy.new('klass::OLD', 'Object', deprecator) )
+ assert_difference("deprecator.messages.size") do
+ klass::OLD.to_s
+ end
+ end
+
+ def test_deprecated_instance_variable_with_instance_deprecator
+ deprecator = deprecator_with_messages
+
+ klass = Class.new() do
+ def initialize
+ @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request)
+ @_request = :a_request
+ end
+ def request; @_request end
+ def old_request; @request end
+ define_method(:deprecator) { deprecator }
+ end
+
+ assert_difference("deprecator.messages.size") { klass.new.old_request.to_s }
+
+ end
+
+ def test_deprecated_instance_variable_with_given_deprecator
+ deprecator = deprecator_with_messages
+
+ klass = Class.new() do
+ define_method(:initialize) do
+ @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
+ @_request = :a_request
+ end
+ def request; @_request end
+ def old_request; @request end
+ end
+
+ assert_difference("deprecator.messages.size") { klass.new.old_request.to_s }
+ end
+
+ def test_included_deprecation_module
+ klass = Class.new() do
+ attr_reader :last_message
+ include ActiveSupport::Deprecation
+ def deprecated_method
+ warn(deprecated_method_warning(:deprecated_method, "You are calling deprecated method"))
+ end
+
+ private
+
+ def deprecated_method_warning(method_name, message = nil)
+ message || "#{method_name} is deprecated and will be removed from This Library"
+ end
+
+ def behavior
+ @behavior ||= [Proc.new { |message| @last_message = message }]
+ end
+ end
+
+ object = klass.new
+ object.deprecated_method
+ assert_match(/You are calling deprecated method/, object.last_message)
+ end
+
+ unless defined?(::MiniTest)
+ def test_assertion_failed_error_doesnt_spout_deprecation_warnings
+ error_class = Class.new(StandardError) do
+ def message
+ ActiveSupport::Deprecation.warn 'warning in error message'
+ super
+ end
+ end
+
+ raise error_class.new('hmm')
+
+ rescue => e
+ error = Test::Unit::Error.new('testing ur doodz', e)
+ assert_not_deprecated { error.message }
+ assert_nil @last_message
+ end
+ end
+
+
+ private
+
+
+ def deprecator_with_messages
+ deprecator = Object.new
+ deprecator.extend(ActiveSupport::Deprecation)
+ deprecator.behavior = Proc.new{|message, callstack| deprecator.messages << message}
+ def deprecator.messages
+ @messages ||= []
+ end
+ deprecator
+ end
end