aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/metal.rb
diff options
context:
space:
mode:
authorYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-08-25 12:14:31 -0700
committerYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-08-25 12:14:31 -0700
commitc7ba911a43e513bd1adbee93f16d2b8efea7cc88 (patch)
tree0a3f88da172575fb9d85980975cbbe41ed6bd53c /actionpack/lib/action_controller/metal.rb
parent09fde6440a729e169e51c04f7caf0d19fe949c1d (diff)
downloadrails-c7ba911a43e513bd1adbee93f16d2b8efea7cc88.tar.gz
rails-c7ba911a43e513bd1adbee93f16d2b8efea7cc88.tar.bz2
rails-c7ba911a43e513bd1adbee93f16d2b8efea7cc88.zip
ActionController::Metal can be a middleware
Diffstat (limited to 'actionpack/lib/action_controller/metal.rb')
-rw-r--r--actionpack/lib/action_controller/metal.rb40
1 files changed, 35 insertions, 5 deletions
diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb
index 5333ca497c..aad9570237 100644
--- a/actionpack/lib/action_controller/metal.rb
+++ b/actionpack/lib/action_controller/metal.rb
@@ -47,7 +47,7 @@ module ActionController
# and response object available. You might wish to control the
# environment and response manually for performance reasons.
- attr_internal :status, :headers, :content_type
+ attr_internal :status, :headers, :content_type, :app, :response
def initialize(*)
@_headers = {}
@@ -75,7 +75,34 @@ module ActionController
# :api: private
def to_a
- [status, headers, response_body]
+ response ? response.to_a : [status, headers, response_body]
+ end
+
+ class ActionEndpoint
+ def initialize(controller, action)
+ @controller, @action = controller, action
+ end
+
+ def call(env)
+ controller = @controller.new.call(@action, env)
+ end
+ end
+
+ class ActionMiddleware
+ def initialize(controller, action)
+ @controller, @action = controller, action
+ end
+
+ def call(env)
+ controller = @controller.new
+ controller.app = @app
+ controller.call(@action, env)
+ end
+
+ def new(app)
+ @app = app
+ self
+ end
end
# Return a rack endpoint for the given action. Memoize the endpoint, so
@@ -89,9 +116,12 @@ module ActionController
# Proc:: A rack application
def self.action(name)
@actions ||= {}
- @actions[name.to_s] ||= proc do |env|
- new.call(name, env)
- end
+ @actions[name.to_s] ||= ActionEndpoint.new(self, name)
+ end
+
+ def self.middleware(name)
+ @middlewares ||= {}
+ @middlewares[name.to_s] ||= ActionMiddleware.new(self, name)
end
end
end