Class: ActionController::Metal
- Inherits:
-
AbstractController::Base
- Object
- AbstractController::Base
- ActionController::Metal
- Includes:
- Testing::Functional
- Defined in:
- lib/action_controller/metal.rb,
lib/action_controller/test_case.rb
Overview
# Action Controller Metal
‘ActionController::Metal` is the simplest possible controller, providing a valid Rack interface without the additional niceties provided by ActionController::Base.
A sample metal controller might look like this:
class HelloController < ActionController::Metal
def index
self.response_body = "Hello World!"
end
end
And then to route requests to your metal controller, you would add something like this to ‘config/routes.rb`:
get 'hello', to: HelloController.action(:index)
The ::action method returns a valid Rack application for the Rails router to dispatch to.
## Rendering Helpers
By default, ‘ActionController::Metal` provides no utilities for rendering views, partials, or other responses aside from some low-level setters such as #response_body=, #content_type=, and #status=. To add the render helpers you’re used to having in a normal controller, you can do the following:
class HelloController < ActionController::Metal
include AbstractController::Rendering
include ActionView::Layouts
append_view_path "#{Rails.root}/app/views"
def index
render "hello/index"
end
end
## Redirection Helpers
To add redirection helpers to your metal controller, do the following:
class HelloController < ActionController::Metal
include ActionController::Redirecting
include Rails.application.routes.url_helpers
def index
redirect_to root_url
end
end
## Other Helpers
You can refer to the modules included in ActionController::Base to see other features you can bring into your metal controller.
Class Method Summary collapse
-
.action(name) ⇒ Object
Returns a Rack endpoint for the given action name.
-
.action_encoding_template(action) ⇒ Object
:nodoc:.
-
.controller_name ⇒ Object
Returns the last part of the controller’s name, underscored, without the ending ‘Controller`.
-
.dispatch(name, req, res) ⇒ Object
Direct dispatch to the controller.
- .make_response!(request) ⇒ Object
-
.middleware ⇒ Object
The middleware stack used by this controller.
-
.use ⇒ Object
Pushes the given Rack middleware and its arguments to the bottom of the middleware stack.
Instance Method Summary collapse
-
#content_type ⇒ Object
Delegates to ActionDispatch::Response#content_type.
-
#content_type= ⇒ Object
Delegates to ActionDispatch::Response#content_type=.
-
#controller_name ⇒ Object
Delegates to the class’s ::controller_name.
-
#dispatch(name, request, response) ⇒ Object
:nodoc:.
-
#headers ⇒ Object
Delegates to ActionDispatch::Response#headers.
-
#initialize ⇒ Metal
constructor
A new instance of Metal.
-
#location ⇒ Object
Delegates to ActionDispatch::Response#location.
-
#location= ⇒ Object
Delegates to ActionDispatch::Response#location=.
-
#media_type ⇒ Object
Delegates to ActionDispatch::Response#media_type.
- #params ⇒ Object
- #params=(val) ⇒ Object
-
#performed? ⇒ Boolean
Tests if render or redirect has already happened.
-
#request ⇒ Object
:attr_reader: request.
- #reset_session ⇒ Object
-
#response ⇒ Object
:attr_reader: response.
-
#response=(response) ⇒ Object
Assign the response and mark it as committed.
- #response_body=(body) ⇒ Object
-
#session ⇒ Object
The ActionDispatch::Request::Session instance for the current request.
-
#set_request!(request) ⇒ Object
:nodoc:.
-
#set_response!(response) ⇒ Object
:nodoc:.
-
#status ⇒ Object
(also: #response_code)
Delegates to ActionDispatch::Response#status.
-
#status= ⇒ Object
Delegates to ActionDispatch::Response#status=.
-
#to_a ⇒ Object
:nodoc:.
-
#url_for(string) ⇒ Object
Basic ‘url_for` that can be overridden for more robust functionality.
Methods included from Testing::Functional
#clear_instance_variables_between_requests, #recycle!
Methods inherited from AbstractController::Base
abstract!, action_methods, #action_methods, #action_name, #available_action?, clear_action_methods!, #controller_path, controller_path, eager_load!, #formats, #inspect, internal_methods, method_added, #process, #response_body, supports_path?
Constructor Details
#initialize ⇒ Metal
Returns a new instance of Metal.
210 211 212 213 214 215 216 217 |
# File 'lib/action_controller/metal.rb', line 210 def initialize @_request = nil @_response = nil @_response_body = nil @_routes = nil @_params = nil super end |
Class Method Details
.action(name) ⇒ Object
Returns a Rack endpoint for the given action name.
315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/action_controller/metal.rb', line 315 def self.action(name) app = lambda { |env| req = ActionDispatch::Request.new(env) res = make_response! req new.dispatch(name, req, res) } if middleware_stack.any? middleware_stack.build(name, app) else app end end |
.action_encoding_template(action) ⇒ Object
:nodoc:
140 141 142 |
# File 'lib/action_controller/metal.rb', line 140 def self.action_encoding_template(action) # :nodoc: false end |
.controller_name ⇒ Object
Returns the last part of the controller’s name, underscored, without the ending ‘Controller`. For instance, `PostsController` returns `posts`. Namespaces are left out, so `Admin::PostsController` returns `posts` as well.
#### Returns
-
‘string`
130 131 132 |
# File 'lib/action_controller/metal.rb', line 130 def self.controller_name @controller_name ||= (name.demodulize.delete_suffix("Controller").underscore unless anonymous?) end |
.dispatch(name, req, res) ⇒ Object
Direct dispatch to the controller. Instantiates the controller, then executes the action named ‘name`.
331 332 333 334 335 336 337 |
# File 'lib/action_controller/metal.rb', line 331 def self.dispatch(name, req, res) if middleware_stack.any? middleware_stack.build(name) { |env| new.dispatch(name, req, res) }.call req.env else new.dispatch(name, req, res) end end |
.make_response!(request) ⇒ Object
134 135 136 137 138 |
# File 'lib/action_controller/metal.rb', line 134 def self.make_response!(request) ActionDispatch::Response.new.tap do |res| res.request = request end end |
.middleware ⇒ Object
The middleware stack used by this controller.
By default uses a variation of ActionDispatch::MiddlewareStack which allows for the following syntax:
class PostsController < ApplicationController
use AuthenticationMiddleware, except: [:index, :show]
end
Read more about [Rails middleware stack] (guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack) in the guides.
310 311 312 |
# File 'lib/action_controller/metal.rb', line 310 def self.middleware middleware_stack end |
.use ⇒ Object
Pushes the given Rack middleware and its arguments to the bottom of the middleware stack.
293 294 295 |
# File 'lib/action_controller/metal.rb', line 293 def use(...) middleware_stack.use(...) end |
Instance Method Details
#content_type ⇒ Object
Delegates to ActionDispatch::Response#content_type
204 |
# File 'lib/action_controller/metal.rb', line 204 delegate :content_type, to: "@_response" |
#content_type= ⇒ Object
Delegates to ActionDispatch::Response#content_type=
192 |
# File 'lib/action_controller/metal.rb', line 192 delegate :content_type=, to: "@_response" |
#controller_name ⇒ Object
Delegates to the class’s ::controller_name.
156 157 158 |
# File 'lib/action_controller/metal.rb', line 156 def controller_name self.class.controller_name end |
#dispatch(name, request, response) ⇒ Object
:nodoc:
249 250 251 252 253 254 255 |
# File 'lib/action_controller/metal.rb', line 249 def dispatch(name, request, response) # :nodoc: set_request!(request) set_response!(response) process(name) request.commit_flash to_a end |
#headers ⇒ Object
Delegates to ActionDispatch::Response#headers.
180 |
# File 'lib/action_controller/metal.rb', line 180 delegate :headers, to: "@_response" |
#location ⇒ Object
Delegates to ActionDispatch::Response#location
200 |
# File 'lib/action_controller/metal.rb', line 200 delegate :location, to: "@_response" |
#location= ⇒ Object
Delegates to ActionDispatch::Response#location=
188 |
# File 'lib/action_controller/metal.rb', line 188 delegate :location=, to: "@_response" |
#media_type ⇒ Object
Delegates to ActionDispatch::Response#media_type
208 |
# File 'lib/action_controller/metal.rb', line 208 delegate :media_type, to: "@_response" |
#params ⇒ Object
219 220 221 |
# File 'lib/action_controller/metal.rb', line 219 def params @_params ||= request.parameters end |
#params=(val) ⇒ Object
223 224 225 |
# File 'lib/action_controller/metal.rb', line 223 def params=(val) @_params = val end |
#performed? ⇒ Boolean
Tests if render or redirect has already happened.
245 246 247 |
# File 'lib/action_controller/metal.rb', line 245 def performed? response_body || response.committed? end |
#request ⇒ Object
:attr_reader: request
The ActionDispatch::Request instance for the current request.
164 |
# File 'lib/action_controller/metal.rb', line 164 attr_internal :request |
#reset_session ⇒ Object
284 285 286 |
# File 'lib/action_controller/metal.rb', line 284 def reset_session @_request.reset_session end |
#response ⇒ Object
:attr_reader: response
The ActionDispatch::Response instance for the current response.
170 |
# File 'lib/action_controller/metal.rb', line 170 attr_internal_reader :response |
#response=(response) ⇒ Object
Assign the response and mark it as committed. No further processing will occur.
268 269 270 271 272 273 |
# File 'lib/action_controller/metal.rb', line 268 def response=(response) set_response!(response) # Force `performed?` to return true: @_response_body = true end |
#response_body=(body) ⇒ Object
234 235 236 237 238 239 240 241 242 |
# File 'lib/action_controller/metal.rb', line 234 def response_body=(body) if body body = [body] if body.is_a?(String) response.body = body super else response.reset_body! end end |
#session ⇒ Object
The ActionDispatch::Request::Session instance for the current request. See further details in the [Active Controller Session guide](guides.rubyonrails.org/action_controller_overview.html#session).
176 |
# File 'lib/action_controller/metal.rb', line 176 delegate :session, to: "@_request" |
#set_request!(request) ⇒ Object
:nodoc:
275 276 277 278 |
# File 'lib/action_controller/metal.rb', line 275 def set_request!(request) # :nodoc: @_request = request @_request.controller_instance = self end |
#set_response!(response) ⇒ Object
:nodoc:
257 258 259 260 261 262 263 264 |
# File 'lib/action_controller/metal.rb', line 257 def set_response!(response) # :nodoc: if @_response _, _, body = @_response body.close if body.respond_to?(:close) end @_response = response end |
#status ⇒ Object Also known as: response_code
Delegates to ActionDispatch::Response#status
196 |
# File 'lib/action_controller/metal.rb', line 196 delegate :status, to: "@_response" |
#status= ⇒ Object
Delegates to ActionDispatch::Response#status=
184 |
# File 'lib/action_controller/metal.rb', line 184 delegate :status=, to: "@_response" |
#to_a ⇒ Object
:nodoc:
280 281 282 |
# File 'lib/action_controller/metal.rb', line 280 def to_a # :nodoc: response.to_a end |
#url_for(string) ⇒ Object
Basic ‘url_for` that can be overridden for more robust functionality.
230 231 232 |
# File 'lib/action_controller/metal.rb', line 230 def url_for(string) string end |