123456789_123456789_123456789_123456789_123456789_

Class: ActionController::Metal

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Base, Middleware, Rails::ApplicationController, Rails::InfoController, Rails::MailersController, Rails::WelcomeController, ActionView::TestCase::TestController
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: AbstractController::Base
Defined in: actionpack/lib/action_controller/metal.rb

Overview

Metal is the simplest possible controller, providing a valid ::Rack interface without the additional niceties provided by 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

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 Base to see other features you can bring into your metal controller.

Class Attribute Summary

::AbstractController::Base - Inherited

.abstract?
.supports_path?

Returns true if the given controller is capable of rendering a path.

Class Method Summary

::AbstractController::Base - Inherited

.abstract,
.abstract!

Define a controller as abstract.

.action_methods

A list of method names that should be considered actions.

.clear_action_methods!

action_methods are cached and there is sometimes need to refresh them.

.controller_path

Returns the full controller name, underscored, without the ending Controller.

.hidden_actions

The list of hidden actions.

.internal_methods

A list of all internal methods for a controller.

.method_added

Refresh the cached action_methods when a new action_method is added.

::ActiveSupport::DescendantsTracker - Extended

Instance Attribute Summary

::AbstractController::Base - Inherited

Instance Method Summary

::AbstractController::Base - Inherited

#action_methods

Delegates to the class' #action_methods

#available_action?

Returns true if a method for the action is available and can be dispatched, false otherwise.

#controller_path

Delegates to the class' #controller_path

#process

Calls the action going through the entire action dispatch stack.

::ActiveSupport::Configurable - Included

#config

Reads and writes attributes from a configuration OrderedHash.

Constructor Details

.newMetal Also known as: .build

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 131

def initialize
  @_headers = {"Content-Type" => "text/html"}
  @_status = 200
  @_request = nil
  @_response = nil
  @_routes = nil
  super
end

Class Attribute Details

.middleware_stack (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 204

class_attribute :middleware_stack

.middleware_stack?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 204

class_attribute :middleware_stack

Class Method Details

.action(name, klass = ActionDispatch::Request)

Returns a ::Rack endpoint for the given action name.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 231

def self.action(name, klass = ActionDispatch::Request)
  if middleware_stack.any?
    middleware_stack.build(name) do |env|
      new.dispatch(name, klass.new(env))
    end
  else
    lambda { |env| new.dispatch(name, klass.new(env)) }
  end
end

.call(env)

Makes the controller a ::Rack endpoint that runs the action in the given #env's action_dispatch.request.path_parameters key.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 225

def self.call(env)
  req = ActionDispatch::Request.new env
  action(req.path_parameters[:action]).call(env)
end

.controller_name

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

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 113

def self.controller_name
  @controller_name ||= name.demodulize.sub(/Controller$/, '').underscore
end

.middleware

Alias for .middleware_stack.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 219

def self.middleware
  middleware_stack
end

.use(*args, &block)

Pushes the given ::Rack middleware and its arguments to the bottom of the middleware stack.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 214

def self.use(*args, &block)
  middleware_stack.use(*args, &block)
end

Instance Attribute Details

#content_type (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 156

def content_type
  headers["Content-Type"]
end

#content_type=(type) (rw)

Basic implementations for content_type=, location=, and headers are provided to reduce the dependency on the RackDelegation module in Renderer and Redirector.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 152

def content_type=(type)
  headers["Content-Type"] = type.to_s
end

#env (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 103

def env
  @_env ||= {}
end

#env=(value) (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 101

attr_internal_writer :env

#headers (rw)

The details below can be overridden to support a specific Request and Response object. The default Base implementation includes RackDelegation, which makes a request and response object available. You might wish to control the environment and response manually for performance reasons.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 128

attr_internal :headers, :response, :request

#location (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 160

def location
  headers["Location"]
end

#location=(url) (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 164

def location=(url)
  headers["Location"] = url
end

#middleware_stack (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 204

class_attribute :middleware_stack

#middleware_stack?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 204

class_attribute :middleware_stack

#params (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 140

def params
  @_params ||= request.parameters
end

#params=(val) (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 144

def params=(val)
  @_params = val
end

#performed?Boolean (readonly)

Tests if render or redirect has already happened.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 188

def performed?
  response_body || (response && response.committed?)
end

#request (rw)

The details below can be overridden to support a specific Request and Response object. The default Base implementation includes RackDelegation, which makes a request and response object available. You might wish to control the environment and response manually for performance reasons.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 128

attr_internal :headers, :response, :request

#response (rw)

The details below can be overridden to support a specific Request and Response object. The default Base implementation includes RackDelegation, which makes a request and response object available. You might wish to control the environment and response manually for performance reasons.

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 128

attr_internal :headers, :response, :request

#response_body=(body) (writeonly)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 182

def response_body=(body)
  body = [body] unless body.nil? || body.respond_to?(:each)
  super
end

#session (readonly)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 129

delegate :session, :to => "@_request"

#status (rw) Also known as: #response_code

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 173

def status
  @_status
end

#status=(status) (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 178

def status=(status)
  @_status = Rack::Utils.status_code(status)
end

Instance Method Details

#controller_name

Delegates to the class' controller_name

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 118

def controller_name
  self.class.controller_name
end

#url_for(string)

Basic url_for that can be overridden for more robust functionality

[ GitHub ]

  
# File 'actionpack/lib/action_controller/metal.rb', line 169

def url_for(string)
  string
end