Skip to content

Commit

Permalink
Make router and controller classes better rack citizens
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Dec 28, 2008
1 parent 45dee38 commit 5d89605
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 87 deletions.
7 changes: 7 additions & 0 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -382,6 +382,13 @@ class Base
attr_accessor :action_name

class << self
def call(env)
# HACK: For global rescue to have access to the original request and response
request = env["actioncontroller.rescue.request"] ||= Request.new(env)
response = env["actioncontroller.rescue.response"] ||= Response.new
process(request, response)
end

# Factory for the standard create, process loop where the controller is discarded after processing.
def process(request, response) #:nodoc:
new.process(request, response)
Expand Down
11 changes: 4 additions & 7 deletions actionpack/lib/action_controller/dispatcher.rb
Expand Up @@ -60,11 +60,10 @@ def initialize(output = $stdout, request = nil, response = nil)
def dispatch
begin
run_callbacks :before_dispatch
controller = Routing::Routes.recognize(@request)
controller.process(@request, @response).to_a
Routing::Routes.call(@env)
rescue Exception => exception
if controller ||= (::ApplicationController rescue Base)
controller.process_with_exception(@request, @response, exception).to_a
controller.call_with_exception(@env, exception).to_a
else
raise exception
end
Expand All @@ -83,8 +82,7 @@ def call(env)
end

def _call(env)
@request = Request.new(env)
@response = Response.new
@env = env
dispatch
end

Expand All @@ -110,8 +108,7 @@ def flush_logger

def checkin_connections
# Don't return connection (and peform implicit rollback) if this request is a part of integration test
# TODO: This callback should have direct access to env
return if @request.key?("rack.test")
return if @env.key?("rack.test")
ActiveRecord::Base.clear_active_connections!
end
end
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/request.rb
Expand Up @@ -398,7 +398,7 @@ def parameters
end

def path_parameters=(parameters) #:nodoc:
@path_parameters = parameters
@env["routing_args"] = parameters
@symbolized_path_parameters = @parameters = nil
end

Expand All @@ -414,7 +414,7 @@ def symbolized_path_parameters
#
# See <tt>symbolized_path_parameters</tt> for symbolized keys.
def path_parameters
@path_parameters ||= {}
@env["routing_args"] ||= {}
end

def body
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/rescue.rb
Expand Up @@ -59,7 +59,9 @@ def self.included(base) #:nodoc:
end

module ClassMethods
def process_with_exception(request, response, exception) #:nodoc:
def call_with_exception(env, exception) #:nodoc:
request = env["actioncontroller.rescue.request"]
response = env["actioncontroller.rescue.response"]
new.process(request, response, :rescue_action, exception)
end
end
Expand Down
6 changes: 6 additions & 0 deletions actionpack/lib/action_controller/routing/route_set.rb
Expand Up @@ -427,6 +427,12 @@ def raise_named_route_error(options, named_route, named_route_name)
end
end

def call(env)
request = Request.new(env)
app = Routing::Routes.recognize(request)
app.call(env).to_a
end

def recognize(request)
params = recognize_path(request.path, extract_request_environment(request))
request.path_parameters = params.with_indifferent_access
Expand Down
4 changes: 1 addition & 3 deletions actionpack/test/controller/dispatcher_test.rb
Expand Up @@ -96,9 +96,7 @@ def test_to_prepare_with_identifier_replaces

private
def dispatch(cache_classes = true)
controller = mock()
controller.stubs(:process).returns([200, {}, 'response'])
ActionController::Routing::Routes.stubs(:recognize).returns(controller)
ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response'])
Dispatcher.define_dispatcher_callbacks(cache_classes)
@dispatcher.call({})
end
Expand Down
6 changes: 5 additions & 1 deletion actionpack/test/controller/rescue_test.rb
Expand Up @@ -367,7 +367,11 @@ def test_block_rescue_handler_with_argument_as_string
end

def test_rescue_dispatcher_exceptions
RescueController.process_with_exception(@request, @response, ActionController::RoutingError.new("Route not found"))
env = @request.env
env["actioncontroller.rescue.request"] = @request
env["actioncontroller.rescue.response"] = @response

RescueController.call_with_exception(env, ActionController::RoutingError.new("Route not found"))
assert_equal "no way", @response.body
end

Expand Down

0 comments on commit 5d89605

Please sign in to comment.