提交 ad6be087 编写于 作者: P Piotr Sarnacki

Made Engine valid rack app with its own middleware stack

上级 f7af7597
......@@ -9,7 +9,7 @@ class Configuration < ::Rails::Engine::Configuration
attr_accessor :allow_concurrency, :cache_classes, :cache_store,
:encoding, :consider_all_requests_local, :dependency_loading,
:filter_parameters, :log_level, :logger, :middleware,
:filter_parameters, :log_level, :logger,
:plugins, :preload_frameworks, :reload_plugins,
:secret_token, :serve_static_assets, :session_options,
:time_zone, :whiny_nils
......
......@@ -17,7 +17,7 @@ def railties
end
def engines
@engines ||= ::Rails::Engine.subclasses.map(&:new)
@engines ||= ::Rails::Engine.subclasses.map(&:instance)
end
def plugins
......@@ -28,4 +28,4 @@ def plugins
end
end
end
end
\ No newline at end of file
end
......@@ -140,6 +140,19 @@ def eager_load!
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.
initializer :set_load_path, :before => :bootstrap_hook do
_all_load_paths.reverse_each do |path|
......
......@@ -3,10 +3,12 @@ class Engine
module Configurable
def self.included(base)
base.extend ClassMethods
base.private_class_method :new
end
module ClassMethods
delegate :middleware, :root, :paths, :to => :config
delegate :call, :to => :instance
def config
@config ||= Engine::Configuration.new(find_root_with_flag("lib"))
......@@ -15,6 +17,15 @@ def config
def inherited(base)
raise "You cannot inherit from a Rails::Engine child"
end
def instance
@instance ||= new
end
def endpoint(endpoint = nil)
@endpoint = endpoint if endpoint
@endpoint
end
end
def config
......@@ -22,4 +33,4 @@ def config
end
end
end
end
\ No newline at end of file
end
......@@ -5,10 +5,12 @@ class Engine
class Configuration < ::Rails::Railtie::Configuration
attr_reader :root
attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
attr_accessor :middleware
def initialize(root=nil)
super()
@root = root
@middleware = ActionDispatch::MiddlewareStack.new
end
def paths
......
......@@ -50,5 +50,40 @@ class Engine < ::Rails::Engine
assert index < initializers.index { |i| i.name == :build_middleware_stack }
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册