Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor MetalLoader and RoutesReloader to rely less on class configu…

…ration.

Signed-off-by: Carl Lerche <carllerche@mac.com>
  • Loading branch information...
commit 226d8e745a0d47efa50633661a2b7fe1859609bc 1 parent 252911e
José Valim josevalim authored Carl Lerche committed
8 railties/lib/rails/application.rb
View
@@ -9,7 +9,7 @@ class Application < Engine
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 @@ def railties
@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!
4 railties/lib/rails/application/finisher.rb
View
@@ -15,9 +15,9 @@ module Finisher
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
40 railties/lib/rails/application/metal.rb → railties/lib/rails/application/metal_loader.rb
View
@@ -2,20 +2,34 @@
module Rails
class Application
- class Metal
- def self.paths
- @paths ||= []
+ class MetalLoader
+ attr_reader :paths, :metals
+
+ def initialize
+ @paths, @metals = [], []
+ end
+
+ def build_middleware(list=nil)
+ load_metals!(list)
+ self
end
- def self.metals
- @metals ||= []
+ def new(app)
+ ActionDispatch::Cascade.new(@metals, app)
+ end
+
+ def name
+ ActionDispatch::Cascade.name
end
+ alias :to_s :name
- def initialize(list=nil)
+ 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 @@ def initialize(list=nil)
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
13 railties/lib/rails/application/routes_reloader.rb
View
@@ -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 @@ def reload!
routes.disable_clear_and_finalize = true
routes.clear!
- self.class.paths.each { |path| load(path) }
+ paths.each { |path| load(path) }
routes.finalize!
nil
2  railties/lib/rails/configuration.rb
View
@@ -16,7 +16,7 @@ def middleware
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')
8 railties/lib/rails/engine.rb
View
@@ -70,9 +70,9 @@ def load_tasks
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 @@ def load_tasks
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
Please sign in to comment.
Something went wrong with that request. Please try again.