mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
defines a new :abort deprecation behaviour that raises
See the CHANGELONG message in the patch for further details.
This commit is contained in:
parent
1577d9661a
commit
73aad75f56
3 changed files with 46 additions and 5 deletions
|
@ -1,3 +1,15 @@
|
|||
* Adds a new deprecation behaviour that aborts the application. Throwing this
|
||||
line into +config/environments/development.rb+
|
||||
|
||||
ActiveSupport::Deprecation.behavior = :abort
|
||||
|
||||
will cause the application to raise an +ActiveSupport::DeprecationException+
|
||||
on deprecations.
|
||||
|
||||
Use this for agressive deprecation cleanups.
|
||||
|
||||
*Xavier Noria*
|
||||
|
||||
* Remove 'cow' => 'kine' irregular inflection from default inflections.
|
||||
|
||||
*Andrew White*
|
||||
|
|
|
@ -1,14 +1,24 @@
|
|||
require "active_support/notifications"
|
||||
|
||||
module ActiveSupport
|
||||
class DeprecationException < StandardError
|
||||
end
|
||||
|
||||
class Deprecation
|
||||
# Default warning behaviors per Rails.env.
|
||||
DEFAULT_BEHAVIORS = {
|
||||
:stderr => Proc.new { |message, callstack|
|
||||
abort: ->(message, callstack) {
|
||||
e = DeprecationException.new(message)
|
||||
e.set_backtrace(callstack)
|
||||
raise e
|
||||
},
|
||||
|
||||
stderr: ->(message, callstack) {
|
||||
$stderr.puts(message)
|
||||
$stderr.puts callstack.join("\n ") if debug
|
||||
},
|
||||
:log => Proc.new { |message, callstack|
|
||||
|
||||
log: ->(message, callstack) {
|
||||
logger =
|
||||
if defined?(Rails) && Rails.logger
|
||||
Rails.logger
|
||||
|
@ -19,11 +29,13 @@ module ActiveSupport
|
|||
logger.warn message
|
||||
logger.debug callstack.join("\n ") if debug
|
||||
},
|
||||
:notify => Proc.new { |message, callstack|
|
||||
|
||||
notify: ->(message, callstack) {
|
||||
ActiveSupport::Notifications.instrument("deprecation.rails",
|
||||
:message => message, :callstack => callstack)
|
||||
},
|
||||
:silence => Proc.new { |message, callstack| }
|
||||
|
||||
silence: ->(message, callstack) {},
|
||||
}
|
||||
|
||||
module Behavior
|
||||
|
@ -40,6 +52,7 @@ module ActiveSupport
|
|||
#
|
||||
# Available behaviors:
|
||||
#
|
||||
# [+abort+] Raises <tt>ActiveSupport::DeprecationException</tt>.
|
||||
# [+stderr+] Log all deprecation warnings to +$stderr+.
|
||||
# [+log+] Log all deprecation warnings to +Rails.logger+.
|
||||
# [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
|
||||
|
@ -52,7 +65,7 @@ module ActiveSupport
|
|||
# ActiveSupport::Deprecation.behavior = :stderr
|
||||
# ActiveSupport::Deprecation.behavior = [:stderr, :log]
|
||||
# ActiveSupport::Deprecation.behavior = MyCustomHandler
|
||||
# ActiveSupport::Deprecation.behavior = proc { |message, callstack|
|
||||
# ActiveSupport::Deprecation.behavior = ->(message, callstack) {
|
||||
# # custom stuff
|
||||
# }
|
||||
def behavior=(behavior)
|
||||
|
|
|
@ -98,6 +98,22 @@ class DeprecationTest < ActiveSupport::TestCase
|
|||
assert_match(/foo=nil/, @b)
|
||||
end
|
||||
|
||||
def test_abort_behaviour
|
||||
ActiveSupport::Deprecation.behavior = :abort
|
||||
|
||||
message = 'Revise this deprecated stuff now!'
|
||||
callstack = %w(foo bar baz)
|
||||
|
||||
begin
|
||||
ActiveSupport::Deprecation.behavior.first.call(message, callstack)
|
||||
rescue ActiveSupport::DeprecationException => e
|
||||
assert_equal message, e.message
|
||||
assert_equal callstack, e.backtrace
|
||||
else
|
||||
flunk 'the :abort deprecation behaviour should raise the expected exception'
|
||||
end
|
||||
end
|
||||
|
||||
def test_default_stderr_behavior
|
||||
ActiveSupport::Deprecation.behavior = :stderr
|
||||
behavior = ActiveSupport::Deprecation.behavior.first
|
||||
|
|
Loading…
Reference in a new issue