mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Get rid of initializers global and create i18n railtie.
This commit is contained in:
parent
c8cc8a9872
commit
98240c49b0
14 changed files with 262 additions and 331 deletions
|
@ -51,6 +51,8 @@ module ActionController
|
|||
|
||||
included do
|
||||
# Set the default directory for helpers
|
||||
# TODO This should support multiple directories in order
|
||||
# to work with engines
|
||||
extlib_inheritable_accessor(:helpers_dir) do
|
||||
defined?(Rails.root) ? "#{Rails.root}/app/helpers" : "app/helpers"
|
||||
end
|
||||
|
|
|
@ -1,6 +1,35 @@
|
|||
require "active_support"
|
||||
require "rails"
|
||||
|
||||
module I18n
|
||||
class Railtie < Rails::Railtie
|
||||
plugin_name :i18n
|
||||
|
||||
# Initialize I18n load paths to an array
|
||||
config.i18n.load_path = []
|
||||
|
||||
initializer :initialize_i18n do
|
||||
require 'active_support/i18n'
|
||||
|
||||
ActionDispatch::Callbacks.to_prepare do
|
||||
I18n.reload!
|
||||
end
|
||||
end
|
||||
|
||||
# Set the i18n configuration from config.i18n but special-case for
|
||||
# the load_path which should be appended to what's already set instead of overwritten.
|
||||
config.after_initialize do |app|
|
||||
app.config.i18n.each do |setting, value|
|
||||
if setting == :load_path
|
||||
I18n.load_path += value
|
||||
else
|
||||
I18n.send("#{setting}=", value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module ActiveSupport
|
||||
class Railtie < Rails::Railtie
|
||||
plugin_name :active_support
|
||||
|
|
|
@ -19,7 +19,7 @@ require 'rails/all'
|
|||
# To pick the frameworks you want, remove 'require "rails/all"'
|
||||
# and list the framework railties that you want:
|
||||
#
|
||||
# require "active_model/railtie"
|
||||
# require "active_support/railtie"
|
||||
# require "active_record/railtie"
|
||||
# require "action_controller/railtie"
|
||||
# require "action_view/railtie"
|
||||
|
@ -28,7 +28,7 @@ require 'rails/all'
|
|||
<% else -%>
|
||||
# Pick the frameworks you want:
|
||||
# require "active_record/railtie"
|
||||
require "active_model/railtie"
|
||||
require "active_support/railtie"
|
||||
require "action_controller/railtie"
|
||||
require "action_view/railtie"
|
||||
require "action_mailer/railtie"
|
||||
|
|
|
@ -8,6 +8,7 @@ require 'rails/initializable'
|
|||
require 'rails/railtie'
|
||||
require 'rails/plugin'
|
||||
require 'rails/engine'
|
||||
require 'rails/bootstrap'
|
||||
require 'rails/application'
|
||||
require 'rails/railties_path'
|
||||
require 'rails/version'
|
||||
|
@ -33,8 +34,6 @@ else
|
|||
end
|
||||
|
||||
module Rails
|
||||
autoload :Bootstrap, 'rails/bootstrap'
|
||||
|
||||
class << self
|
||||
def application
|
||||
@@application ||= nil
|
||||
|
|
|
@ -2,6 +2,11 @@ require 'fileutils'
|
|||
|
||||
module Rails
|
||||
class Application < Engine
|
||||
|
||||
# TODO Clear up 2 way delegation flow between App class and instance.
|
||||
# Infact just add a method_missing on the class.
|
||||
#
|
||||
# TODO I'd like to track the "default app" different using an inherited hook.
|
||||
class << self
|
||||
alias :configure :class_eval
|
||||
delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance
|
||||
|
@ -70,10 +75,9 @@ module Rails
|
|||
routes.disable_clear_and_finalize = false
|
||||
end
|
||||
|
||||
|
||||
def require_environment
|
||||
require config.environment_path
|
||||
rescue LoadError
|
||||
environment = config.paths.config.environment.to_a.first
|
||||
require environment if environment
|
||||
end
|
||||
|
||||
def load_tasks
|
||||
|
@ -92,13 +96,6 @@ module Rails
|
|||
plugins.each { |p| p.load_generators }
|
||||
end
|
||||
|
||||
def initializers
|
||||
initializers = Bootstrap.new(self).initializers
|
||||
plugins.each { |p| initializers += p.initializers }
|
||||
initializers += super
|
||||
initializers
|
||||
end
|
||||
|
||||
# TODO: Fix this method. It loads all railties independent if :all is given
|
||||
# or not, otherwise frameworks are never loaded.
|
||||
def plugins
|
||||
|
@ -120,59 +117,30 @@ module Rails
|
|||
app.call(env)
|
||||
end
|
||||
|
||||
initializer :add_builtin_route, :before => :build_middleware_stack do |app|
|
||||
def initializers
|
||||
my = super
|
||||
hook = my.index { |i| i.name == :set_autoload_paths } + 1
|
||||
initializers = Bootstrap.new(self).initializers
|
||||
initializers += my[0...hook]
|
||||
plugins.each { |p| initializers += p.initializers }
|
||||
initializers += my[hook..-1]
|
||||
initializers
|
||||
end
|
||||
|
||||
initializer :add_builtin_route do |app|
|
||||
if Rails.env.development?
|
||||
app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
|
||||
end
|
||||
end
|
||||
|
||||
initializer :build_middleware_stack, :after => :load_application_initializers do
|
||||
initializer :build_middleware_stack do
|
||||
app
|
||||
end
|
||||
|
||||
# Fires the user-supplied after_initialize block (Configuration#after_initialize)
|
||||
initializer :after_initialize, :after => :build_middleware_stack do
|
||||
# Fires the user-supplied after_initialize block (config#after_initialize)
|
||||
initializer :after_initialize do
|
||||
config.after_initialize_blocks.each do |block|
|
||||
block.call
|
||||
end
|
||||
end
|
||||
|
||||
# Set the i18n configuration from config.i18n but special-case for the load_path which should be
|
||||
# appended to what's already set instead of overwritten.
|
||||
initializer :initialize_i18n do
|
||||
require 'active_support/i18n'
|
||||
|
||||
config.i18n.each do |setting, value|
|
||||
if setting == :load_path
|
||||
I18n.load_path += value
|
||||
else
|
||||
I18n.send("#{setting}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
ActionDispatch::Callbacks.to_prepare do
|
||||
I18n.reload!
|
||||
end
|
||||
end
|
||||
|
||||
initializer :set_clear_dependencies_hook do
|
||||
unless config.cache_classes
|
||||
ActionDispatch::Callbacks.after do
|
||||
ActiveSupport::Dependencies.clear
|
||||
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)
|
||||
block.call(self)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module Rails
|
||||
class Bootstrap #< Railtie
|
||||
class Bootstrap
|
||||
include Initializable
|
||||
|
||||
def initialize(application)
|
||||
|
@ -12,6 +12,15 @@ module Rails
|
|||
require "active_support/all" unless config.active_support.bare
|
||||
end
|
||||
|
||||
# Preload all frameworks specified by the Configuration#frameworks.
|
||||
# Used by Passenger to ensure everything's loaded before forking and
|
||||
# to avoid autoload race conditions in JRuby.
|
||||
initializer :preload_frameworks do
|
||||
require 'active_support/dependencies'
|
||||
ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
|
||||
end
|
||||
|
||||
# Initialize the logger early in the stack in case we need to log some deprecation.
|
||||
initializer :initialize_logger do
|
||||
Rails.logger ||= config.logger || begin
|
||||
logger = ActiveSupport::BufferedLogger.new(config.paths.log.to_a.first)
|
||||
|
@ -29,32 +38,42 @@ module Rails
|
|||
end
|
||||
end
|
||||
|
||||
initializer :container do
|
||||
# FIXME This is just a dumb initializer used as hook
|
||||
end
|
||||
|
||||
# Preload all frameworks specified by the Configuration#frameworks.
|
||||
# Used by Passenger to ensure everything's loaded before forking and
|
||||
# to avoid autoload race conditions in JRuby.
|
||||
initializer :preload_frameworks do
|
||||
ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
|
||||
end
|
||||
|
||||
# Initialize cache early in the stack so railties can make use of it.
|
||||
initializer :initialize_cache do
|
||||
unless defined?(RAILS_CACHE)
|
||||
silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) }
|
||||
|
||||
if RAILS_CACHE.respond_to?(:middleware)
|
||||
# Insert middleware to setup and teardown local cache for each request
|
||||
config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Sets the dependency loading mechanism based on the value of
|
||||
# Configuration#cache_classes.
|
||||
# Initialize rails subscriber on top of notifications.
|
||||
initializer :initialize_subscriber do |app|
|
||||
require 'active_support/notifications'
|
||||
|
||||
if app.config.colorize_logging == false
|
||||
Rails::Subscriber.colorize_logging = false
|
||||
app.config.generators.colorize_logging = false
|
||||
end
|
||||
|
||||
ActiveSupport::Notifications.subscribe do |*args|
|
||||
Rails::Subscriber.dispatch(args)
|
||||
end
|
||||
end
|
||||
|
||||
initializer :set_clear_dependencies_hook do
|
||||
unless config.cache_classes
|
||||
ActionDispatch::Callbacks.after do
|
||||
ActiveSupport::Dependencies.clear
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Sets the dependency loading mechanism.
|
||||
# TODO: Remove files from the $" and always use require.
|
||||
initializer :initialize_dependency_mechanism do
|
||||
# TODO: Remove files from the $" and always use require
|
||||
ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,59 +1,112 @@
|
|||
require 'active_support/ordered_options'
|
||||
|
||||
module Rails
|
||||
module SharedConfiguration
|
||||
def self.middleware_stack
|
||||
@default_middleware_stack ||= ActionDispatch::MiddlewareStack.new.tap do |middleware|
|
||||
middleware.use('::ActionDispatch::Static', lambda { Rails.public_path }, :if => lambda { Rails.application.config.serve_static_assets })
|
||||
middleware.use('::Rack::Lock', :if => lambda { !ActionController::Base.allow_concurrency })
|
||||
middleware.use('::Rack::Runtime')
|
||||
middleware.use('::Rails::Rack::Logger')
|
||||
middleware.use('::ActionDispatch::ShowExceptions', lambda { ActionController::Base.consider_all_requests_local })
|
||||
middleware.use('::ActionDispatch::Callbacks', lambda { !Rails.application.config.cache_classes })
|
||||
middleware.use('::ActionDispatch::Cookies')
|
||||
middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
|
||||
middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store })
|
||||
middleware.use(lambda { Rails::Rack::Metal.new(Rails.application.config.paths.app.metals.to_a, Rails.application.config.metals) })
|
||||
middleware.use('ActionDispatch::ParamsParser')
|
||||
middleware.use('::Rack::MethodOverride')
|
||||
middleware.use('::ActionDispatch::Head')
|
||||
module Shared
|
||||
# Those configuration values are shared between railtie, engines and so forth.
|
||||
module Configuration
|
||||
def middleware
|
||||
@@default_middleware_stack ||= ActionDispatch::MiddlewareStack.new.tap do |middleware|
|
||||
middleware.use('::ActionDispatch::Static', lambda { Rails.public_path }, :if => lambda { Rails.application.config.serve_static_assets })
|
||||
middleware.use('::Rack::Lock', :if => lambda { !ActionController::Base.allow_concurrency })
|
||||
middleware.use('::Rack::Runtime')
|
||||
middleware.use('::Rails::Rack::Logger')
|
||||
middleware.use('::ActionDispatch::ShowExceptions', lambda { ActionController::Base.consider_all_requests_local })
|
||||
middleware.use('::ActionDispatch::Callbacks', lambda { !Rails.application.config.cache_classes })
|
||||
middleware.use('::ActionDispatch::Cookies')
|
||||
middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
|
||||
middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store })
|
||||
middleware.use(lambda { Rails::Rack::Metal.new(Rails.application.config.paths.app.metals.to_a, Rails.application.config.metals) })
|
||||
middleware.use('ActionDispatch::ParamsParser')
|
||||
middleware.use('::Rack::MethodOverride')
|
||||
middleware.use('::ActionDispatch::Head')
|
||||
end
|
||||
end
|
||||
|
||||
# Holds generators configuration:
|
||||
#
|
||||
# config.generators do |g|
|
||||
# g.orm :datamapper, :migration => true
|
||||
# g.template_engine :haml
|
||||
# g.test_framework :rspec
|
||||
# end
|
||||
#
|
||||
# If you want to disable color in console, do:
|
||||
#
|
||||
# config.generators.colorize_logging = false
|
||||
#
|
||||
def generators
|
||||
@@generators ||= GeneratorsConfiguration.new
|
||||
if block_given?
|
||||
yield @@generators
|
||||
else
|
||||
@@generators
|
||||
end
|
||||
end
|
||||
|
||||
def after_initialize_blocks
|
||||
@@after_initialize_blocks ||= []
|
||||
end
|
||||
|
||||
def after_initialize(&blk)
|
||||
after_initialize_blocks << blk if blk
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def options
|
||||
@@options ||= Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new }
|
||||
end
|
||||
end
|
||||
|
||||
def self.options
|
||||
@options ||= Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new }
|
||||
class GeneratorsConfiguration #:nodoc:
|
||||
attr_accessor :aliases, :options, :colorize_logging
|
||||
|
||||
def initialize
|
||||
@aliases = Hash.new { |h,k| h[k] = {} }
|
||||
@options = Hash.new { |h,k| h[k] = {} }
|
||||
@colorize_logging = true
|
||||
end
|
||||
|
||||
def method_missing(method, *args)
|
||||
method = method.to_s.sub(/=$/, '').to_sym
|
||||
|
||||
if method == :rails
|
||||
namespace, configuration = :rails, args.shift
|
||||
elsif args.first.is_a?(Hash)
|
||||
namespace, configuration = method, args.shift
|
||||
else
|
||||
namespace, configuration = args.shift, args.shift
|
||||
@options[:rails][method] = namespace
|
||||
end
|
||||
|
||||
if configuration
|
||||
aliases = configuration.delete(:aliases)
|
||||
@aliases[namespace].merge!(aliases) if aliases
|
||||
@options[namespace].merge!(configuration)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Temporarily separate the plugin configuration class from the main
|
||||
# configuration class while this bit is being cleaned up.
|
||||
# Holds Railtie basic configuration. It does not include configuration values
|
||||
# related with load paths and the application specifics.
|
||||
class Railtie::Configuration
|
||||
include Shared::Configuration
|
||||
|
||||
def self.default
|
||||
@default ||= new
|
||||
end
|
||||
|
||||
attr_reader :middleware
|
||||
|
||||
def initialize
|
||||
@options = SharedConfiguration.options
|
||||
@middleware = SharedConfiguration.middleware_stack
|
||||
end
|
||||
|
||||
def respond_to?(name)
|
||||
super || name.to_s =~ config_key_regexp
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
attr_reader :options
|
||||
|
||||
private
|
||||
|
||||
def method_missing(name, *args, &blk)
|
||||
if name.to_s =~ config_key_regexp
|
||||
return $2 == '=' ? @options[$1] = args.first : @options[$1]
|
||||
return $2 == '=' ? options[$1] = args.first : options[$1]
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
|
@ -68,8 +121,8 @@ module Rails
|
|||
end
|
||||
|
||||
class Engine::Configuration < Railtie::Configuration
|
||||
attr_reader :root
|
||||
attr_accessor :eager_load_paths, :load_once_paths, :load_paths
|
||||
attr_reader :root
|
||||
attr_writer :eager_load_paths, :load_once_paths, :load_paths
|
||||
|
||||
def initialize(root)
|
||||
@root = root
|
||||
|
@ -86,8 +139,8 @@ module Rails
|
|||
paths.lib "lib", :load_path => true
|
||||
paths.config "config"
|
||||
paths.config.environment "config/environments", :glob => "#{Rails.env}.rb"
|
||||
paths.config.initializers "config/initializers"
|
||||
paths.config.locales "config/locales"
|
||||
paths.config.initializers "config/initializers", :glob => "**/*.rb"
|
||||
paths.config.locales "config/locales", :glob => "*.{rb,yml}"
|
||||
paths.config.routes "config/routes.rb"
|
||||
paths
|
||||
end
|
||||
|
@ -111,41 +164,35 @@ module Rails
|
|||
end
|
||||
|
||||
class Configuration < Engine::Configuration
|
||||
attr_accessor :after_initialize_blocks, :cache_classes, :colorize_logging,
|
||||
:consider_all_requests_local, :dependency_loading, :filter_parameters,
|
||||
:logger, :metals, :plugins,
|
||||
:preload_frameworks, :reload_plugins, :serve_static_assets,
|
||||
:time_zone, :whiny_nils
|
||||
attr_accessor :cache_classes, :cache_store, :colorize_logging,
|
||||
:consider_all_requests_local, :dependency_loading,
|
||||
:filter_parameters, :log_level, :logger, :metals,
|
||||
:plugins, :preload_frameworks, :reload_plugins,
|
||||
:serve_static_assets, :time_zone, :whiny_nils
|
||||
|
||||
attr_writer :cache_store, :controller_paths, :i18n, :log_level
|
||||
|
||||
def initialize(*)
|
||||
def initialize(*)
|
||||
super
|
||||
@after_initialize_blocks = []
|
||||
@filter_parameters = []
|
||||
@dependency_loading = true
|
||||
@serve_static_assets = true
|
||||
end
|
||||
|
||||
def after_initialize(&blk)
|
||||
@after_initialize_blocks << blk if blk
|
||||
end
|
||||
@filter_parameters = []
|
||||
@dependency_loading = true
|
||||
@serve_static_assets = true
|
||||
end
|
||||
|
||||
def paths
|
||||
@paths ||= begin
|
||||
paths = super
|
||||
paths.app.controllers.concat(builtin_directories)
|
||||
paths.app.controllers << builtin_controller if builtin_controller
|
||||
paths.config.database "config/database.yml"
|
||||
paths.log "log/#{Rails.env}.log"
|
||||
paths.tmp "tmp"
|
||||
paths.tmp.cache "tmp/cache"
|
||||
paths.vendor "vendor", :load_path => true
|
||||
paths.vendor "vendor", :load_path => true
|
||||
paths.vendor.plugins "vendor/plugins"
|
||||
|
||||
if File.exists?("#{root}/test/mocks/#{Rails.env}")
|
||||
ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " <<
|
||||
"automatically to load paths anymore in future releases"
|
||||
paths.mocks_path "test/mocks/#{Rails.env}", :load_path => true
|
||||
paths.mocks_path "test/mocks", :load_path => true, :glob => Rails.env
|
||||
end
|
||||
|
||||
paths
|
||||
|
@ -182,6 +229,29 @@ module Rails
|
|||
YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result)
|
||||
end
|
||||
|
||||
def cache_store
|
||||
@cache_store ||= begin
|
||||
if File.exist?("#{root}/tmp/cache/")
|
||||
[ :file_store, "#{root}/tmp/cache/" ]
|
||||
else
|
||||
:memory_store
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def builtin_controller
|
||||
File.join(RAILTIES_PATH, "builtin", "rails_info") if Rails.env.development?
|
||||
end
|
||||
|
||||
def log_level
|
||||
@log_level ||= Rails.env.production? ? :info : :debug
|
||||
end
|
||||
|
||||
def time_zone
|
||||
@time_zone ||= "UTC"
|
||||
end
|
||||
|
||||
# Deprecated paths
|
||||
def view_path=(value)
|
||||
ActiveSupport::Deprecation.warn "config.view_path= is deprecated, " <<
|
||||
"please do config.paths.app.views= instead", caller
|
||||
|
@ -241,108 +311,5 @@ module Rails
|
|||
"please do config.paths.app.controllers instead", caller
|
||||
paths.app.controllers.to_a.uniq
|
||||
end
|
||||
|
||||
def cache_store
|
||||
@cache_store ||= begin
|
||||
if File.exist?("#{root}/tmp/cache/")
|
||||
[ :file_store, "#{root}/tmp/cache/" ]
|
||||
else
|
||||
:memory_store
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Include builtins only in the development environment.
|
||||
def builtin_directories
|
||||
Rails.env.development? ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
|
||||
end
|
||||
|
||||
def log_level
|
||||
@log_level ||= Rails.env.production? ? :info : :debug
|
||||
end
|
||||
|
||||
def time_zone
|
||||
@time_zone ||= "UTC"
|
||||
end
|
||||
|
||||
def i18n
|
||||
@i18n ||= begin
|
||||
i18n = ActiveSupport::OrderedOptions.new
|
||||
i18n.load_path = []
|
||||
|
||||
if File.exist?(File.join(root, 'config', 'locales'))
|
||||
i18n.load_path << Dir[File.join(root, 'config', 'locales', '*.{rb,yml}')]
|
||||
i18n.load_path.flatten!
|
||||
end
|
||||
|
||||
i18n
|
||||
end
|
||||
end
|
||||
|
||||
def environment_path
|
||||
"#{root}/config/environments/#{Rails.env}.rb"
|
||||
end
|
||||
|
||||
# Holds generators configuration:
|
||||
#
|
||||
# config.generators do |g|
|
||||
# g.orm :datamapper, :migration => true
|
||||
# g.template_engine :haml
|
||||
# g.test_framework :rspec
|
||||
# end
|
||||
#
|
||||
# If you want to disable color in console, do:
|
||||
#
|
||||
# config.generators.colorize_logging = false
|
||||
#
|
||||
def generators
|
||||
@generators ||= Generators.new
|
||||
if block_given?
|
||||
yield @generators
|
||||
else
|
||||
@generators
|
||||
end
|
||||
end
|
||||
|
||||
# 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
|
||||
|
||||
class Generators #:nodoc:
|
||||
attr_accessor :aliases, :options, :colorize_logging
|
||||
|
||||
def initialize
|
||||
@aliases = Hash.new { |h,k| h[k] = {} }
|
||||
@options = Hash.new { |h,k| h[k] = {} }
|
||||
@colorize_logging = true
|
||||
end
|
||||
|
||||
def method_missing(method, *args)
|
||||
method = method.to_s.sub(/=$/, '').to_sym
|
||||
|
||||
if method == :rails
|
||||
namespace, configuration = :rails, args.shift
|
||||
elsif args.first.is_a?(Hash)
|
||||
namespace, configuration = method, args.shift
|
||||
else
|
||||
namespace, configuration = args.shift, args.shift
|
||||
@options[:rails][method] = namespace
|
||||
end
|
||||
|
||||
if configuration
|
||||
aliases = configuration.delete(:aliases)
|
||||
@aliases[namespace].merge!(aliases) if aliases
|
||||
@options[namespace].merge!(configuration)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,6 @@ module Rails
|
|||
# TODO Move I18n here
|
||||
# TODO Set routes namespaces
|
||||
class Engine < Railtie
|
||||
|
||||
class << self
|
||||
attr_accessor :called_from
|
||||
|
||||
|
@ -49,8 +48,8 @@ module Rails
|
|||
delegate :middleware, :root, :to => :config
|
||||
|
||||
# Add configured load paths to ruby load paths and remove duplicates.
|
||||
initializer :set_load_path, :before => :container do
|
||||
expand_load_path(config.load_paths).reverse_each do |path|
|
||||
initializer :set_load_path do
|
||||
config.load_paths.reverse_each do |path|
|
||||
$LOAD_PATH.unshift(path) if File.directory?(path)
|
||||
end
|
||||
$LOAD_PATH.uniq!
|
||||
|
@ -58,11 +57,9 @@ module Rails
|
|||
|
||||
# Set the paths from which Rails will automatically load source files,
|
||||
# and the load_once paths.
|
||||
initializer :set_autoload_paths, :before => :container do
|
||||
require 'active_support/dependencies'
|
||||
|
||||
ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths)
|
||||
ActiveSupport::Dependencies.load_once_paths = expand_load_path(config.load_once_paths)
|
||||
initializer :set_autoload_paths do
|
||||
ActiveSupport::Dependencies.load_paths.concat(config.load_paths)
|
||||
ActiveSupport::Dependencies.load_once_paths.concat(config.load_once_paths)
|
||||
|
||||
extra = ActiveSupport::Dependencies.load_once_paths -
|
||||
ActiveSupport::Dependencies.load_paths
|
||||
|
@ -74,31 +71,32 @@ module Rails
|
|||
end_error
|
||||
end
|
||||
|
||||
# Freeze the arrays so future modifications will fail rather than do nothing mysteriously
|
||||
# Freeze so future modifications will fail rather than do nothing mysteriously
|
||||
config.load_once_paths.freeze
|
||||
end
|
||||
|
||||
# Routing must be initialized after plugins to allow the former to extend the routes
|
||||
initializer :add_routing_files do |app|
|
||||
routes = select_existing(config.paths.config.routes)
|
||||
app.route_configuration_files.concat(routes)
|
||||
end
|
||||
|
||||
initializer :add_view_paths do
|
||||
views = select_existing(config.paths.app.views)
|
||||
ActionController::Base.view_paths.concat(views) if defined? ActionController
|
||||
ActionMailer::Base.view_paths.concat(views) if defined? ActionMailer
|
||||
end
|
||||
|
||||
initializer :load_application_initializers do
|
||||
select_existing(config.paths.config.initializers).each do |initializers|
|
||||
Dir["#{initializers}/**/*.rb"].sort.each do |initializer|
|
||||
load(initializer)
|
||||
end
|
||||
config.paths.config.routes.to_a.each do |route|
|
||||
app.route_configuration_files << route if File.exists?(route)
|
||||
end
|
||||
end
|
||||
|
||||
initializer :add_locales do
|
||||
config.i18n.load_path.concat(config.paths.config.locales.to_a)
|
||||
end
|
||||
|
||||
initializer :add_view_paths do
|
||||
views = config.paths.app.views.to_a
|
||||
ActionController::Base.view_paths.concat(views) if defined?(ActionController)
|
||||
ActionMailer::Base.view_paths.concat(views) if defined?(ActionMailer)
|
||||
end
|
||||
|
||||
initializer :load_application_initializers do
|
||||
config.paths.config.initializers.each do |initializer|
|
||||
load(initializer)
|
||||
end
|
||||
end
|
||||
|
||||
# Eager load application classes
|
||||
initializer :load_application_classes do |app|
|
||||
next if $rails_rake_task
|
||||
|
||||
|
@ -111,15 +109,5 @@ module Rails
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def select_existing(paths)
|
||||
paths.to_a.select { |path| File.exists?(path) }.uniq
|
||||
end
|
||||
|
||||
def expand_load_path(load_paths)
|
||||
load_paths.map { |path| Dir.glob(path.to_s) }.flatten.uniq
|
||||
end
|
||||
end
|
||||
end
|
|
@ -19,12 +19,6 @@ module Rails
|
|||
@options[:after]
|
||||
end
|
||||
|
||||
def global
|
||||
@options[:global]
|
||||
end
|
||||
|
||||
alias global? global
|
||||
|
||||
def run(*args)
|
||||
@context.instance_exec(*args, &block)
|
||||
end
|
||||
|
@ -71,7 +65,7 @@ module Rails
|
|||
|
||||
def initializers
|
||||
@initializers ||= begin
|
||||
initializers = self.class.initializers_for(:instance)
|
||||
initializers = self.class.initializers_chain
|
||||
Collection.new(initializers.map { |i| i.bind(self) })
|
||||
end
|
||||
end
|
||||
|
@ -81,26 +75,23 @@ module Rails
|
|||
@initializers ||= []
|
||||
end
|
||||
|
||||
def initializers_for(scope = :global)
|
||||
def initializers_chain
|
||||
initializers = Collection.new
|
||||
ancestors.reverse_each do |klass|
|
||||
next unless klass.respond_to?(:initializers)
|
||||
initializers = initializers + klass.initializers.select { |i|
|
||||
(scope == :global) == !!i.global?
|
||||
}
|
||||
initializers = initializers + klass.initializers
|
||||
end
|
||||
initializers
|
||||
end
|
||||
|
||||
def initializer(name, opts = {}, &blk)
|
||||
raise ArgumentError, "A block must be passed when defining an initializer" unless blk
|
||||
@initializers ||= []
|
||||
@initializers << Initializer.new(name, nil, opts, &blk)
|
||||
initializers << Initializer.new(name, nil, opts, &blk)
|
||||
end
|
||||
|
||||
def run_initializers(*args)
|
||||
return if @ran
|
||||
initializers_for(:global).each do |initializer|
|
||||
initializers_chain.each do |initializer|
|
||||
instance_exec(*args, &initializer.block)
|
||||
end
|
||||
@ran = true
|
||||
|
|
|
@ -29,7 +29,7 @@ module ApplicationTests
|
|||
|
||||
add_to_config <<-RUBY
|
||||
config.root = "#{app_path}"
|
||||
config.eager_load_paths = "#{app_path}/lib"
|
||||
config.eager_load_paths << "#{app_path}/lib"
|
||||
RUBY
|
||||
|
||||
require "#{app_path}/config/environment"
|
||||
|
@ -132,7 +132,7 @@ module ApplicationTests
|
|||
require "#{app_path}/config/environment"
|
||||
|
||||
assert_equal [
|
||||
"#{app_path}/config/locales/en.yml", "my/other/locale.yml"
|
||||
"my/other/locale.yml", "#{app_path}/config/locales/en.yml"
|
||||
], Rails.application.config.i18n.load_path
|
||||
end
|
||||
|
||||
|
|
|
@ -1,17 +1,6 @@
|
|||
require "isolation/abstract_unit"
|
||||
|
||||
module ApplicationTests
|
||||
class MyQueue
|
||||
def publish(name, *args)
|
||||
raise name
|
||||
end
|
||||
|
||||
# Not a full queue implementation
|
||||
def method_missing(name, *args, &blk)
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class MockLogger
|
||||
def method_missing(*args)
|
||||
@logged ||= []
|
||||
|
@ -39,22 +28,6 @@ module ApplicationTests
|
|||
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")
|
||||
|
||||
add_to_config <<-RUBY
|
||||
config.notifications.notifier = ActiveSupport::Notifications::Notifier.new(ApplicationTests::MyQueue.new)
|
||||
RUBY
|
||||
|
||||
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
|
||||
|
|
|
@ -10,14 +10,14 @@ module InitializableTests
|
|||
attr_accessor :foo, :bar
|
||||
end
|
||||
|
||||
initializer :omg, :global => true do
|
||||
initializer :omg do
|
||||
@foo ||= 0
|
||||
@foo += 1
|
||||
end
|
||||
end
|
||||
|
||||
class Bar < Foo
|
||||
initializer :bar, :global => true do
|
||||
initializer :bar do
|
||||
@bar ||= 0
|
||||
@bar += 1
|
||||
end
|
||||
|
@ -26,7 +26,7 @@ module InitializableTests
|
|||
module Word
|
||||
include Rails::Initializable
|
||||
|
||||
initializer :word, :global => true do
|
||||
initializer :word do
|
||||
$word = "bird"
|
||||
end
|
||||
end
|
||||
|
@ -34,11 +34,11 @@ module InitializableTests
|
|||
class Parent
|
||||
include Rails::Initializable
|
||||
|
||||
initializer :one, :global => true do
|
||||
initializer :one do
|
||||
$arr << 1
|
||||
end
|
||||
|
||||
initializer :two, :global => true do
|
||||
initializer :two do
|
||||
$arr << 2
|
||||
end
|
||||
end
|
||||
|
@ -46,17 +46,17 @@ module InitializableTests
|
|||
class Child < Parent
|
||||
include Rails::Initializable
|
||||
|
||||
initializer :three, :before => :one, :global => true do
|
||||
initializer :three, :before => :one do
|
||||
$arr << 3
|
||||
end
|
||||
|
||||
initializer :four, :after => :one, :global => true do
|
||||
initializer :four, :after => :one do
|
||||
$arr << 4
|
||||
end
|
||||
end
|
||||
|
||||
class Parent
|
||||
initializer :five, :before => :one, :global => true do
|
||||
initializer :five, :before => :one do
|
||||
$arr << 5
|
||||
end
|
||||
end
|
||||
|
@ -72,11 +72,11 @@ module InitializableTests
|
|||
$arr << 2
|
||||
end
|
||||
|
||||
initializer :three, :global => true do
|
||||
initializer :three do
|
||||
$arr << 3
|
||||
end
|
||||
|
||||
initializer :four, :global => true do
|
||||
initializer :four do
|
||||
$arr << 4
|
||||
end
|
||||
end
|
||||
|
@ -181,13 +181,7 @@ module InitializableTests
|
|||
$arr = []
|
||||
instance = Instance.new
|
||||
instance.run_initializers
|
||||
assert_equal [1, 2], $arr
|
||||
end
|
||||
|
||||
test "running globals" do
|
||||
$arr = []
|
||||
Instance.run_initializers
|
||||
assert_equal [3, 4], $arr
|
||||
assert_equal [1, 2, 3, 4], $arr
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ module InitializerTests
|
|||
config.root = "#{app_path}"
|
||||
config.i18n.load_path << "my/other/locale.yml"
|
||||
RUBY
|
||||
|
||||
require "#{app_path}/config/environment"
|
||||
|
||||
#{RAILS_FRAMEWORK_ROOT}/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml
|
||||
|
@ -23,8 +24,8 @@ module InitializerTests
|
|||
#{RAILS_FRAMEWORK_ROOT}/activemodel/lib/active_model/locale/en.yml
|
||||
#{RAILS_FRAMEWORK_ROOT}/activerecord/lib/active_record/locale/en.yml
|
||||
#{RAILS_FRAMEWORK_ROOT}/actionpack/lib/action_view/locale/en.yml
|
||||
#{RAILS_FRAMEWORK_ROOT}/railties/tmp/app/config/locales/en.yml
|
||||
my/other/locale.yml
|
||||
#{RAILS_FRAMEWORK_ROOT}/railties/tmp/app/config/locales/en.yml
|
||||
).map { |path| File.expand_path(path) }, I18n.load_path.map { |path| File.expand_path(path) }
|
||||
end
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ module InitializerTests
|
|||
assert_path @paths.tmp, "tmp"
|
||||
assert_path @paths.tmp.cache, "tmp", "cache"
|
||||
assert_path @paths.config, "config"
|
||||
assert_path @paths.config.locales, "config", "locales"
|
||||
assert_path @paths.config.locales, "config", "locales", "en.yml"
|
||||
assert_path @paths.config.environment, "config", "environments", "development.rb"
|
||||
|
||||
assert_equal root("app", "controllers"), @paths.app.controllers.to_a.first
|
||||
|
|
Loading…
Reference in a new issue