From edb8131535c74ac215bf36c0d58d2019ed091a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 15:27:40 +0100 Subject: Move Rails::Rack::Metal to Rails::Application::Metal and just add cascade if any metal was declared. --- railties/lib/rails/application.rb | 1 + railties/lib/rails/application/metal.rb | 46 ++++++++++++++++++++++++++++ railties/lib/rails/configuration.rb | 2 +- railties/lib/rails/engine.rb | 2 +- railties/lib/rails/rack.rb | 1 - railties/lib/rails/rack/metal.rb | 41 ------------------------- railties/test/application/middleware_test.rb | 7 ++++- 7 files changed, 55 insertions(+), 45 deletions(-) create mode 100644 railties/lib/rails/application/metal.rb delete mode 100644 railties/lib/rails/rack/metal.rb diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 12aa279959..9e41210119 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -9,6 +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 :Railties, 'rails/application/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' diff --git a/railties/lib/rails/application/metal.rb b/railties/lib/rails/application/metal.rb new file mode 100644 index 0000000000..17786dd4ba --- /dev/null +++ b/railties/lib/rails/application/metal.rb @@ -0,0 +1,46 @@ +require 'action_dispatch' + +module Rails + class Application + class Metal + def self.paths + @paths ||= [] + end + + def self.metals + @metals ||= [] + end + + def initialize(list=nil) + metals = [] + list = Array(list || :all).map(&:to_sym) + + self.class.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 + next unless list.include?(metal) || list.include?(:all) + require_dependency metal + metals << metal + end + end + + metals = metals.sort_by do |m| + [list.index(m) || list.index(:all), m.to_s] + 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 diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 3c5c1c1e16..c5cb7b2d09 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -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::Rack::Metal.new(Rails.application.config.metals) }) + middleware.use(lambda { Rails::Application::Metal.new(Rails.application.config.metals) }, :if => lambda { Rails::Application::Metal.metals.any? }) middleware.use('ActionDispatch::ParamsParser') middleware.use('::Rack::MethodOverride') middleware.use('::ActionDispatch::Head') diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 8cb938c2b9..ebbee67cf4 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -99,7 +99,7 @@ module Rails end initializer :add_metals do - Rails::Rack::Metal.paths.unshift(*paths.app.metals.to_a) + Rails::Application::Metal.paths.unshift(*paths.app.metals.to_a) end initializer :load_application_initializers do diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb index 4bc0c2c88b..1f20ceae44 100644 --- a/railties/lib/rails/rack.rb +++ b/railties/lib/rails/rack.rb @@ -3,7 +3,6 @@ module Rails autoload :Debugger, "rails/rack/debugger" autoload :Logger, "rails/rack/logger" autoload :LogTailer, "rails/rack/log_tailer" - autoload :Metal, "rails/rack/metal" autoload :Static, "rails/rack/static" end end diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb deleted file mode 100644 index 732936da32..0000000000 --- a/railties/lib/rails/rack/metal.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'action_dispatch' - -module Rails - module Rack - class Metal - def self.paths - @paths ||= [] - end - - def initialize(list=nil) - metals = [] - list = Array(list || :all).map(&:to_sym) - - self.class.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 - next unless list.include?(metal) || list.include?(:all) - require_dependency metal - metals << metal - end - end - - metals = metals.sort_by do |m| - [list.index(m) || list.index(:all), m.to_s] - end - - @metals = metals.map { |m| m.to_s.camelize.constantize } - end - - def new(app) - ActionDispatch::Cascade.new(@metals, app) - end - - def name - ActionDispatch::Cascade.name - end - alias_method :to_s, :name - end - end -end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index 31696598ce..0b92cdba54 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -23,7 +23,6 @@ module ApplicationTests "ActionDispatch::Cookies", "ActionDispatch::Session::CookieStore", "ActionDispatch::Flash", - "ActionDispatch::Cascade", "ActionDispatch::ParamsParser", "Rack::MethodOverride", "ActionDispatch::Head", @@ -70,6 +69,12 @@ module ApplicationTests assert_equal "Rack::Config", middleware.first end + test "shows cascade if any metal exists" do + app_file "app/metal/foo.rb", "class Foo; end" + boot! + assert middleware.include?("ActionDispatch::Cascade") + end + private def boot! require "#{app_path}/config/environment" -- cgit v1.2.3