diff options
Diffstat (limited to 'activesupport/lib/active_support/deprecation')
4 files changed, 104 insertions, 101 deletions
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 |