From b955939d55b71905fa3e5036149ee4c84943c599 Mon Sep 17 00:00:00 2001 From: Alexey Gaziev Date: Tue, 30 Oct 2012 10:23:41 +0800 Subject: Make caller attribute in deprecation methods optional --- activesupport/CHANGELOG.md | 5 +++++ activesupport/lib/active_support/benchmarkable.rb | 2 +- activesupport/lib/active_support/callbacks.rb | 4 ++-- activesupport/lib/active_support/core_ext/array/uniq_by.rb | 4 ++-- .../lib/active_support/core_ext/date_time/calculations.rb | 2 +- activesupport/lib/active_support/core_ext/module/introspection.rb | 2 +- activesupport/lib/active_support/core_ext/proc.rb | 2 +- activesupport/lib/active_support/core_ext/string/encoding.rb | 2 +- activesupport/lib/active_support/deprecation/method_wrappers.rb | 2 +- activesupport/lib/active_support/deprecation/reporting.rb | 7 +++++-- activesupport/lib/active_support/json/variable.rb | 2 +- activesupport/lib/active_support/test_case.rb | 2 +- activesupport/test/deprecation_test.rb | 2 +- 13 files changed, 23 insertions(+), 15 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 83e763fd77..1c1edc9048 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Make callstack attribute optional in + ActiveSupport::Deprecation::Reporting methods `warn` and `deprecation_warning` + + *Alexey Gaziev* + * Implement HashWithIndifferentAccess#replace so key? works correctly. *David Graham* * Handle the possible Permission Denied errors atomic.rb might trigger due to its chown and chmod calls. *Daniele Sluijters* diff --git a/activesupport/lib/active_support/benchmarkable.rb b/activesupport/lib/active_support/benchmarkable.rb index 4c7a694c52..6413502b53 100644 --- a/activesupport/lib/active_support/benchmarkable.rb +++ b/activesupport/lib/active_support/benchmarkable.rb @@ -49,7 +49,7 @@ module ActiveSupport # Silence the logger during the execution of the block. def silence message = "ActiveSupport::Benchmarkable#silence is deprecated. It will be removed from Rails 4.1." - ActiveSupport::Deprecation.warn(message, caller) + ActiveSupport::Deprecation.warn message old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger yield ensure diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index a4ea6748f9..8199f431f1 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -284,13 +284,13 @@ module ActiveSupport if !filter.respond_to?(kind) && filter.respond_to?(:filter) message = "Filter object with #filter method is deprecated. Define method corresponding " \ "to filter type (#before, #after or #around)." - ActiveSupport::Deprecation.warn(message, caller) + ActiveSupport::Deprecation.warn message filter.singleton_class.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{kind}(context, &block) filter(context, &block) end RUBY_EVAL elsif filter.respond_to?(:before) && filter.respond_to?(:after) && kind == :around && !filter.respond_to?(:around) message = "Filter object with #before and #after methods is deprecated. Define #around method instead." - ActiveSupport::Deprecation.warn(message, caller) + ActiveSupport::Deprecation.warn message def filter.around(context) should_continue = before(context) yield if should_continue diff --git a/activesupport/lib/active_support/core_ext/array/uniq_by.rb b/activesupport/lib/active_support/core_ext/array/uniq_by.rb index c1d5a355a4..ca3b7748cd 100644 --- a/activesupport/lib/active_support/core_ext/array/uniq_by.rb +++ b/activesupport/lib/active_support/core_ext/array/uniq_by.rb @@ -5,7 +5,7 @@ class Array # # [1, 2, 3, 4].uniq_by { |i| i.odd? } # => [1, 2] def uniq_by(&block) - ActiveSupport::Deprecation.warn 'uniq_by is deprecated. Use Array#uniq instead', caller + ActiveSupport::Deprecation.warn 'uniq_by is deprecated. Use Array#uniq instead' uniq(&block) end @@ -13,7 +13,7 @@ class Array # # Same as +uniq_by+, but modifies +self+. def uniq_by!(&block) - ActiveSupport::Deprecation.warn 'uniq_by! is deprecated. Use Array#uniq! instead', caller + ActiveSupport::Deprecation.warn 'uniq_by! is deprecated. Use Array#uniq! instead' uniq!(&block) end end diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index 385aa586bb..0c6437b02b 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -4,7 +4,7 @@ class DateTime class << self # *DEPRECATED*: Use +DateTime.civil_from_format+ directly. def local_offset - ActiveSupport::Deprecation.warn 'DateTime.local_offset is deprecated. Use DateTime.civil_from_format directly.', caller + ActiveSupport::Deprecation.warn 'DateTime.local_offset is deprecated. Use DateTime.civil_from_format directly.' ::Time.local(2012).utc_offset.to_r / 86400 end diff --git a/activesupport/lib/active_support/core_ext/module/introspection.rb b/activesupport/lib/active_support/core_ext/module/introspection.rb index 649a969149..08e5f8a5c3 100644 --- a/activesupport/lib/active_support/core_ext/module/introspection.rb +++ b/activesupport/lib/active_support/core_ext/module/introspection.rb @@ -72,7 +72,7 @@ class Module # This method is useful for forward compatibility, since Ruby 1.8 returns # constant names as strings, whereas 1.9 returns them as symbols. def local_constant_names - ActiveSupport::Deprecation.warn 'Module#local_constant_names is deprecated, use Module#local_constants instead', caller + ActiveSupport::Deprecation.warn 'Module#local_constant_names is deprecated, use Module#local_constants instead' local_constants.map { |c| c.to_s } end end diff --git a/activesupport/lib/active_support/core_ext/proc.rb b/activesupport/lib/active_support/core_ext/proc.rb index cd63740940..166c3855a0 100644 --- a/activesupport/lib/active_support/core_ext/proc.rb +++ b/activesupport/lib/active_support/core_ext/proc.rb @@ -3,7 +3,7 @@ require "active_support/deprecation" class Proc #:nodoc: def bind(object) - ActiveSupport::Deprecation.warn 'Proc#bind is deprecated and will be removed in future versions', caller + ActiveSupport::Deprecation.warn 'Proc#bind is deprecated and will be removed in future versions' block, time = self, Time.now object.class_eval do diff --git a/activesupport/lib/active_support/core_ext/string/encoding.rb b/activesupport/lib/active_support/core_ext/string/encoding.rb index dc635ed6a5..a583b914db 100644 --- a/activesupport/lib/active_support/core_ext/string/encoding.rb +++ b/activesupport/lib/active_support/core_ext/string/encoding.rb @@ -2,7 +2,7 @@ require 'active_support/deprecation' class String def encoding_aware? - ActiveSupport::Deprecation.warn 'String#encoding_aware? is deprecated', caller + ActiveSupport::Deprecation.warn 'String#encoding_aware? is deprecated' true end end diff --git a/activesupport/lib/active_support/deprecation/method_wrappers.rb b/activesupport/lib/active_support/deprecation/method_wrappers.rb index d3907b03e5..cab8a1b14d 100644 --- a/activesupport/lib/active_support/deprecation/method_wrappers.rb +++ b/activesupport/lib/active_support/deprecation/method_wrappers.rb @@ -33,7 +33,7 @@ module ActiveSupport method_names.each do |method_name| target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation| target_module.send(:define_method, "#{target}_with_deprecation#{punctuation}") do |*args, &block| - deprecator.deprecation_warning(method_name, options[method_name], caller) + deprecator.deprecation_warning(method_name, options[method_name]) send(:"#{target}_without_deprecation#{punctuation}", *args, &block) end end diff --git a/activesupport/lib/active_support/deprecation/reporting.rb b/activesupport/lib/active_support/deprecation/reporting.rb index 1ce54d9381..a7d265d732 100644 --- a/activesupport/lib/active_support/deprecation/reporting.rb +++ b/activesupport/lib/active_support/deprecation/reporting.rb @@ -11,8 +11,10 @@ module ActiveSupport # # ActiveSupport::Deprecation.warn('something broke!') # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)" - def warn(message = nil, callstack = caller) + def warn(message = nil, callstack = nil) return if silenced + + callstack ||= caller(2) deprecation_message(callstack, message).tap do |m| behavior.each { |b| b.call(m, callstack) } end @@ -34,7 +36,8 @@ module ActiveSupport @silenced = old_silenced end - def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = caller) + def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil) + caller_backtrace ||= caller(2) deprecated_method_warning(deprecated_method_name, message).tap do |msg| warn(msg, caller_backtrace) end diff --git a/activesupport/lib/active_support/json/variable.rb b/activesupport/lib/active_support/json/variable.rb index 42e25fbdb3..d69dab6408 100644 --- a/activesupport/lib/active_support/json/variable.rb +++ b/activesupport/lib/active_support/json/variable.rb @@ -7,7 +7,7 @@ module ActiveSupport def initialize(*args) message = 'ActiveSupport::JSON::Variable is deprecated and will be removed in Rails 4.1. ' \ 'For your own custom JSON literals, define #as_json and #encode_json yourself.' - ActiveSupport::Deprecation.warn(message, caller) + ActiveSupport::Deprecation.warn message super end diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 646e70c29f..8b06739b7f 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -44,7 +44,7 @@ module ActiveSupport super else message = "`describe` without a block is deprecated, please switch to: `def self.name; #{text.inspect}; end`\n" - ActiveSupport::Deprecation.warn(message, caller) + ActiveSupport::Deprecation.warn message class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def self.name diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index c081103cc7..332100f5a1 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -9,7 +9,7 @@ class Deprecatee def old_request; @request end def partially(foo = nil) - ActiveSupport::Deprecation.warn('calling with foo=nil is out', caller) if foo.nil? + ActiveSupport::Deprecation.warn('calling with foo=nil is out') if foo.nil? end def not() 2 end -- cgit v1.2.3