Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:rails/rails
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed May 4, 2009
2 parents d3ee875 + bcc4537 commit 8b6d2ef
Show file tree
Hide file tree
Showing 30 changed files with 422 additions and 592 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller.rb
Expand Up @@ -60,7 +60,7 @@ def self.load_all!
autoload :Redirector, 'action_controller/base/redirect' autoload :Redirector, 'action_controller/base/redirect'
autoload :Renderer, 'action_controller/base/render' autoload :Renderer, 'action_controller/base/render'
autoload :RequestForgeryProtection, 'action_controller/base/request_forgery_protection' autoload :RequestForgeryProtection, 'action_controller/base/request_forgery_protection'
autoload :Rescue, 'action_controller/dispatch/rescue' autoload :Rescue, 'action_controller/base/rescue'
autoload :Resources, 'action_controller/routing/resources' autoload :Resources, 'action_controller/routing/resources'
autoload :Responder, 'action_controller/base/responder' autoload :Responder, 'action_controller/base/responder'
autoload :Routing, 'action_controller/routing' autoload :Routing, 'action_controller/routing'
Expand Down
9 changes: 2 additions & 7 deletions actionpack/lib/action_controller/base/base.rb
Expand Up @@ -30,10 +30,6 @@ def initialize(*allowed_methods)
def allowed_methods_header def allowed_methods_header
allowed_methods.map { |method_symbol| method_symbol.to_s.upcase } * ', ' allowed_methods.map { |method_symbol| method_symbol.to_s.upcase } * ', '
end end

def handle_response!(response)
response.headers['Allow'] ||= allowed_methods_header
end
end end


class NotImplemented < MethodNotAllowed #:nodoc: class NotImplemented < MethodNotAllowed #:nodoc:
Expand Down Expand Up @@ -510,9 +506,8 @@ def exempt_from_layout(*types)


public public
def call(env) def call(env)
# HACK: For global rescue to have access to the original request and response request = ActionDispatch::Request.new(env)
request = env["action_dispatch.rescue.request"] ||= ActionDispatch::Request.new(env) response = ActionDispatch::Response.new
response = env["action_dispatch.rescue.response"] ||= ActionDispatch::Response.new
process(request, response).to_a process(request, response).to_a
end end


Expand Down
50 changes: 50 additions & 0 deletions actionpack/lib/action_controller/base/rescue.rb
@@ -0,0 +1,50 @@
module ActionController #:nodoc:
# Actions that fail to perform as expected throw exceptions. These
# exceptions can either be rescued for the public view (with a nice
# user-friendly explanation) or for the developers view (with tons of
# debugging information). The developers view is already implemented by
# the Action Controller, but the public view should be tailored to your
# specific application.
#
# The default behavior for public exceptions is to render a static html
# file with the name of the error code thrown. If no such file exists, an
# empty response is sent with the correct status code.
#
# You can override what constitutes a local request by overriding the
# <tt>local_request?</tt> method in your own controller. Custom rescue
# behavior is achieved by overriding the <tt>rescue_action_in_public</tt>
# and <tt>rescue_action_locally</tt> methods.
module Rescue
def self.included(base) #:nodoc:
base.send :include, ActiveSupport::Rescuable
base.extend(ClassMethods)

base.class_eval do
alias_method_chain :perform_action, :rescue
end
end

module ClassMethods
def rescue_action(env)
exception = env.delete('action_dispatch.rescue.exception')
request = ActionDispatch::Request.new(env)
response = ActionDispatch::Response.new
new.process(request, response, :rescue_action, exception).to_a
end
end

protected
# Exception handler called when the performance of an action raises
# an exception.
def rescue_action(exception)
rescue_with_handler(exception) || raise(exception)
end

private
def perform_action_with_rescue
perform_action_without_rescue
rescue Exception => exception
rescue_action(exception)
end
end
end
53 changes: 22 additions & 31 deletions actionpack/lib/action_controller/dispatch/dispatcher.rb
Expand Up @@ -5,9 +5,9 @@ class Dispatcher
class << self class << self
def define_dispatcher_callbacks(cache_classes) def define_dispatcher_callbacks(cache_classes)
unless cache_classes unless cache_classes
unless self.middleware.include?(ActionDispatch::Reloader) # Development mode callbacks
self.middleware.insert_after(ActionDispatch::Failsafe, ActionDispatch::Reloader) before_dispatch :reload_application
end after_dispatch :cleanup_application


ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
end end
Expand Down Expand Up @@ -40,22 +40,11 @@ def to_prepare(identifier = nil, &block)
def run_prepare_callbacks def run_prepare_callbacks
new.send :run_callbacks, :prepare_dispatch new.send :run_callbacks, :prepare_dispatch
end end

def reload_application
# Run prepare callbacks before every request in development mode
run_prepare_callbacks

Routing::Routes.reload
end

def cleanup_application
# Cleanup the application before processing the current request.
ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
ActiveSupport::Dependencies.clear
ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
end
end end


cattr_accessor :router
self.router = Routing::Routes

cattr_accessor :middleware cattr_accessor :middleware
self.middleware = ActionDispatch::MiddlewareStack.new do |middleware| self.middleware = ActionDispatch::MiddlewareStack.new do |middleware|
middlewares = File.join(File.dirname(__FILE__), "middlewares.rb") middlewares = File.join(File.dirname(__FILE__), "middlewares.rb")
Expand All @@ -66,27 +55,29 @@ def cleanup_application
define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch


def initialize def initialize
@app = @@middleware.build(lambda { |env| self._call(env) }) @app = @@middleware.build(@@router)
freeze freeze
end end


def call(env) def call(env)
run_callbacks :before_dispatch
@app.call(env) @app.call(env)
ensure
run_callbacks :after_dispatch, :enumerator => :reverse_each
end end


def _call(env) def reload_application
begin # Run prepare callbacks before every request in development mode
run_callbacks :before_dispatch run_callbacks :prepare_dispatch
Routing::Routes.call(env)
rescue Exception => exception @@router.reload
if controller ||= (::ApplicationController rescue Base) end
controller.call_with_exception(env, exception)
else def cleanup_application
raise exception # Cleanup the application before processing the current request.
end ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
ensure ActiveSupport::Dependencies.clear
run_callbacks :after_dispatch, :enumerator => :reverse_each ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
end
end end


def flush_logger def flush_logger
Expand Down
5 changes: 5 additions & 0 deletions actionpack/lib/action_controller/dispatch/middlewares.rb
Expand Up @@ -3,6 +3,11 @@
} }


use "ActionDispatch::Failsafe" use "ActionDispatch::Failsafe"
use "ActionDispatch::ShowExceptions", lambda { ActionController::Base.consider_all_requests_local }
use "ActionDispatch::Rescue", lambda {
controller = (::ApplicationController rescue ActionController::Base)
controller.method(:rescue_action)
}


use lambda { ActionController::Base.session_store }, use lambda { ActionController::Base.session_store },
lambda { ActionController::Base.session_options } lambda { ActionController::Base.session_options }
Expand Down
185 changes: 0 additions & 185 deletions actionpack/lib/action_controller/dispatch/rescue.rb

This file was deleted.

This file was deleted.

3 changes: 2 additions & 1 deletion actionpack/lib/action_dispatch.rb
Expand Up @@ -47,7 +47,8 @@ module ActionDispatch


autoload :Failsafe, 'action_dispatch/middleware/failsafe' autoload :Failsafe, 'action_dispatch/middleware/failsafe'
autoload :ParamsParser, 'action_dispatch/middleware/params_parser' autoload :ParamsParser, 'action_dispatch/middleware/params_parser'
autoload :Reloader, 'action_dispatch/middleware/reloader' autoload :Rescue, 'action_dispatch/middleware/rescue'
autoload :ShowExceptions, 'action_dispatch/middleware/show_exceptions'
autoload :MiddlewareStack, 'action_dispatch/middleware/stack' autoload :MiddlewareStack, 'action_dispatch/middleware/stack'


autoload :Assertions, 'action_dispatch/testing/assertions' autoload :Assertions, 'action_dispatch/testing/assertions'
Expand Down

0 comments on commit 8b6d2ef

Please sign in to comment.