mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Implement FooController.action(:name)
* Rails actions are now Rack endpoints, and can be retrieved via FooController.action(name) and called with an env * Updated some tests that relied on the old internal #process/#call implementation
This commit is contained in:
parent
ad2a1b5cb1
commit
72160d9f89
7 changed files with 57 additions and 34 deletions
|
@ -370,18 +370,18 @@ module ActionController #:nodoc:
|
|||
attr_reader :template
|
||||
|
||||
class << self
|
||||
def call(env)
|
||||
# HACK: For global rescue to have access to the original request and response
|
||||
request = env["action_controller.rescue.request"] ||= ActionDispatch::Request.new(env)
|
||||
response = env["action_controller.rescue.response"] ||= ActionDispatch::Response.new
|
||||
process(request, response)
|
||||
def action(name = nil)
|
||||
@actions ||= {}
|
||||
@actions[name] ||= proc do |env|
|
||||
controller = new
|
||||
# HACK: For global rescue to have access to the original request and response
|
||||
request = env["action_controller.rescue.request"] ||= ActionDispatch::Request.new(env)
|
||||
response = env["action_controller.rescue.response"] ||= ActionDispatch::Response.new
|
||||
controller.action_name = name && name.to_s
|
||||
controller.process(request, response).to_a
|
||||
end
|
||||
end
|
||||
|
||||
# Factory for the standard create, process loop where the controller is discarded after processing.
|
||||
def process(request, response) #:nodoc:
|
||||
new.process(request, response)
|
||||
end
|
||||
|
||||
|
||||
# Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
|
||||
def controller_class_name
|
||||
@controller_class_name ||= name.demodulize
|
||||
|
@ -518,7 +518,6 @@ module ActionController #:nodoc:
|
|||
assign_shortcuts(request, response)
|
||||
initialize_template_class(response)
|
||||
initialize_current_url
|
||||
assign_names
|
||||
|
||||
log_processing
|
||||
send(method, *arguments)
|
||||
|
@ -883,10 +882,6 @@ module ActionController #:nodoc:
|
|||
@performed_render || @performed_redirect
|
||||
end
|
||||
|
||||
def assign_names
|
||||
@action_name = (params['action'] || 'index')
|
||||
end
|
||||
|
||||
def reset_variables_added_to_assigns
|
||||
@template.instance_variable_set("@assigns_added", nil)
|
||||
end
|
||||
|
|
|
@ -39,13 +39,20 @@ module ActionController
|
|||
end
|
||||
|
||||
# :api: private
|
||||
def call(env)
|
||||
def call(name, env)
|
||||
@_request = ActionDispatch::Request.new(env)
|
||||
@_response = ActionDispatch::Response.new
|
||||
process(@_request.parameters[:action])
|
||||
process(name)
|
||||
@_response.body = response_body
|
||||
@_response.prepare!
|
||||
self
|
||||
to_rack
|
||||
end
|
||||
|
||||
def self.action(name)
|
||||
@actions ||= {}
|
||||
@actions[name] ||= proc do |env|
|
||||
new.call(name, env)
|
||||
end
|
||||
end
|
||||
|
||||
# :api: private
|
||||
|
|
|
@ -430,7 +430,7 @@ module ActionController
|
|||
def call(env)
|
||||
request = ActionDispatch::Request.new(env)
|
||||
app = Routing::Routes.recognize(request)
|
||||
app.call(env).to_a
|
||||
app.action(request.parameters[:action] || 'index').call(env)
|
||||
end
|
||||
|
||||
def recognize(request)
|
||||
|
|
|
@ -131,7 +131,11 @@ module ActionController #:nodoc:
|
|||
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
|
||||
build_request_uri(action, parameters)
|
||||
|
||||
@request.env["action_controller.rescue.request"] = @request
|
||||
@request.env["action_controller.rescue.request"] = @response
|
||||
|
||||
Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
|
||||
@controller.action_name = action.to_s
|
||||
@controller.process(@request, @response)
|
||||
end
|
||||
|
||||
|
|
|
@ -602,8 +602,9 @@ class FilterTest < Test::Unit::TestCase
|
|||
def test_dynamic_dispatch
|
||||
%w(foo bar baz).each do |action|
|
||||
request = ActionController::TestRequest.new
|
||||
request.env["action_controller.rescue.request"] = request
|
||||
request.query_parameters[:choose] = action
|
||||
response = DynamicDispatchController.process(request, ActionController::TestResponse.new)
|
||||
response = DynamicDispatchController.action.call(request.env).last
|
||||
assert_equal action, response.body
|
||||
end
|
||||
end
|
||||
|
|
|
@ -101,20 +101,30 @@ class HelperTest < Test::Unit::TestCase
|
|||
assert master_helper_methods.include?('delegate_attr=')
|
||||
end
|
||||
|
||||
def test_helper_for_nested_controller
|
||||
def call_controller(klass, action)
|
||||
request = ActionController::TestRequest.new
|
||||
response = ActionController::TestResponse.new
|
||||
request.action = 'render_hello_world'
|
||||
request.env["action_controller.rescue.request"] = request
|
||||
klass.action(action).call(request.env)
|
||||
end
|
||||
|
||||
assert_equal 'hello: Iz guuut!', Fun::GamesController.process(request, response).body
|
||||
def test_helper_for_nested_controller
|
||||
assert_equal 'hello: Iz guuut!',
|
||||
call_controller(Fun::GamesController, "render_hello_world").last.body
|
||||
# request = ActionController::TestRequest.new
|
||||
# request.env["action_controller.rescue.request"] = request
|
||||
#
|
||||
# resp = Fun::GamesController.action(:render_hello_world).call(request.env)
|
||||
# assert_equal 'hello: Iz guuut!', resp.last.body
|
||||
end
|
||||
|
||||
def test_helper_for_acronym_controller
|
||||
request = ActionController::TestRequest.new
|
||||
response = ActionController::TestResponse.new
|
||||
request.action = 'test'
|
||||
|
||||
assert_equal 'test: baz', Fun::PdfController.process(request, response).body
|
||||
assert_equal "test: baz", call_controller(Fun::PdfController, "test").last.body
|
||||
#
|
||||
# request = ActionController::TestRequest.new
|
||||
# response = ActionController::TestResponse.new
|
||||
# request.action = 'test'
|
||||
#
|
||||
# assert_equal 'test: baz', Fun::PdfController.process(request, response).body
|
||||
end
|
||||
|
||||
def test_all_helpers
|
||||
|
@ -204,6 +214,12 @@ class IsolatedHelpersTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def call_controller(klass, action)
|
||||
request = ActionController::TestRequest.new
|
||||
request.env["action_controller.rescue.request"] = request
|
||||
klass.action(action).call(request.env)
|
||||
end
|
||||
|
||||
def setup
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
|
@ -211,14 +227,14 @@ class IsolatedHelpersTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_helper_in_a
|
||||
assert_raise(ActionView::TemplateError) { A.process(@request, @response) }
|
||||
assert_raise(ActionView::TemplateError) { call_controller(A, "index") }
|
||||
end
|
||||
|
||||
def test_helper_in_b
|
||||
assert_equal 'B', B.process(@request, @response).body
|
||||
assert_equal 'B', call_controller(B, "index").last.body
|
||||
end
|
||||
|
||||
def test_helper_in_c
|
||||
assert_equal 'C', C.process(@request, @response).body
|
||||
assert_equal 'C', call_controller(C, "index").last.body
|
||||
end
|
||||
end
|
||||
|
|
|
@ -66,7 +66,7 @@ class Rack::TestCase < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def assert_body(body)
|
||||
assert_equal body, last_response.body
|
||||
assert_equal body, Array.wrap(last_response.body).join
|
||||
end
|
||||
|
||||
def self.assert_body(body)
|
||||
|
|
Loading…
Reference in a new issue