mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor MetalLoader and RoutesReloader to rely less on class configuration.
Signed-off-by: Carl Lerche <carllerche@mac.com>
This commit is contained in:
parent
252911e378
commit
226d8e745a
6 changed files with 40 additions and 35 deletions
|
@ -9,7 +9,7 @@ module Rails
|
|||
autoload :Configurable, 'rails/application/configurable'
|
||||
autoload :Configuration, 'rails/application/configuration'
|
||||
autoload :Finisher, 'rails/application/finisher'
|
||||
autoload :Metal, 'rails/application/metal'
|
||||
autoload :MetalLoader, 'rails/application/metal_loader'
|
||||
autoload :Railties, 'rails/application/railties'
|
||||
autoload :RoutesReloader, 'rails/application/routes_reloader'
|
||||
|
||||
|
@ -51,8 +51,12 @@ module Rails
|
|||
@railties ||= Railties.new(config)
|
||||
end
|
||||
|
||||
def metal_loader
|
||||
@metal_laoder ||= MetalLoader.new
|
||||
end
|
||||
|
||||
def routes_reloader
|
||||
@routes_reloader ||= RoutesReloader.new(config)
|
||||
@routes_reloader ||= RoutesReloader.new
|
||||
end
|
||||
|
||||
def reload_routes!
|
||||
|
|
|
@ -15,9 +15,9 @@ module Rails
|
|||
end
|
||||
end
|
||||
|
||||
initializer :add_builtin_route do
|
||||
initializer :add_builtin_route do |app|
|
||||
if Rails.env.development?
|
||||
Rails::Application::RoutesReloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
|
||||
app.routes_reloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -2,20 +2,34 @@ require 'action_dispatch'
|
|||
|
||||
module Rails
|
||||
class Application
|
||||
class Metal
|
||||
def self.paths
|
||||
@paths ||= []
|
||||
class MetalLoader
|
||||
attr_reader :paths, :metals
|
||||
|
||||
def initialize
|
||||
@paths, @metals = [], []
|
||||
end
|
||||
|
||||
def self.metals
|
||||
@metals ||= []
|
||||
def build_middleware(list=nil)
|
||||
load_metals!(list)
|
||||
self
|
||||
end
|
||||
|
||||
def initialize(list=nil)
|
||||
def new(app)
|
||||
ActionDispatch::Cascade.new(@metals, app)
|
||||
end
|
||||
|
||||
def name
|
||||
ActionDispatch::Cascade.name
|
||||
end
|
||||
alias :to_s :name
|
||||
|
||||
protected
|
||||
|
||||
def load_metals!(list)
|
||||
metals = []
|
||||
list = Array(list || :all).map(&:to_sym)
|
||||
list = Array(list || :all).map(&:to_sym)
|
||||
|
||||
self.class.paths.each do |path|
|
||||
paths.each do |path|
|
||||
matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/
|
||||
Dir.glob("#{path}/**/*.rb").sort.each do |metal_path|
|
||||
metal = metal_path.sub(matcher, '\1').to_sym
|
||||
|
@ -30,17 +44,7 @@ module Rails
|
|||
end
|
||||
|
||||
@metals = metals.map { |m| m.to_s.camelize.constantize }
|
||||
self.class.metals.concat(@metals)
|
||||
end
|
||||
|
||||
def new(app)
|
||||
ActionDispatch::Cascade.new(@metals, app)
|
||||
end
|
||||
|
||||
def name
|
||||
ActionDispatch::Cascade.name
|
||||
end
|
||||
alias_method :to_s, :name
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,19 +1,16 @@
|
|||
module Rails
|
||||
class Application
|
||||
# TODO Write tests for this behavior extracted from Application
|
||||
class RoutesReloader
|
||||
def self.paths
|
||||
@paths ||= []
|
||||
end
|
||||
attr_reader :paths
|
||||
|
||||
def initialize(config)
|
||||
@config, @last_change_at = config, nil
|
||||
def initialize
|
||||
@paths, @last_change_at = [], nil
|
||||
end
|
||||
|
||||
def changed_at
|
||||
routes_changed_at = nil
|
||||
|
||||
self.class.paths.each do |path|
|
||||
paths.each do |path|
|
||||
config_changed_at = File.stat(path).mtime
|
||||
|
||||
if routes_changed_at.nil? || config_changed_at > routes_changed_at
|
||||
|
@ -29,7 +26,7 @@ module Rails
|
|||
routes.disable_clear_and_finalize = true
|
||||
|
||||
routes.clear!
|
||||
self.class.paths.each { |path| load(path) }
|
||||
paths.each { |path| load(path) }
|
||||
routes.finalize!
|
||||
|
||||
nil
|
||||
|
|
|
@ -16,7 +16,7 @@ module Rails
|
|||
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::Application::Metal.new(Rails.application.config.metals) }, :if => lambda { Rails::Application::Metal.metals.any? })
|
||||
middleware.use(lambda { Rails.application.metal_loader.build_middleware(Rails.application.config.metals) }, :if => lambda { Rails.application.metal_loader.metals.any? })
|
||||
middleware.use('ActionDispatch::ParamsParser')
|
||||
middleware.use('::Rack::MethodOverride')
|
||||
middleware.use('::ActionDispatch::Head')
|
||||
|
|
|
@ -70,9 +70,9 @@ module Rails
|
|||
config.load_once_paths.freeze
|
||||
end
|
||||
|
||||
initializer :add_routing_paths do
|
||||
initializer :add_routing_paths do |app|
|
||||
paths.config.routes.to_a.each do |route|
|
||||
Rails::Application::RoutesReloader.paths.unshift(route) if File.exists?(route)
|
||||
app.routes_reloader.paths.unshift(route) if File.exists?(route)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -98,8 +98,8 @@ module Rails
|
|||
ActionMailer::Base.view_paths.unshift(*views) if defined?(ActionMailer)
|
||||
end
|
||||
|
||||
initializer :add_metals do
|
||||
Rails::Application::Metal.paths.unshift(*paths.app.metals.to_a)
|
||||
initializer :add_metals do |app|
|
||||
app.metal_loader.paths.unshift(*paths.app.metals.to_a)
|
||||
end
|
||||
|
||||
initializer :load_application_initializers do
|
||||
|
|
Loading…
Reference in a new issue