mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Cleanup junk metal and revise API
API Change: Returning a "X-Cascade: pass" header triggers the cascade instead of a 404 response.
This commit is contained in:
parent
3921586091
commit
02bbde4e78
5 changed files with 29 additions and 66 deletions
|
@ -37,11 +37,33 @@ module ActionController
|
||||||
ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank?
|
ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
initializer "action_controller.initialize_metal" do |app|
|
class MetalMiddlewareBuilder
|
||||||
Rails::Rack::Metal.requested_metals = app.config.metals
|
def initialize(metals)
|
||||||
|
@metals = metals
|
||||||
|
end
|
||||||
|
|
||||||
app.config.middleware.insert_before(:"ActionDispatch::ParamsParser",
|
def new(app)
|
||||||
Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
|
ActionDispatch::Cascade.new(@metals, app)
|
||||||
|
end
|
||||||
|
|
||||||
|
def name
|
||||||
|
ActionDispatch::Cascade.name
|
||||||
|
end
|
||||||
|
alias_method :to_s, :name
|
||||||
|
end
|
||||||
|
|
||||||
|
initializer "action_controller.initialize_metal" do |app|
|
||||||
|
metal_root = "#{Rails.root}/app/metal"
|
||||||
|
load_list = app.config.metals || Dir["#{metal_root}/**/*.rb"]
|
||||||
|
|
||||||
|
metals = load_list.map { |metal|
|
||||||
|
metal = File.basename(metal.gsub("#{metal_root}/", ''), '.rb')
|
||||||
|
require_dependency metal
|
||||||
|
metal.camelize.constantize
|
||||||
|
}.compact
|
||||||
|
|
||||||
|
middleware = MetalMiddlewareBuilder.new(metals)
|
||||||
|
app.config.middleware.insert_before(:"ActionDispatch::ParamsParser", middleware)
|
||||||
end
|
end
|
||||||
|
|
||||||
# # Prepare dispatcher callbacks and run 'prepare' callbacks
|
# # Prepare dispatcher callbacks and run 'prepare' callbacks
|
||||||
|
|
|
@ -2,7 +2,6 @@ module Rails
|
||||||
module Rack
|
module Rack
|
||||||
autoload :Debugger, "rails/rack/debugger"
|
autoload :Debugger, "rails/rack/debugger"
|
||||||
autoload :LogTailer, "rails/rack/log_tailer"
|
autoload :LogTailer, "rails/rack/log_tailer"
|
||||||
autoload :Metal, "rails/rack/metal"
|
|
||||||
autoload :Static, "rails/rack/static"
|
autoload :Static, "rails/rack/static"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
require 'active_support/ordered_hash'
|
|
||||||
require 'active_support/core_ext/class/attribute_accessors'
|
|
||||||
require 'active_support/dependencies'
|
|
||||||
|
|
||||||
module Rails
|
|
||||||
module Rack
|
|
||||||
class Metal
|
|
||||||
NotFoundResponse = [404, {}, []].freeze
|
|
||||||
NotFound = lambda { NotFoundResponse }
|
|
||||||
|
|
||||||
cattr_accessor :metal_paths
|
|
||||||
self.metal_paths = ["#{Rails.root}/app/metal"]
|
|
||||||
cattr_accessor :requested_metals
|
|
||||||
|
|
||||||
cattr_accessor :pass_through_on
|
|
||||||
self.pass_through_on = 404
|
|
||||||
|
|
||||||
def self.metals
|
|
||||||
matcher = /#{Regexp.escape('/app/metal/')}(.*)\.rb\Z/
|
|
||||||
metal_glob = metal_paths.map{ |base| "#{base}/**/*.rb" }
|
|
||||||
all_metals = {}
|
|
||||||
|
|
||||||
metal_glob.each do |glob|
|
|
||||||
Dir[glob].sort.map do |file|
|
|
||||||
file = file.match(matcher)[1]
|
|
||||||
all_metals[file.camelize] = file
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
load_list = requested_metals || all_metals.keys
|
|
||||||
|
|
||||||
load_list.map do |requested_metal|
|
|
||||||
if metal = all_metals[requested_metal]
|
|
||||||
require_dependency metal
|
|
||||||
requested_metal.constantize
|
|
||||||
end
|
|
||||||
end.compact
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(app)
|
|
||||||
@app = app
|
|
||||||
@pass_through_on = {}
|
|
||||||
[*self.class.pass_through_on].each { |status| @pass_through_on[status] = true }
|
|
||||||
|
|
||||||
@metals = ActiveSupport::OrderedHash.new
|
|
||||||
self.class.metals.each { |app| @metals[app] = true }
|
|
||||||
freeze
|
|
||||||
end
|
|
||||||
|
|
||||||
def call(env)
|
|
||||||
@metals.keys.each do |app|
|
|
||||||
result = app.call(env)
|
|
||||||
return result unless @pass_through_on.include?(result[0].to_i)
|
|
||||||
end
|
|
||||||
@app.call(env)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -37,7 +37,7 @@ module ApplicationTests
|
||||||
app_file 'app/metal/metal_a.rb', <<-RUBY
|
app_file 'app/metal/metal_a.rb', <<-RUBY
|
||||||
class MetalA
|
class MetalA
|
||||||
def self.call(env)
|
def self.call(env)
|
||||||
[404, { "Content-Type" => "text/html"}, ["Metal A"]]
|
[404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Metal A"]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
|
@ -59,7 +59,7 @@ module ApplicationTests
|
||||||
app_file 'app/metal/foo_metal.rb', <<-RUBY
|
app_file 'app/metal/foo_metal.rb', <<-RUBY
|
||||||
class FooMetal
|
class FooMetal
|
||||||
def self.call(env)
|
def self.call(env)
|
||||||
[404, { "Content-Type" => "text/html"}, ["Not Found"]]
|
[404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Not Found"]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
|
|
|
@ -20,6 +20,7 @@ module ApplicationTests
|
||||||
"ActionDispatch::ShowExceptions",
|
"ActionDispatch::ShowExceptions",
|
||||||
"ActionDispatch::Callbacks",
|
"ActionDispatch::Callbacks",
|
||||||
"ActionDispatch::Session::CookieStore",
|
"ActionDispatch::Session::CookieStore",
|
||||||
|
"ActionDispatch::Cascade",
|
||||||
"ActionDispatch::ParamsParser",
|
"ActionDispatch::ParamsParser",
|
||||||
"Rack::MethodOverride",
|
"Rack::MethodOverride",
|
||||||
"Rack::Head",
|
"Rack::Head",
|
||||||
|
|
Loading…
Reference in a new issue