Skip to content
Browse files

Made Engine valid rack app with its own middleware stack

  • Loading branch information...
1 parent f7af759 commit ad6be0876271f86e76c89645a0106b85c9d77ad7 @drogus drogus committed
View
2 railties/lib/rails/application/configuration.rb
@@ -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
View
4 railties/lib/rails/application/railties.rb
@@ -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
+end
View
13 railties/lib/rails/engine.rb
@@ -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|
View
13 railties/lib/rails/engine/configurable.rb
@@ -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
+end
View
2 railties/lib/rails/engine/configuration.rb
@@ -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
View
35 railties/test/railties/engine_test.rb
@@ -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

0 comments on commit ad6be08

Please sign in to comment.
Something went wrong with that request. Please try again.