Class: ActionController::Metal

Inherits:
AbstractController::Base show all
Defined in:
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).

Direct Known Subclasses

Base, Middleware

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractController::Base

abstract!, #action_methods, action_methods, clear_action_methods!, controller_path, #controller_path, hidden_actions, internal_methods, method_added, #process

Constructor Details

#initializeMetal

Returns a new instance of Metal.



81
82
83
84
85
# File 'lib/action_controller/metal.rb', line 81

def initialize(*)
  @_headers = {"Content-Type" => "text/html"}
  @_status = 200
  super
end

Class Method Details

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

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



171
172
173
174
175
# File 'lib/action_controller/metal.rb', line 171

def self.action(name, klass = ActionDispatch::Request)
  middleware_stack.build(name.to_s) do |env|
    new.dispatch(name, klass.new(env))
  end
end

.call(env) ⇒ Object



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

def self.call(env)
  action(env['action_dispatch.request.path_parameters'][:action]).call(env)
end

.controller_nameObject

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 'lib/action_controller/metal.rb', line 63

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

.inherited(base) ⇒ Object



145
146
147
148
# File 'lib/action_controller/metal.rb', line 145

def self.inherited(base)
  base.middleware_stack = self.middleware_stack.dup
  super
end

.middlewareObject



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

def self.middleware
  middleware_stack
end

.use(*args) ⇒ Object



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

def self.use(*args)
  middleware_stack.use(*args)
end

Instance Method Details

#content_typeObject



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

def content_type
  headers["Content-Type"]
end

#content_type=(type) ⇒ Object

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 'lib/action_controller/metal.rb', line 99

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

#controller_nameObject

Delegates to the class’ #controller_name



68
69
70
# File 'lib/action_controller/metal.rb', line 68

def controller_name
  self.class.controller_name
end

#dispatch(name, request) ⇒ Object

:api: private



129
130
131
132
133
134
135
# File 'lib/action_controller/metal.rb', line 129

def dispatch(name, request)
  @_request = request
  @_env = request.env
  @_env['action_controller.instance'] = self
  process(name)
  to_a
end

#locationObject



107
108
109
# File 'lib/action_controller/metal.rb', line 107

def location
  headers["Location"]
end

#location=(url) ⇒ Object



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

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

#paramsObject



87
88
89
# File 'lib/action_controller/metal.rb', line 87

def params
  @_params ||= request.parameters
end

#params=(val) ⇒ Object



91
92
93
# File 'lib/action_controller/metal.rb', line 91

def params=(val)
  @_params = val
end

#response_body=(val) ⇒ Object



123
124
125
126
# File 'lib/action_controller/metal.rb', line 123

def response_body=(val)
  body = val.respond_to?(:each) ? val : [val]
  super body
end

#statusObject



115
116
117
# File 'lib/action_controller/metal.rb', line 115

def status
  @_status
end

#status=(status) ⇒ Object



119
120
121
# File 'lib/action_controller/metal.rb', line 119

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

#to_aObject

:api: private



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

def to_a
  response ? response.to_a : [status, headers, response_body]
end