提交 d2bd71a1 编写于 作者: C Carlhuda

Finish moving config.frameworks-dependent code to the framework plugin

上级 61af34b0
require "action_mailer"
module ActionMailer
class Plugin < Rails::Plugin
plugin_name :action_mailer
initializer "action_mailer.set_configs" do |app|
app.config.action_mailer.each do |k,v|
ActionMailer::Base.send "#{k}=", v
end
end
# TODO: ActionController::Base.logger should delegate to its own config.logger
initializer "action_mailer.logger" do
ActionMailer::Base.logger ||= Rails.logger
end
initializer "action_mailer.view_paths" do |app|
# TODO: this should be combined with the logic for default config.action_mailer.view_paths
view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes)
ActionMailer::Base.template_root = view_path if ActionMailer::Base.view_paths.blank?
end
end
end
\ No newline at end of file
require 'active_support/notifications'
ActiveSupport::Notifications.subscribe(/(read|write|cache|expire|exist)_(fragment|page)\??/) do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
if logger = ActionController::Base.logger
human_name = event.name.to_s.humanize
logger.info("#{human_name} (%.1fms)" % event.duration)
end
end
......@@ -23,5 +23,80 @@ class Plugin < Rails::Plugin
app.route_configuration_files << app.config.builtin_routes_configuration_file
app.reload_routes!
end
# Include middleware to serve up static assets
initializer "action_controller.initialize_static_server" do |app|
if app.config.serve_static_assets
app.config.middleware.use(ActionDispatch::Static, Rails.public_path)
end
end
initializer "action_controller.initialize_middleware_stack" do |app|
middleware = app.config.middleware
middleware.use(::Rack::Lock, :if => lambda { ActionController::Base.allow_concurrency })
middleware.use(::Rack::Runtime)
middleware.use(ActionDispatch::ShowExceptions, lambda { ActionController::Base.consider_all_requests_local })
middleware.use(ActionDispatch::Callbacks, lambda { ActionController::Dispatcher.prepare_each_request })
middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
middleware.use(ActionDispatch::ParamsParser)
middleware.use(::Rack::MethodOverride)
middleware.use(::Rack::Head)
middleware.use(ActionDispatch::StringCoercion)
end
initializer "action_controller.initialize_framework_caches" do
ActionController::Base.cache_store ||= RAILS_CACHE
end
# Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
# (but only for those frameworks that are to be loaded). If the framework's
# paths have already been set, it is not changed, otherwise it is
# set to use Configuration#view_path.
initializer "action_controller.initialize_framework_views" do |app|
# TODO: this should be combined with the logic for default config.action_controller.view_paths
view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes)
ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank?
end
initializer "action_controller.initialize_metal" do |app|
Rails::Rack::Metal.requested_metals = app.config.metals
app.config.middleware.insert_before(:"ActionDispatch::ParamsParser",
Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
end
# # Prepare dispatcher callbacks and run 'prepare' callbacks
initializer "action_controller.prepare_dispatcher" do |app|
# TODO: This used to say unless defined?(Dispatcher). Find out why and fix.
require 'rails/dispatcher'
Dispatcher.define_dispatcher_callbacks(app.config.cache_classes)
unless app.config.cache_classes
# Setup dev mode route reloading
routes_last_modified = app.routes_changed_at
reload_routes = lambda do
unless app.routes_changed_at == routes_last_modified
routes_last_modified = app.routes_changed_at
app.reload_routes!
end
end
ActionDispatch::Callbacks.before_dispatch { |callbacks| reload_routes.call }
end
end
initializer "action_controller.notifications" do |app|
require 'active_support/notifications'
ActiveSupport::Notifications.subscribe(/(read|write|cache|expire|exist)_(fragment|page)\??/) do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
if logger = ActionController::Base.logger
human_name = event.name.to_s.humanize
logger.info("#{human_name} (%.1fms)" % event.duration)
end
end
end
end
end
\ No newline at end of file
require 'active_support/notifications'
ActiveSupport::Notifications.subscribe("sql") do |name, before, after, result, instrumenter_id, payload|
ActiveRecord::Base.connection.log_info(payload[:sql], name, after - before)
end
......@@ -47,5 +47,13 @@ class Plugin < Rails::Plugin
ActiveRecord::Base.logger ||= Rails.logger
end
initializer "active_record.notifications" do
require 'active_support/notifications'
ActiveSupport::Notifications.subscribe("sql") do |name, before, after, result, instrumenter_id, payload|
ActiveRecord::Base.connection.log_info(payload[:sql], name, after - before)
end
end
end
end
\ No newline at end of file
......@@ -139,14 +139,6 @@ def call(env)
$LOAD_PATH.uniq!
end
# # Requires all frameworks specified by the Configuration#frameworks
# # list. By default, all frameworks (Active Record, Active Support,
# # Action Pack, Action Mailer, and Active Resource) are loaded.
# initializer :require_frameworks do
# require 'active_support/all' unless config.active_support.bare
# config.frameworks.each { |framework| require(framework.to_s) }
# end
# Set the paths from which Rails will automatically load source files, and
# the load_once paths.
initializer :set_autoload_paths do
......@@ -177,34 +169,7 @@ def call(env)
# Used by Passenger to ensure everything's loaded before forking and
# to avoid autoload race conditions in JRuby.
initializer :preload_frameworks do
if config.preload_frameworks
config.frameworks.each do |framework|
# String#classify and #constantize aren't available yet.
toplevel = Object.const_get(framework.to_s.gsub(/(?:^|_)(.)/) { $1.upcase })
toplevel.load_all! if toplevel.respond_to?(:load_all!)
end
end
end
# Include middleware to serve up static assets
initializer :initialize_static_server do
if defined?(ActionController) && config.serve_static_assets
config.middleware.use(ActionDispatch::Static, Rails.public_path)
end
end
initializer :initialize_middleware_stack do
if defined?(ActionController)
config.middleware.use(::Rack::Lock, :if => lambda { ActionController::Base.allow_concurrency })
config.middleware.use(::Rack::Runtime)
config.middleware.use(ActionDispatch::ShowExceptions, lambda { ActionController::Base.consider_all_requests_local })
config.middleware.use(ActionDispatch::Callbacks, lambda { ActionController::Dispatcher.prepare_each_request })
config.middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
config.middleware.use(ActionDispatch::ParamsParser)
config.middleware.use(::Rack::MethodOverride)
config.middleware.use(::Rack::Head)
config.middleware.use(ActionDispatch::StringCoercion)
end
ActiveSupport::Autoload.eager_load! if config.preload_frameworks
end
initializer :initialize_cache do
......@@ -218,12 +183,6 @@ def call(env)
end
end
initializer :initialize_framework_caches do
if defined?(ActionController)
ActionController::Base.cache_store ||= RAILS_CACHE
end
end
initializer :initialize_logger do
# if the environment has explicitly defined a logger, use it
next if Rails.logger
......@@ -254,14 +213,6 @@ def call(env)
# logger is already set, it is not changed, otherwise it is set to use
# RAILS_DEFAULT_LOGGER.
initializer :initialize_framework_logging do
for framework in [ :action_controller, :action_mailer ]
# TODO BEFORE PUSHING: REMOVEZ
begin
framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
rescue Exception
end
end
ActiveSupport::Dependencies.logger ||= Rails.logger
Rails.cache.logger ||= Rails.logger
end
......@@ -308,46 +259,6 @@ def call(env)
end
end
# Initializes framework-specific settings for each of the loaded frameworks
# (Configuration#frameworks). The available settings map to the accessors
# on each of the corresponding Base classes.
initializer :initialize_framework_settings do
(config.frameworks - [:active_record, :action_controller]).each do |framework|
# TODO BEFORE PUSHING: This needs to work differently
begin
base_class = framework.to_s.camelize.constantize.const_get("Base")
config.send(framework).each do |setting, value|
base_class.send("#{setting}=", value)
end
rescue Exception
end
end
end
# Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
# (but only for those frameworks that are to be loaded). If the framework's
# paths have already been set, it is not changed, otherwise it is
# set to use Configuration#view_path.
initializer :initialize_framework_views do
if defined?(ActionView)
view_path = ActionView::PathSet.type_cast(config.view_path, config.cache_classes)
ActionMailer::Base.template_root = view_path if defined?(ActionMailer) && ActionMailer::Base.view_paths.blank?
ActionController::Base.view_paths = view_path if defined?(ActionController) && ActionController::Base.view_paths.blank?
end
end
initializer :initialize_metal do
if defined?(ActionController)
Rails::Rack::Metal.requested_metals = config.metals
config.middleware.insert_before(
:"ActionDispatch::ParamsParser",
Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
end
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
......@@ -364,27 +275,6 @@ def call(env)
end
end
# TODO: Make a DSL way to limit an initializer to a particular framework
# # Prepare dispatcher callbacks and run 'prepare' callbacks
initializer :prepare_dispatcher do
next unless configuration.frameworks.include?(:action_controller)
require 'rails/dispatcher' unless defined?(::Dispatcher)
Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
unless configuration.cache_classes
# Setup dev mode route reloading
routes_last_modified = routes_changed_at
reload_routes = lambda do
unless routes_changed_at == routes_last_modified
routes_last_modified = routes_changed_at
reload_routes!
end
end
ActionDispatch::Callbacks.before_dispatch { |callbacks| reload_routes.call }
end
end
# Eager load application classes
initializer :load_application_classes do
next if $rails_rake_task
......
......@@ -171,6 +171,8 @@ def baz
end
RUBY
sleep 0.1
get '/foo'
assert_equal 'baz', last_response.body
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册