1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Set up subscriber on initialization.

This commit is contained in:
José Valim 2010-01-12 17:48:09 +01:00
parent 7f25ccf38d
commit fe3ceabeed
6 changed files with 74 additions and 15 deletions

View file

@ -104,9 +104,6 @@ module Rails
@app.call(env)
end
# # bail out if gems are missing - note that check_gem_dependencies will have
# # already called abort() unless $gems_rake_task is set
# return unless gems_dependencies_loaded
initializer :load_application_initializers do
Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer|
load(initializer)

View file

@ -128,5 +128,18 @@ module Rails
end
end
end
initializer :initialize_notifications do
require 'active_support/notifications'
if config.colorize_logging == false
Rails::Subscriber.colorize_logging = false
config.generators.colorize_logging = false
end
ActiveSupport::Notifications.subscribe do |*args|
Rails::Subscriber.dispatch(args)
end
end
end
end

View file

@ -64,17 +64,16 @@ module Rails
end
class Configuration < Railtie::Configuration
attr_accessor :after_initialize_blocks, :cache_classes,
:consider_all_requests_local, :dependency_loading, :gems,
attr_accessor :after_initialize_blocks, :cache_classes, :colorize_logging,
:consider_all_requests_local, :dependency_loading,
:load_once_paths, :logger, :metals, :plugins,
:preload_frameworks, :reload_plugins, :serve_static_assets,
:time_zone, :whiny_nils
attr_writer :cache_store, :controller_paths,
:database_configuration_file, :eager_load_paths,
:i18n, :load_paths,
:log_level, :log_path, :paths, :routes_configuration_file,
:view_path
:i18n, :load_paths, :log_level, :log_path, :paths,
:routes_configuration_file, :view_path
def initialize(base = nil)
super
@ -287,10 +286,14 @@ module Rails
end
end
# Allows Notifications queue to be modified.
# Allow Notifications queue to be modified or add subscriptions:
#
# config.notifications.queue = MyNewQueue.new
#
# config.notifications.subscribe /action_dispatch.show_exception/ do |*args|
# ExceptionDeliver.deliver_exception(args)
# end
#
def notifications
ActiveSupport::Notifications
end

View file

@ -52,8 +52,8 @@ module ApplicationTests
config.generators.test_framework :rspec
RUBY
require "#{app_path}/config/environment"
# Initialize the application
require "#{app_path}/config/environment"
require "rails/generators"
Rails::Generators.configure!

View file

@ -12,28 +12,64 @@ module ApplicationTests
end
end
class MockLogger
def method_missing(*args)
@logged ||= []
@logged << args.last
end
def logged
@logged.compact.map { |l| l.to_s.strip }
end
end
class NotificationsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
end
def instrument(*args, &block)
ActiveSupport::Notifications.instrument(*args, &block)
end
def wait
ActiveSupport::Notifications.notifier.wait
end
test "new queue is set" do
# We don't want to load all frameworks, so remove them and clean up environments.
use_frameworks []
FileUtils.rm_rf("#{app_path}/config/environments")
require "active_support/notifications"
@events = []
add_to_config <<-RUBY
config.notifications.notifier = ActiveSupport::Notifications::Notifier.new(ApplicationTests::MyQueue.new)
RUBY
end
test "new queue is set" do
use_frameworks []
require "#{app_path}/config/environment"
assert_raise RuntimeError do
ActiveSupport::Notifications.publish('foo')
end
end
test "rails subscribers are added" do
add_to_config <<-RUBY
config.colorize_logging = false
RUBY
require "#{app_path}/config/environment"
ActiveRecord::Base.logger = logger = MockLogger.new
# Mimic an ActiveRecord notifications
instrument "active_record.sql", :name => "SQL", :sql => "SHOW tables"
wait
assert_equal 1, logger.logged.size
assert_match /SHOW tables/, logger.logged.last
end
end
end

View file

@ -22,7 +22,10 @@ ActiveSupport::Notifications.subscribe do |*args|
end
class MySubscriber < Rails::Subscriber
attr_reader :event
def some_event(event)
@event = event
info event.name
end
@ -85,6 +88,13 @@ class SubscriberTest < ActiveSupport::TestCase
assert_equal %w(my_subscriber.some_event), @logger.logged(:info)
end
def test_event_is_an_active_support_notifications_event
Rails::Subscriber.add :my_subscriber, @subscriber
instrument "my_subscriber.some_event"
wait
assert_kind_of ActiveSupport::Notifications::Event, @subscriber.event
end
def test_does_not_send_the_event_if_it_doesnt_match_the_class
Rails::Subscriber.add :my_subscriber, @subscriber
instrument "my_subscriber.unknown_event"