2009-05-14 04:56:07 -04:00
|
|
|
require 'action_controller/abstract'
|
|
|
|
require 'active_support/core_ext/module/delegation'
|
|
|
|
|
2009-05-01 20:27:44 -04:00
|
|
|
module ActionController
|
2009-06-10 18:27:53 -04:00
|
|
|
# ActionController::Http provides a way to get a valid Rack application from a controller.
|
|
|
|
#
|
|
|
|
# In AbstractController, dispatching is triggered directly by calling #process on a new controller.
|
|
|
|
# ActionController::Http provides an #action 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 the action returned by FooController.action(:index).
|
2009-05-01 20:27:44 -04:00
|
|
|
class Http < AbstractController::Base
|
|
|
|
abstract!
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-01 20:27:44 -04:00
|
|
|
# :api: public
|
2009-05-27 04:40:43 -04:00
|
|
|
attr_internal :params, :env
|
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
|
|
|
|
# "Controller". For instance, MyApp::MyPostsController would return "my_posts" for
|
|
|
|
# controller_name
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# String
|
2009-05-01 20:27:44 -04:00
|
|
|
def self.controller_name
|
|
|
|
@controller_name ||= controller_path.split("/").last
|
|
|
|
end
|
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# Delegates to the class' #controller_name
|
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-06-10 18:27:53 -04:00
|
|
|
# Returns the full controller name, underscored, without the ending Controller.
|
|
|
|
# For instance, MyApp::MyPostsController would return "my_app/my_posts" for
|
|
|
|
# controller_name.
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# String
|
2009-05-01 20:27:44 -04:00
|
|
|
def self.controller_path
|
|
|
|
@controller_path ||= self.name.sub(/Controller$/, '').underscore
|
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# Delegates to the class' #controller_path
|
2009-05-28 10:49:02 -04:00
|
|
|
def controller_path
|
|
|
|
self.class.controller_path
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
# implementation includes RackConvenience, which makes a request
|
|
|
|
# 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
|
|
|
|
2009-05-27 04:40:43 -04:00
|
|
|
attr_internal :status, :headers, :content_type
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
@_headers = {}
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-06-10 18:27:53 -04:00
|
|
|
# Basic implementations for content_type=, location=, and headers are
|
2009-05-27 04:40:43 -04:00
|
|
|
# provided to reduce the dependency on the RackConvenience module
|
|
|
|
# in Renderer and Redirector.
|
|
|
|
|
|
|
|
def content_type=(type)
|
|
|
|
headers["Content-Type"] = type.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def location=(url)
|
|
|
|
headers["Location"] = url
|
2009-05-12 19:21:34 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-01 20:27:44 -04:00
|
|
|
# :api: private
|
2009-05-02 05:15:09 -04:00
|
|
|
def call(name, env)
|
2009-05-27 04:40:43 -04:00
|
|
|
@_env = env
|
2009-05-02 05:15:09 -04:00
|
|
|
process(name)
|
|
|
|
to_rack
|
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-27 04:40:43 -04:00
|
|
|
# :api: private
|
|
|
|
def to_rack
|
|
|
|
[status, headers, response_body]
|
|
|
|
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
|
|
|
|
# action<#to_s>:: An action name
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# Proc:: A rack application
|
2009-05-02 05:15:09 -04:00
|
|
|
def self.action(name)
|
|
|
|
@actions ||= {}
|
2009-05-14 04:56:07 -04:00
|
|
|
@actions[name.to_s] ||= proc do |env|
|
2009-05-02 05:15:09 -04:00
|
|
|
new.call(name, env)
|
|
|
|
end
|
2009-05-01 20:27:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|