Skip to content

Commit

Permalink
Cleanup dispatch path
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Dec 18, 2008
1 parent 3b35366 commit a9fde9a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
27 changes: 9 additions & 18 deletions actionpack/lib/action_controller/dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,23 @@ def to_prepare(identifier = nil, &block)
include ActiveSupport::Callbacks
define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch

# DEPRECATE: Remove arguments
# DEPRECATE: Remove arguments, since they are only used by CGI
def initialize(output = $stdout, request = nil, response = nil)
@output, @request, @response = output, request, response
@output = output
@app = @@middleware.build(lambda { |env| self.dup._call(env) })
end

def dispatch
begin
run_callbacks :before_dispatch
handle_request
controller = Routing::Routes.recognize(@request)
controller.process(@request, @response).to_a
rescue Exception => exception
failsafe_rescue exception
if controller ||= (::ApplicationController rescue Base)
controller.process_with_exception(@request, @response, exception).to_a
else
raise exception
end
ensure
run_callbacks :after_dispatch, :enumerator => :reverse_each
end
Expand Down Expand Up @@ -123,19 +128,5 @@ def checkin_connections
return if @request.key?("rack.test")
ActiveRecord::Base.clear_active_connections!
end

protected
def handle_request
@controller = Routing::Routes.recognize(@request)
@controller.process(@request, @response).out
end

def failsafe_rescue(exception)
if @controller ||= (::ApplicationController rescue Base)
@controller.process_with_exception(@request, @response, exception).out
else
raise exception
end
end
end
end
3 changes: 1 addition & 2 deletions actionpack/lib/action_controller/rack_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def status
@status || super
end

def out(&block)
def to_a(&block)
@block = block
@status = headers.delete("Status")
if [204, 304].include?(status.to_i)
Expand All @@ -93,7 +93,6 @@ def out(&block)
[status, headers.to_hash, self]
end
end
alias to_a out

def each(&callback)
if @body.respond_to?(:call)
Expand Down
4 changes: 3 additions & 1 deletion actionpack/test/controller/dispatcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def test_to_prepare_with_identifier_replaces

private
def dispatch(cache_classes = true)
Dispatcher.any_instance.stubs(:handle_request).returns([200, {}, 'response'])
controller = mock()
controller.stubs(:process).returns([200, {}, 'response'])
ActionController::Routing::Routes.stubs(:recognize).returns(controller)
Dispatcher.define_dispatcher_callbacks(cache_classes)
@dispatcher.call({})
end
Expand Down
6 changes: 3 additions & 3 deletions actionpack/test/controller/rack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_simple_output
@response.body = "Hello, World!"
@response.prepare!

status, headers, body = @response.out
status, headers, body = @response.to_a
assert_equal "200 OK", status
assert_equal({
"Content-Type" => "text/html; charset=utf-8",
Expand All @@ -257,7 +257,7 @@ def test_streaming_block
end
@response.prepare!

status, headers, body = @response.out
status, headers, body = @response.to_a
assert_equal "200 OK", status
assert_equal({"Content-Type" => "text/html; charset=utf-8", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers)

Expand Down Expand Up @@ -293,6 +293,6 @@ def test_status
private
def response_headers
@response.prepare!
@response.out[1]
@response.to_a[1]
end
end

0 comments on commit a9fde9a

Please sign in to comment.