2010-01-31 21:32:28 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
2010-05-30 09:53:14 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
|
|
|
require 'action_dispatch/middleware/stack'
|
2009-09-14 15:47:31 -04:00
|
|
|
|
2009-05-01 20:27:44 -04:00
|
|
|
module ActionController
|
2010-05-30 09:53:14 -04:00
|
|
|
# Extend ActionDispatch middleware stack to make it aware of options
|
|
|
|
# allowing the following syntax in controllers:
|
|
|
|
#
|
|
|
|
# class PostsController < ApplicationController
|
|
|
|
# use AuthenticationMiddleware, :except => [:index, :show]
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class MiddlewareStack < ActionDispatch::MiddlewareStack #:nodoc:
|
|
|
|
class Middleware < ActionDispatch::MiddlewareStack::Middleware #:nodoc:
|
2010-09-11 17:30:21 -04:00
|
|
|
def initialize(klass, *args, &block)
|
2010-05-30 09:53:14 -04:00
|
|
|
options = args.extract_options!
|
|
|
|
@only = Array(options.delete(:only)).map(&:to_s)
|
|
|
|
@except = Array(options.delete(:except)).map(&:to_s)
|
|
|
|
args << options unless options.empty?
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?(action)
|
|
|
|
if @only.present?
|
|
|
|
@only.include?(action)
|
|
|
|
elsif @except.present?
|
|
|
|
!@except.include?(action)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(action, app=nil, &block)
|
|
|
|
app ||= block
|
|
|
|
action = action.to_s
|
|
|
|
raise "MiddlewareStack#build requires an app" unless app
|
|
|
|
|
|
|
|
reverse.inject(app) do |a, middleware|
|
|
|
|
middleware.valid?(action) ?
|
|
|
|
middleware.build(a) : a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-26 16:32:40 -04:00
|
|
|
# Provides a way to get a valid Rack application from a controller.
|
2009-06-10 18:27:53 -04:00
|
|
|
#
|
|
|
|
# In AbstractController, dispatching is triggered directly by calling #process on a new controller.
|
2010-08-26 16:32:40 -04:00
|
|
|
# <tt>ActionController::Metal</tt> provides an <tt>action</tt> method that returns a valid Rack application for a
|
|
|
|
# given action. Other rack builders, such as Rack::Builder, Rack::URLMap, and the \Rails router,
|
|
|
|
# can dispatch directly to actions returned by controllers in your application.
|
2009-08-06 18:50:22 -04:00
|
|
|
class Metal < AbstractController::Base
|
2009-05-01 20:27:44 -04:00
|
|
|
abstract!
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2010-07-14 03:17:24 -04:00
|
|
|
attr_internal_writer :env
|
|
|
|
|
|
|
|
def env
|
|
|
|
@_env ||= {}
|
|
|
|
end
|
2009-05-01 20:27:44 -04:00
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# Returns the last part of the controller's name, underscored, without the ending
|
2010-08-26 16:32:40 -04:00
|
|
|
# <tt>Controller</tt>. For instance, PostsController returns <tt>posts</tt>.
|
|
|
|
# Namespaces are left out, so Admin::PostsController returns <tt>posts</tt> as well.
|
2009-06-10 18:27:53 -04:00
|
|
|
#
|
|
|
|
# ==== Returns
|
2010-08-26 16:32:40 -04:00
|
|
|
# * <tt>string</tt>
|
2009-05-01 20:27:44 -04:00
|
|
|
def self.controller_name
|
2010-06-22 14:47:31 -04:00
|
|
|
@controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
|
2009-05-01 20:27:44 -04:00
|
|
|
end
|
|
|
|
|
2010-08-26 16:32:40 -04:00
|
|
|
# Delegates to the class' <tt>controller_name</tt>
|
2009-05-28 10:49:02 -04:00
|
|
|
def controller_name
|
|
|
|
self.class.controller_name
|
|
|
|
end
|
2009-05-01 20:27:44 -04:00
|
|
|
|
2009-05-27 04:40:43 -04:00
|
|
|
# The details below can be overridden to support a specific
|
|
|
|
# Request and Response object. The default ActionController::Base
|
2009-12-20 21:05:26 -05:00
|
|
|
# implementation includes RackDelegation, which makes a request
|
2009-05-27 04:40:43 -04:00
|
|
|
# and response object available. You might wish to control the
|
|
|
|
# environment and response manually for performance reasons.
|
2009-05-20 19:38:48 -04:00
|
|
|
|
2010-03-17 02:24:00 -04:00
|
|
|
attr_internal :headers, :response, :request
|
2010-03-08 19:49:47 -05:00
|
|
|
delegate :session, :to => "@_request"
|
2009-05-27 04:40:43 -04:00
|
|
|
|
|
|
|
def initialize(*)
|
2010-03-18 20:32:53 -04:00
|
|
|
@_headers = {"Content-Type" => "text/html"}
|
|
|
|
@_status = 200
|
2009-05-27 04:40:43 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-06-19 11:19:00 -04:00
|
|
|
def params
|
|
|
|
@_params ||= request.parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def params=(val)
|
|
|
|
@_params = val
|
|
|
|
end
|
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# Basic implementations for content_type=, location=, and headers are
|
2009-12-20 21:05:26 -05:00
|
|
|
# provided to reduce the dependency on the RackDelegation module
|
|
|
|
# in Renderer and Redirector.
|
2010-08-26 16:32:40 -04:00
|
|
|
|
2009-05-27 04:40:43 -04:00
|
|
|
def content_type=(type)
|
|
|
|
headers["Content-Type"] = type.to_s
|
|
|
|
end
|
|
|
|
|
2010-02-16 13:45:59 -05:00
|
|
|
def content_type
|
|
|
|
headers["Content-Type"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def location
|
|
|
|
headers["Location"]
|
|
|
|
end
|
|
|
|
|
2009-05-27 04:40:43 -04:00
|
|
|
def location=(url)
|
|
|
|
headers["Location"] = url
|
2009-05-12 19:21:34 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2010-03-17 02:24:00 -04:00
|
|
|
def status
|
|
|
|
@_status
|
|
|
|
end
|
|
|
|
|
2009-12-20 21:50:47 -05:00
|
|
|
def status=(status)
|
2009-12-22 17:08:03 -05:00
|
|
|
@_status = Rack::Utils.status_code(status)
|
2009-12-20 21:50:47 -05:00
|
|
|
end
|
|
|
|
|
2010-03-18 20:32:53 -04:00
|
|
|
def response_body=(val)
|
|
|
|
body = val.respond_to?(:each) ? val : [val]
|
|
|
|
super body
|
|
|
|
end
|
|
|
|
|
2010-08-26 16:32:40 -04:00
|
|
|
def dispatch(name, request) #:nodoc:
|
2010-03-08 19:49:47 -05:00
|
|
|
@_request = request
|
|
|
|
@_env = request.env
|
2010-01-20 09:59:26 -05:00
|
|
|
@_env['action_controller.instance'] = self
|
2009-05-02 05:15:09 -04:00
|
|
|
process(name)
|
2009-08-06 19:05:14 -04:00
|
|
|
to_a
|
2009-05-02 05:15:09 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2010-08-26 16:32:40 -04:00
|
|
|
def to_a #:nodoc:
|
2009-08-25 15:14:31 -04:00
|
|
|
response ? response.to_a : [status, headers, response_body]
|
|
|
|
end
|
|
|
|
|
2010-01-31 21:32:28 -05:00
|
|
|
class_attribute :middleware_stack
|
2010-05-30 09:53:14 -04:00
|
|
|
self.middleware_stack = ActionController::MiddlewareStack.new
|
2010-01-31 21:32:28 -05:00
|
|
|
|
|
|
|
def self.inherited(base)
|
2010-05-30 09:53:14 -04:00
|
|
|
base.middleware_stack = self.middleware_stack.dup
|
2010-03-03 03:25:41 -05:00
|
|
|
super
|
2010-01-31 21:32:28 -05:00
|
|
|
end
|
2009-09-12 14:51:15 -04:00
|
|
|
|
2010-09-11 17:30:21 -04:00
|
|
|
def self.use(*args, &block)
|
|
|
|
middleware_stack.use(*args, &block)
|
2009-09-12 14:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.middleware
|
|
|
|
middleware_stack
|
|
|
|
end
|
|
|
|
|
2009-10-20 17:03:55 -04:00
|
|
|
def self.call(env)
|
|
|
|
action(env['action_dispatch.request.path_parameters'][:action]).call(env)
|
|
|
|
end
|
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# Return a rack endpoint for the given action. Memoize the endpoint, so
|
|
|
|
# multiple calls into MyController.action will return the same object
|
|
|
|
# for the same action.
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
2010-08-26 16:32:40 -04:00
|
|
|
# * <tt>action</tt> - An action name
|
2009-06-10 18:27:53 -04:00
|
|
|
#
|
|
|
|
# ==== Returns
|
2010-08-26 16:32:40 -04:00
|
|
|
# * <tt>proc</tt> - A rack application
|
2010-03-08 19:49:47 -05:00
|
|
|
def self.action(name, klass = ActionDispatch::Request)
|
2010-05-30 09:53:14 -04:00
|
|
|
middleware_stack.build(name.to_s) do |env|
|
2010-03-08 19:49:47 -05:00
|
|
|
new.dispatch(name, klass.new(env))
|
|
|
|
end
|
2009-08-25 15:14:31 -04:00
|
|
|
end
|
2009-05-01 20:27:44 -04:00
|
|
|
end
|
|
|
|
end
|