Skip to content
Browse files

Create a deprecation behavior that triggers a notification for deprec…

…ation notices, and make the behaviors independent of the environment names.

* In Rails 2.3 apps being upgraded, you will need to add the deprecation
  configuration to each of your environments. Failing to do so will
  result in the same behavior as Rails 2.3, but with an outputted warning
  to provide information on how to set up the setting.
* New Rails 3 applications generate the setting
* The notification style will send deprecation notices using
  ActiveSupport::Notifications. Third-party tools can listen in to
  these notifications to provide a streamlined view of the
  deprecation notices occurring in your app.
* The payload in the notification is the deprecation warning itself
  as well as the callstack from the point that triggered the
  notification.
  • Loading branch information...
1 parent 21957b7 commit d4c7d3fd94e5a885a6366eaeb3b908bb58ffd4db @wycats wycats committed Jun 29, 2010
View
21 activesupport/lib/active_support/deprecation/behaviors.rb
@@ -1,28 +1,27 @@
+require "active_support/notifications"
+
module ActiveSupport
module Deprecation
class << self
- # Behavior is a block that takes a message argument.
- attr_writer :behavior
-
# Whether to print a backtrace along with the warning.
attr_accessor :debug
def behavior
- @behavior ||= default_behavior
+ @behavior ||= DEFAULT_BEHAVIORS[:stderr]
end
- def default_behavior
- Deprecation::DEFAULT_BEHAVIORS[defined?(Rails.env) ? Rails.env.to_s : 'test']
+ def behavior=(behavior)
+ @behavior = DEFAULT_BEHAVIORS[behavior] || behavior
end
end
- # Default warning behaviors per Rails.env. Ignored in production.
+ # Default warning behaviors per Rails.env.
DEFAULT_BEHAVIORS = {
- 'test' => Proc.new { |message, callstack|
+ :stderr => Proc.new { |message, callstack|
$stderr.puts(message)
$stderr.puts callstack.join("\n ") if debug
},
- 'development' => Proc.new { |message, callstack|
+ :log => Proc.new { |message, callstack|
logger =
if defined?(Rails) && Rails.logger
Rails.logger
@@ -32,6 +31,10 @@ def default_behavior
end
logger.warn message
logger.debug callstack.join("\n ") if debug
+ },
+ :notify => Proc.new { |message, callstack|
+ ActiveSupport::Notifications.instrument("deprecation.rails",
+ :message => message, :callstack => callstack)
}
}
end
View
28 activesupport/lib/active_support/railtie.rb
@@ -12,6 +12,34 @@ class Railtie < Rails::Railtie
require 'active_support/whiny_nil' if app.config.whiny_nils
end
+ initializer "active_support.deprecation_behavior" do |app|
+ if deprecation = app.config.active_support.deprecation
+ ActiveSupport::Deprecation.behavior = deprecation
+ else
+ defaults = {"development" => :log,
+ "production" => :notify,
+ "test" => :stderr}
+
+ env = Rails.env
+
+ if defaults.key?(env)
+ msg = "You did not specify how you would like Rails to report " \
+ "deprecation notices for your #{env} environment, please " \
+ "set it to :#{defaults[env]} at config/environments/#{env}.rb"
+
+ warn msg
+ ActiveSupport::Deprecation.behavior = defaults[env]
+ else
+ msg = "You did not specify how you would like Rails to report " \
+ "deprecation notices for your #{env} environment, please " \
+ "set it to :log, :notify or :stderr at config/environments/#{env}.rb"
+
+ warn msg
+ ActiveSupport::Deprecation.behavior = :stderr
+ end
+ end
+ end
+
# Sets the default value for Time.zone
# If assigned value cannot be matched to a TimeZone, an exception will be raised.
initializer "active_support.initialize_time_zone" do |app|
View
3 railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
@@ -16,4 +16,7 @@
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
+
+ # Print deprecation notices to the Rails logger
+ config.active_support.deprecation = :log
end
View
3 railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
@@ -43,4 +43,7 @@
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
+
+ # Send deprecation notices to registered listeners
+ config.active_support.deprecation = :notification
@m4n
m4n added a note Jun 29, 2010

Shouldn't this be :notify?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
View
3 railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
@@ -29,4 +29,7 @@
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
+
+ # Print deprecation notices to the stderr
+ config.active_support.deprecation = :stderr
end
View
1 railties/test/application/url_generation_test.rb
@@ -16,6 +16,7 @@ def app
class MyApp < Rails::Application
config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
config.session_store :cookie_store, :key => "_myapp_session"
+ config.active_support.deprecation = :log
end
MyApp.initialize!
View
3 railties/test/isolation/abstract_unit.rb
@@ -100,7 +100,7 @@ def build_app(options = {})
end
end
- add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"'
+ add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"; config.active_support.deprecation = :log'
end
def make_basic_app
@@ -110,6 +110,7 @@ def make_basic_app
app = Class.new(Rails::Application)
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
app.config.session_store :cookie_store, :key => "_myapp_session"
+ app.config.active_support.deprecation = :log
yield app if block_given?
app.initialize!

0 comments on commit d4c7d3f

Please sign in to comment.
Something went wrong with that request. Please try again.