Class: ActionController::Metal
- Inherits:
-
AbstractController::Base
- Object
- AbstractController::Base
- ActionController::Metal
- Defined in:
- actionpack/lib/action_controller/metal.rb
Overview
ActionController::Metal 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::Metal 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).
Class Method Summary (collapse)
-
+ (Object) action(name, klass = ActionDispatch::Request)
Return a rack endpoint for the given action.
- + (Object) call(env)
-
+ (Object) controller_name
Returns the last part of the controller's name, underscored, without the ending "Controller".
- + (Object) inherited(base)
- + (Object) middleware
- + (Object) use(*args, &block)
Instance Method Summary (collapse)
- - (Object) content_type
-
- (Object) content_type=(type)
Basic implementations for content_type=, location=, and headers are provided to reduce the dependency on the RackDelegation module in Renderer and Redirector.
-
- (Object) controller_name
Delegates to the class' #controller_name.
-
- (Object) dispatch(name, request)
:api: private.
-
- (Metal) initialize
constructor
A new instance of Metal.
- - (Object) location
- - (Object) location=(url)
- - (Object) params
- - (Object) params=(val)
- - (Object) response_body=(val)
- - (Object) status
- - (Object) status=(status)
-
- (Object) to_a
:api: private.
-
- (Object) url_for(string)
basic url_for that can be overridden for more robust functionality.
Methods inherited from AbstractController::Base
abstract!, action_methods, #action_methods, clear_action_methods!, controller_path, #controller_path, hidden_actions, internal_methods, method_added, #process
Methods included from ActiveSupport::DescendantsTracker
clear, #descendants, descendants, #direct_descendants, direct_descendants, #inherited
Methods included from ActiveSupport::Configurable
Methods included from ActiveSupport::Concern
#append_features, extended, #included
Constructor Details
- (Metal) initialize
A new instance of Metal
81 82 83 84 85 |
# File 'actionpack/lib/action_controller/metal.rb', line 81 def initialize(*) @_headers = {"Content-Type" => "text/html"} @_status = 200 super end |
Class Method Details
+ (Object) action(name, klass = ActionDispatch::Request)
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 |
176 177 178 179 180 |
# File 'actionpack/lib/action_controller/metal.rb', line 176 def self.action(name, klass = ActionDispatch::Request) middleware_stack.build(name.to_s) do |env| new.dispatch(name, klass.new(env)) end end |
+ (Object) call(env)
163 164 165 |
# File 'actionpack/lib/action_controller/metal.rb', line 163 def self.call(env) action(env['action_dispatch.request.path_parameters'][:action]).call(env) end |
+ (Object) controller_name
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
63 64 65 |
# File 'actionpack/lib/action_controller/metal.rb', line 63 def self.controller_name @controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore end |
+ (Object) inherited(base)
150 151 152 153 |
# File 'actionpack/lib/action_controller/metal.rb', line 150 def self.inherited(base) base.middleware_stack = self.middleware_stack.dup super end |
+ (Object) middleware
159 160 161 |
# File 'actionpack/lib/action_controller/metal.rb', line 159 def self.middleware middleware_stack end |
+ (Object) use(*args, &block)
155 156 157 |
# File 'actionpack/lib/action_controller/metal.rb', line 155 def self.use(*args, &block) middleware_stack.use(*args, &block) end |
Instance Method Details
- (Object) content_type
103 104 105 |
# File 'actionpack/lib/action_controller/metal.rb', line 103 def content_type headers["Content-Type"] end |
- (Object) content_type=(type)
Basic implementations for content_type=, location=, and headers are provided to reduce the dependency on the RackDelegation module in Renderer and Redirector.
99 100 101 |
# File 'actionpack/lib/action_controller/metal.rb', line 99 def content_type=(type) headers["Content-Type"] = type.to_s end |
- (Object) controller_name
Delegates to the class' #controller_name
68 69 70 |
# File 'actionpack/lib/action_controller/metal.rb', line 68 def controller_name self.class.controller_name end |
- (Object) dispatch(name, request)
:api: private
134 135 136 137 138 139 140 |
# File 'actionpack/lib/action_controller/metal.rb', line 134 def dispatch(name, request) @_request = request @_env = request.env @_env['action_controller.instance'] = self process(name) to_a end |
- (Object) location
107 108 109 |
# File 'actionpack/lib/action_controller/metal.rb', line 107 def location headers["Location"] end |
- (Object) location=(url)
111 112 113 |
# File 'actionpack/lib/action_controller/metal.rb', line 111 def location=(url) headers["Location"] = url end |
- (Object) params
87 88 89 |
# File 'actionpack/lib/action_controller/metal.rb', line 87 def params @_params ||= request.parameters end |
- (Object) params=(val)
91 92 93 |
# File 'actionpack/lib/action_controller/metal.rb', line 91 def params=(val) @_params = val end |
- (Object) response_body=(val)
128 129 130 131 |
# File 'actionpack/lib/action_controller/metal.rb', line 128 def response_body=(val) body = val.respond_to?(:each) ? val : [val] super body end |
- (Object) status
120 121 122 |
# File 'actionpack/lib/action_controller/metal.rb', line 120 def status @_status end |
- (Object) status=(status)
124 125 126 |
# File 'actionpack/lib/action_controller/metal.rb', line 124 def status=(status) @_status = Rack::Utils.status_code(status) end |
- (Object) to_a
:api: private
143 144 145 |
# File 'actionpack/lib/action_controller/metal.rb', line 143 def to_a response ? response.to_a : [status, headers, response_body] end |
- (Object) url_for(string)
basic url_for that can be overridden for more robust functionality
116 117 118 |
# File 'actionpack/lib/action_controller/metal.rb', line 116 def url_for(string) string end |