1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Made Engine valid rack app with its own middleware stack

This commit is contained in:
Piotr Sarnacki 2010-06-22 23:51:28 +02:00
parent f7af75976a
commit ad6be08762
6 changed files with 65 additions and 4 deletions

View file

@ -9,7 +9,7 @@ module Rails
attr_accessor :allow_concurrency, :cache_classes, :cache_store, attr_accessor :allow_concurrency, :cache_classes, :cache_store,
:encoding, :consider_all_requests_local, :dependency_loading, :encoding, :consider_all_requests_local, :dependency_loading,
:filter_parameters, :log_level, :logger, :middleware, :filter_parameters, :log_level, :logger,
:plugins, :preload_frameworks, :reload_plugins, :plugins, :preload_frameworks, :reload_plugins,
:secret_token, :serve_static_assets, :session_options, :secret_token, :serve_static_assets, :session_options,
:time_zone, :whiny_nils :time_zone, :whiny_nils

View file

@ -17,7 +17,7 @@ module Rails
end end
def engines def engines
@engines ||= ::Rails::Engine.subclasses.map(&:new) @engines ||= ::Rails::Engine.subclasses.map(&:instance)
end end
def plugins def plugins
@ -28,4 +28,4 @@ module Rails
end end
end end
end end
end end

View file

@ -140,6 +140,19 @@ module Rails
end end
end end
def app
raise "You can't use Engine as rack application without providing valid rack endpoint" unless endpoint
@app ||= config.middleware.build(endpoint)
end
def endpoint
self.class.endpoint
end
def call(env)
app.call(env)
end
# Add configured load paths to ruby load paths and remove duplicates. # Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path, :before => :bootstrap_hook do initializer :set_load_path, :before => :bootstrap_hook do
_all_load_paths.reverse_each do |path| _all_load_paths.reverse_each do |path|

View file

@ -3,10 +3,12 @@ module Rails
module Configurable module Configurable
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
base.private_class_method :new
end end
module ClassMethods module ClassMethods
delegate :middleware, :root, :paths, :to => :config delegate :middleware, :root, :paths, :to => :config
delegate :call, :to => :instance
def config def config
@config ||= Engine::Configuration.new(find_root_with_flag("lib")) @config ||= Engine::Configuration.new(find_root_with_flag("lib"))
@ -15,6 +17,15 @@ module Rails
def inherited(base) def inherited(base)
raise "You cannot inherit from a Rails::Engine child" raise "You cannot inherit from a Rails::Engine child"
end end
def instance
@instance ||= new
end
def endpoint(endpoint = nil)
@endpoint = endpoint if endpoint
@endpoint
end
end end
def config def config
@ -22,4 +33,4 @@ module Rails
end end
end end
end end
end end

View file

@ -5,10 +5,12 @@ module Rails
class Configuration < ::Rails::Railtie::Configuration class Configuration < ::Rails::Railtie::Configuration
attr_reader :root attr_reader :root
attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
attr_accessor :middleware
def initialize(root=nil) def initialize(root=nil)
super() super()
@root = root @root = root
@middleware = ActionDispatch::MiddlewareStack.new
end end
def paths def paths

View file

@ -50,5 +50,40 @@ module RailtiesTest
assert index < initializers.index { |i| i.name == :build_middleware_stack } assert index < initializers.index { |i| i.name == :build_middleware_stack }
end end
class Upcaser
def initialize(app)
@app = app
end
def call(env)
response = @app.call(env)
response[2].upcase!
response
end
end
test "engine is a rack app and can have his own middleware stack" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, 'Hello World'] }
config.middleware.use ::RailtiesTest::EngineTest::Upcaser
end
end
RUBY
boot_rails
Rails::Application.routes.draw do |map|
mount(Bukkits::Engine => "/bukkits")
end
env = Rack::MockRequest.env_for("/bukkits")
response = Rails::Application.call(env)
assert_equal "HELLO WORLD", response[2]
end
end end
end end