mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Moving out some framework specific initializers into the framework libraries.
This commit is contained in:
parent
fa8dfc7d01
commit
38aeb1528c
8 changed files with 142 additions and 97 deletions
2
Gemfile
2
Gemfile
|
@ -31,3 +31,5 @@ if ENV['CI']
|
||||||
gem "test-unit", ">= 2.0.5"
|
gem "test-unit", ">= 2.0.5"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
disable_system_gems
|
27
actionpack/lib/action_controller/rails.rb
Normal file
27
actionpack/lib/action_controller/rails.rb
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
module ActionController
|
||||||
|
class Plugin < Rails::Plugin
|
||||||
|
plugin_name :action_controller
|
||||||
|
|
||||||
|
initializer "action_controller.set_configs" do |app|
|
||||||
|
app.config.action_controller.each do |k,v|
|
||||||
|
ActionController::Base.send "#{k}=", v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: ActionController::Base.logger should delegate to its own config.logger
|
||||||
|
initializer "action_controller.logger" do
|
||||||
|
ActionController::Base.logger ||= Rails.logger
|
||||||
|
end
|
||||||
|
|
||||||
|
# Routing must be initialized after plugins to allow the former to extend the routes
|
||||||
|
# ---
|
||||||
|
# If Action Controller is not one of the loaded frameworks (Configuration#frameworks)
|
||||||
|
# this does nothing. Otherwise, it loads the routing definitions and sets up
|
||||||
|
# loading module used to lazily load controllers (Configuration#controller_paths).
|
||||||
|
initializer "action_controller.initialize_routing" do |app|
|
||||||
|
app.route_configuration_files << app.config.routes_configuration_file
|
||||||
|
app.route_configuration_files << app.config.builtin_routes_configuration_file
|
||||||
|
app.reload_routes!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
51
activerecord/lib/active_record/rails.rb
Normal file
51
activerecord/lib/active_record/rails.rb
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
# For now, action_controller must always be present with
|
||||||
|
# rails, so let's make sure that it gets required before
|
||||||
|
# here. This is needed for correctly setting up the middleware.
|
||||||
|
# In the future, this might become an optional require.
|
||||||
|
require "action_controller/rails"
|
||||||
|
|
||||||
|
module ActiveRecord
|
||||||
|
class Plugin < Rails::Plugin
|
||||||
|
plugin_name :active_record
|
||||||
|
|
||||||
|
initializer "active_record.set_configs" do |app|
|
||||||
|
app.config.active_record.each do |k,v|
|
||||||
|
ActiveRecord::Base.send "#{k}=", v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# This sets the database configuration from Configuration#database_configuration
|
||||||
|
# and then establishes the connection.
|
||||||
|
initializer "active_record.initialize_database" do |app|
|
||||||
|
ActiveRecord::Base.configurations = app.config.database_configuration
|
||||||
|
ActiveRecord::Base.establish_connection
|
||||||
|
end
|
||||||
|
|
||||||
|
initializer "active_record.initialize_timezone" do
|
||||||
|
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||||
|
ActiveRecord::Base.default_timezone = :utc
|
||||||
|
end
|
||||||
|
|
||||||
|
# Setup database middleware after initializers have run
|
||||||
|
initializer "active_record.initialize_database_middleware" do |app|
|
||||||
|
middleware = app.config.middleware
|
||||||
|
if middleware.include?(ActiveRecord::SessionStore)
|
||||||
|
middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::ConnectionAdapters::ConnectionManagement
|
||||||
|
middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::QueryCache
|
||||||
|
else
|
||||||
|
middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
||||||
|
middleware.use ActiveRecord::QueryCache
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
initializer "active_record.load_observers" do
|
||||||
|
ActiveRecord::Base.instantiate_observers
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: ActiveRecord::Base.logger should delegate to its own config.logger
|
||||||
|
initializer "active_record.logger" do
|
||||||
|
ActiveRecord::Base.logger ||= Rails.logger
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -21,11 +21,7 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
def config
|
def config
|
||||||
@config ||= begin
|
@config ||= Configuration.new(Plugin::Configuration.default)
|
||||||
config = Configuration.new
|
|
||||||
Plugin.plugins.each { |p| config.merge(p.config) }
|
|
||||||
config
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: change the plugin loader to use config
|
# TODO: change the plugin loader to use config
|
||||||
|
@ -122,10 +118,11 @@ module Rails
|
||||||
initializers
|
initializers
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Fix this method
|
||||||
def plugins
|
def plugins
|
||||||
@plugins ||= begin
|
@plugins ||= begin
|
||||||
plugin_names = config.plugins || [:all]
|
plugin_names = config.plugins || [:all]
|
||||||
Plugin.plugins.select { |p| plugin_names.include?(p.plugin_name) } +
|
Plugin.plugins.select { |p| plugin_names.include?(:all) || plugin_names.include?(p.plugin_name) } +
|
||||||
Plugin::Vendored.all(config.plugins || [:all], config.paths.vendor.plugins)
|
Plugin::Vendored.all(config.plugins || [:all], config.paths.vendor.plugins)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -189,20 +186,9 @@ module Rails
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# This initialization routine does nothing unless <tt>:active_record</tt>
|
|
||||||
# is one of the frameworks to load (Configuration#frameworks). If it is,
|
|
||||||
# this sets the database configuration from Configuration#database_configuration
|
|
||||||
# and then establishes the connection.
|
|
||||||
initializer :initialize_database do
|
|
||||||
if defined?(ActiveRecord)
|
|
||||||
ActiveRecord::Base.configurations = config.database_configuration
|
|
||||||
ActiveRecord::Base.establish_connection
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Include middleware to serve up static assets
|
# Include middleware to serve up static assets
|
||||||
initializer :initialize_static_server do
|
initializer :initialize_static_server do
|
||||||
if config.frameworks.include?(:action_controller) && config.serve_static_assets
|
if defined?(ActionController) && config.serve_static_assets
|
||||||
config.middleware.use(ActionDispatch::Static, Rails.public_path)
|
config.middleware.use(ActionDispatch::Static, Rails.public_path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -268,7 +254,7 @@ module Rails
|
||||||
# logger is already set, it is not changed, otherwise it is set to use
|
# logger is already set, it is not changed, otherwise it is set to use
|
||||||
# RAILS_DEFAULT_LOGGER.
|
# RAILS_DEFAULT_LOGGER.
|
||||||
initializer :initialize_framework_logging do
|
initializer :initialize_framework_logging do
|
||||||
for framework in [ :active_record, :action_controller, :action_mailer ]
|
for framework in [ :action_controller, :action_mailer ]
|
||||||
# TODO BEFORE PUSHING: REMOVEZ
|
# TODO BEFORE PUSHING: REMOVEZ
|
||||||
begin
|
begin
|
||||||
framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
|
framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
|
||||||
|
@ -293,7 +279,7 @@ module Rails
|
||||||
require('active_support/whiny_nil') if config.whiny_nils
|
require('active_support/whiny_nil') if config.whiny_nils
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
|
# Sets the default value for Time.zone
|
||||||
# If assigned value cannot be matched to a TimeZone, an exception will be raised.
|
# If assigned value cannot be matched to a TimeZone, an exception will be raised.
|
||||||
initializer :initialize_time_zone do
|
initializer :initialize_time_zone do
|
||||||
if config.time_zone
|
if config.time_zone
|
||||||
|
@ -307,11 +293,6 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
Time.zone_default = zone_default
|
Time.zone_default = zone_default
|
||||||
|
|
||||||
if defined?(ActiveRecord)
|
|
||||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
|
||||||
ActiveRecord::Base.default_timezone = :utc
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -331,7 +312,7 @@ module Rails
|
||||||
# (Configuration#frameworks). The available settings map to the accessors
|
# (Configuration#frameworks). The available settings map to the accessors
|
||||||
# on each of the corresponding Base classes.
|
# on each of the corresponding Base classes.
|
||||||
initializer :initialize_framework_settings do
|
initializer :initialize_framework_settings do
|
||||||
config.frameworks.each do |framework|
|
(config.frameworks - [:active_record, :action_controller]).each do |framework|
|
||||||
# TODO BEFORE PUSHING: This needs to work differently
|
# TODO BEFORE PUSHING: This needs to work differently
|
||||||
begin
|
begin
|
||||||
base_class = framework.to_s.camelize.constantize.const_get("Base")
|
base_class = framework.to_s.camelize.constantize.const_get("Base")
|
||||||
|
@ -383,20 +364,6 @@ module Rails
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# # Setup database middleware after initializers have run
|
|
||||||
initializer :initialize_database_middleware do
|
|
||||||
if defined?(ActiveRecord)
|
|
||||||
if defined?(ActionController) && ActionController::Base.session_store &&
|
|
||||||
ActionController::Base.session_store.name == 'ActiveRecord::SessionStore'
|
|
||||||
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
|
|
||||||
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
|
|
||||||
else
|
|
||||||
configuration.middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
|
||||||
configuration.middleware.use ActiveRecord::QueryCache
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: Make a DSL way to limit an initializer to a particular framework
|
# TODO: Make a DSL way to limit an initializer to a particular framework
|
||||||
|
|
||||||
# # Prepare dispatcher callbacks and run 'prepare' callbacks
|
# # Prepare dispatcher callbacks and run 'prepare' callbacks
|
||||||
|
@ -418,25 +385,6 @@ module Rails
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Routing must be initialized after plugins to allow the former to extend the routes
|
|
||||||
# ---
|
|
||||||
# If Action Controller is not one of the loaded frameworks (Configuration#frameworks)
|
|
||||||
# this does nothing. Otherwise, it loads the routing definitions and sets up
|
|
||||||
# loading module used to lazily load controllers (Configuration#controller_paths).
|
|
||||||
initializer :initialize_routing do
|
|
||||||
next unless configuration.frameworks.include?(:action_controller)
|
|
||||||
route_configuration_files << configuration.routes_configuration_file
|
|
||||||
route_configuration_files << configuration.builtin_routes_configuration_file
|
|
||||||
reload_routes!
|
|
||||||
end
|
|
||||||
#
|
|
||||||
# # Observers are loaded after plugins in case Observers or observed models are modified by plugins.
|
|
||||||
initializer :load_observers do
|
|
||||||
if defined?(ActiveRecord)
|
|
||||||
ActiveRecord::Base.instantiate_observers
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Eager load application classes
|
# Eager load application classes
|
||||||
initializer :load_application_classes do
|
initializer :load_application_classes do
|
||||||
next if $rails_rake_task
|
next if $rails_rake_task
|
||||||
|
|
|
@ -5,22 +5,26 @@ module Rails
|
||||||
# configuration class while this bit is being cleaned up.
|
# configuration class while this bit is being cleaned up.
|
||||||
class Plugin::Configuration
|
class Plugin::Configuration
|
||||||
|
|
||||||
def initialize
|
def self.default
|
||||||
@options = Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new }
|
@default ||= new
|
||||||
end
|
end
|
||||||
|
|
||||||
def middleware
|
attr_reader :middleware
|
||||||
@middleware ||= ActionDispatch::MiddlewareStack.new
|
|
||||||
|
def initialize(base = nil)
|
||||||
|
if base
|
||||||
|
@options = base.options.dup
|
||||||
|
@middleware = base.middleware.dup
|
||||||
|
else
|
||||||
|
@options = Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new }
|
||||||
|
@middleware = ActionDispatch::MiddlewareStack.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def respond_to?(name)
|
def respond_to?(name)
|
||||||
super || name.to_s =~ config_key_regexp
|
super || name.to_s =~ config_key_regexp
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge(config)
|
|
||||||
@options = config.options.merge(@options)
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
attr_reader :options
|
attr_reader :options
|
||||||
|
@ -41,8 +45,7 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
def config_keys
|
def config_keys
|
||||||
([ :active_support, :active_record, :action_controller,
|
([ :active_support, :action_view, :action_mailer, :active_resource ] +
|
||||||
:action_view, :action_mailer, :active_resource ] +
|
|
||||||
Plugin.plugin_names).map { |n| n.to_s }.uniq
|
Plugin.plugin_names).map { |n| n.to_s }.uniq
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -60,7 +63,7 @@ module Rails
|
||||||
:log_level, :log_path, :paths, :routes_configuration_file,
|
:log_level, :log_path, :paths, :routes_configuration_file,
|
||||||
:view_path
|
:view_path
|
||||||
|
|
||||||
def initialize
|
def initialize(base = nil)
|
||||||
super
|
super
|
||||||
@load_once_paths = []
|
@load_once_paths = []
|
||||||
@after_initialize_blocks = []
|
@after_initialize_blocks = []
|
||||||
|
|
|
@ -2,8 +2,10 @@ module Rails
|
||||||
class Plugin
|
class Plugin
|
||||||
include Initializable
|
include Initializable
|
||||||
|
|
||||||
def self.plugin_name
|
def self.plugin_name(plugin_name = nil)
|
||||||
@plugin_name || name.demodulize.underscore
|
@plugin_name ||= name.demodulize.underscore
|
||||||
|
@plugin_name = plugin_name if plugin_name
|
||||||
|
@plugin_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.inherited(klass)
|
def self.inherited(klass)
|
||||||
|
@ -20,7 +22,7 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.config
|
def self.config
|
||||||
@config ||= Configuration.new
|
Configuration.default
|
||||||
end
|
end
|
||||||
|
|
||||||
class Vendored < Plugin
|
class Vendored < Plugin
|
||||||
|
|
|
@ -135,21 +135,9 @@ module ApplicationTests
|
||||||
assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
|
assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "database middleware doesn't initialize when activerecord is not in frameworks" do
|
|
||||||
add_to_config <<-RUBY
|
|
||||||
config.root = "#{app_path}"
|
|
||||||
config.frameworks = []
|
|
||||||
RUBY
|
|
||||||
require "#{app_path}/config/environment"
|
|
||||||
|
|
||||||
assert_equal [], Rails.application.config.middleware
|
|
||||||
end
|
|
||||||
|
|
||||||
test "database middleware initializes when session store is active record" do
|
test "database middleware initializes when session store is active record" do
|
||||||
add_to_config <<-RUBY
|
add_to_config "config.action_controller.session_store = :active_record_store"
|
||||||
config.root = "#{app_path}"
|
|
||||||
config.action_controller.session_store = :active_record_store
|
|
||||||
RUBY
|
|
||||||
require "#{app_path}/config/environment"
|
require "#{app_path}/config/environment"
|
||||||
|
|
||||||
expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
|
expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
|
||||||
|
@ -157,16 +145,6 @@ module ApplicationTests
|
||||||
assert_equal expects, middleware & expects
|
assert_equal expects, middleware & expects
|
||||||
end
|
end
|
||||||
|
|
||||||
test "ensure database middleware doesn't use action_controller on initializing" do
|
|
||||||
add_to_config <<-RUBY
|
|
||||||
config.root = "#{app_path}"
|
|
||||||
config.frameworks -= [:action_controller]
|
|
||||||
config.action_controller.session_store = :active_record_store
|
|
||||||
RUBY
|
|
||||||
require "#{app_path}/config/environment"
|
|
||||||
|
|
||||||
assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
|
|
||||||
end
|
|
||||||
test "Rails.root should be a Pathname" do
|
test "Rails.root should be a Pathname" do
|
||||||
add_to_config <<-RUBY
|
add_to_config <<-RUBY
|
||||||
config.root = "#{app_path}"
|
config.root = "#{app_path}"
|
||||||
|
@ -175,4 +153,20 @@ module ApplicationTests
|
||||||
assert_instance_of Pathname, Rails.root
|
assert_instance_of Pathname, Rails.root
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class InitializerCustomFrameworkExtensionsTest < Test::Unit::TestCase
|
||||||
|
include ActiveSupport::Testing::Isolation
|
||||||
|
|
||||||
|
def setup
|
||||||
|
build_app
|
||||||
|
boot_rails
|
||||||
|
end
|
||||||
|
|
||||||
|
test "database middleware doesn't initialize when activerecord is not in frameworks" do
|
||||||
|
use_frameworks []
|
||||||
|
require "#{app_path}/config/environment"
|
||||||
|
|
||||||
|
assert !defined?(ActiveRecord)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
18
railties/test/plugins/framework_extension_test.rb
Normal file
18
railties/test/plugins/framework_extension_test.rb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
require "isolation/abstract_unit"
|
||||||
|
|
||||||
|
module PluginsTest
|
||||||
|
class FrameworkExtensionTest < Test::Unit::TestCase
|
||||||
|
def setup
|
||||||
|
build_app
|
||||||
|
boot_rails
|
||||||
|
end
|
||||||
|
|
||||||
|
test "active_record extensions are applied to ActiveRecord" do
|
||||||
|
add_to_config "config.active_record.table_name_prefix = 'tbl_'"
|
||||||
|
|
||||||
|
require "#{app_path}/config/environment"
|
||||||
|
|
||||||
|
assert_equal 'tbl_', ActiveRecord::Base.table_name_prefix
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue