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
ActionController::Metal
by default provides no utilities for rendering views, partials, or other responses aside from explicitly calling of 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
-
#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.
- #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:.
-
#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.
185 186 187 188 189 190 191 192 |
# File 'lib/action_controller/metal.rb', line 185 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.
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/action_controller/metal.rb', line 289 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
.
305 306 307 308 309 310 311 |
# File 'lib/action_controller/metal.rb', line 305 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 in the guides.
284 285 286 |
# File 'lib/action_controller/metal.rb', line 284 def self.middleware middleware_stack end |
.use ⇒ Object
Pushes the given Rack middleware and its arguments to the bottom of the middleware stack.
267 268 269 |
# File 'lib/action_controller/metal.rb', line 267 def use(...) middleware_stack.use(...) end |
Instance Method Details
#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:
224 225 226 227 228 229 230 |
# File 'lib/action_controller/metal.rb', line 224 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" |
#params ⇒ Object
194 195 196 |
# File 'lib/action_controller/metal.rb', line 194 def params @_params ||= request.parameters end |
#params=(val) ⇒ Object
198 199 200 |
# File 'lib/action_controller/metal.rb', line 198 def params=(val) @_params = val end |
#performed? ⇒ Boolean
Tests if render or redirect has already happened.
220 221 222 |
# File 'lib/action_controller/metal.rb', line 220 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
258 259 260 |
# File 'lib/action_controller/metal.rb', line 258 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.
242 243 244 245 246 247 |
# File 'lib/action_controller/metal.rb', line 242 def response=(response) set_response!(response) # Force `performed?` to return true: @_response_body = true end |
#response_body=(body) ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/action_controller/metal.rb', line 209 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.
176 |
# File 'lib/action_controller/metal.rb', line 176 delegate :session, to: "@_request" |
#set_request!(request) ⇒ Object
:nodoc:
249 250 251 252 |
# File 'lib/action_controller/metal.rb', line 249 def set_request!(request) # :nodoc: @_request = request @_request.controller_instance = self end |
#set_response!(response) ⇒ Object
:nodoc:
232 233 234 235 236 237 238 239 |
# File 'lib/action_controller/metal.rb', line 232 def set_response!(response) # :nodoc: if @_response _, _, body = @_response body.close if body.respond_to?(:close) end @_response = response end |
#to_a ⇒ Object
:nodoc:
254 255 256 |
# File 'lib/action_controller/metal.rb', line 254 def to_a # :nodoc: response.to_a end |
#url_for(string) ⇒ Object
Basic url_for that can be overridden for more robust functionality.
205 206 207 |
# File 'lib/action_controller/metal.rb', line 205 def url_for(string) string end |