mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Ported Rescuable to new base
This commit is contained in:
@@ -160,7 +160,7 @@ module ActionController #:nodoc:
|
||||
def convert_only_and_except_options_to_sets_of_strings(opts)
|
||||
[:only, :except].each do |key|
|
||||
if values = opts[key]
|
||||
opts[key] = Array(values).map(&:to_s).to_set
|
||||
opts[key] = Array(values).map {|val| val.to_s }.to_set
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,6 +6,7 @@ use "ActionDispatch::Failsafe"
|
||||
use "ActionDispatch::ShowExceptions", lambda { ActionController::Base.consider_all_requests_local }
|
||||
use "ActionDispatch::Rescue", lambda {
|
||||
controller = (::ApplicationController rescue ActionController::Base)
|
||||
# TODO: Replace with controller.action(:_rescue_action)
|
||||
controller.method(:rescue_action)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ module ActionController
|
||||
autoload :Rails2Compatibility, "action_controller/new_base/compatibility"
|
||||
autoload :Redirector, "action_controller/new_base/redirector"
|
||||
autoload :Renderer, "action_controller/new_base/renderer"
|
||||
autoload :Rescue, "action_controller/new_base/rescuable"
|
||||
autoload :Testing, "action_controller/new_base/testing"
|
||||
autoload :UrlFor, "action_controller/new_base/url_for"
|
||||
|
||||
|
||||
@@ -12,14 +12,40 @@ module ActionController
|
||||
include ActionController::Renderer
|
||||
include ActionController::Layouts
|
||||
include ActionController::ConditionalGet
|
||||
|
||||
|
||||
# Legacy modules
|
||||
include SessionManagement
|
||||
include ActionDispatch::StatusCodes
|
||||
|
||||
|
||||
# Rails 2.x compatibility
|
||||
include ActionController::Rails2Compatibility
|
||||
|
||||
|
||||
# TODO: Extract into its own module
|
||||
# This should be moved together with other normalizing behavior
|
||||
module ImplicitRender
|
||||
def process_action(method_name)
|
||||
ret = super
|
||||
render if response_body.nil?
|
||||
ret
|
||||
end
|
||||
|
||||
def _implicit_render
|
||||
render
|
||||
end
|
||||
|
||||
def method_for_action(action_name)
|
||||
super || begin
|
||||
if view_paths.find_by_parts?(action_name.to_s, {:formats => formats, :locales => [I18n.locale]}, controller_path)
|
||||
"_implicit_render"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
include ImplicitRender
|
||||
|
||||
include ActionController::Rescue
|
||||
|
||||
def self.inherited(klass)
|
||||
::ActionController::Base.subclasses << klass.to_s
|
||||
super
|
||||
@@ -113,23 +139,5 @@ module ActionController
|
||||
|
||||
super(url, status)
|
||||
end
|
||||
|
||||
def process_action(method_name)
|
||||
ret = super
|
||||
render if response_body.nil?
|
||||
ret
|
||||
end
|
||||
|
||||
def _implicit_render
|
||||
render
|
||||
end
|
||||
|
||||
def method_for_action(action_name)
|
||||
super || begin
|
||||
if view_paths.find_by_parts?(action_name.to_s, {:formats => formats, :locales => [I18n.locale]}, controller_path)
|
||||
"_implicit_render"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -45,6 +45,14 @@ module ActionController
|
||||
|
||||
cattr_accessor :use_accept_header
|
||||
self.use_accept_header = true
|
||||
|
||||
cattr_accessor :page_cache_directory
|
||||
self.page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
|
||||
|
||||
cattr_reader :cache_store
|
||||
|
||||
cattr_accessor :consider_all_requests_local
|
||||
self.consider_all_requests_local = true
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
@@ -53,6 +61,11 @@ module ActionController
|
||||
def rescue_action(env)
|
||||
raise env["action_dispatch.rescue.exception"]
|
||||
end
|
||||
|
||||
# Defines the storage option for cached fragments
|
||||
def cache_store=(store_option)
|
||||
@@cache_store = ActiveSupport::Cache.lookup_store(store_option)
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(*)
|
||||
|
||||
@@ -48,8 +48,6 @@ module ActionController
|
||||
@_response = ActionDispatch::Response.new
|
||||
@_response.request = request
|
||||
process(name)
|
||||
@_response.body = response_body
|
||||
@_response.prepare!
|
||||
to_rack
|
||||
end
|
||||
|
||||
@@ -62,6 +60,8 @@ module ActionController
|
||||
|
||||
# :api: private
|
||||
def to_rack
|
||||
@_response.body = response_body
|
||||
@_response.prepare!
|
||||
@_response.to_a
|
||||
end
|
||||
end
|
||||
|
||||
53
actionpack/lib/action_controller/new_base/rescuable.rb
Normal file
53
actionpack/lib/action_controller/new_base/rescuable.rb
Normal file
@@ -0,0 +1,53 @@
|
||||
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
|
||||
extend ActiveSupport::DependencyModule
|
||||
|
||||
included do
|
||||
include ActiveSupport::Rescuable
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# This can be removed once we can move action(:_rescue_action) into middlewares.rb
|
||||
# Currently, it does controller.method(:rescue_action), which is hiding the implementation
|
||||
# difference between the old and new base.
|
||||
def rescue_action(env)
|
||||
action(:_rescue_action).call(env)
|
||||
end
|
||||
end
|
||||
|
||||
attr_internal :rescued_exception
|
||||
|
||||
private
|
||||
|
||||
def method_for_action(action_name)
|
||||
return action_name if self.rescued_exception = request.env.delete("action_dispatch.rescue.exception")
|
||||
super
|
||||
end
|
||||
|
||||
def _rescue_action
|
||||
rescue_with_handler(rescued_exception) || raise(rescued_exception)
|
||||
end
|
||||
|
||||
def process_action(*)
|
||||
super
|
||||
rescue Exception => exception
|
||||
self.rescued_exception = exception
|
||||
_rescue_action
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,7 +7,7 @@ module ActionController
|
||||
@_response = response
|
||||
@_response.request = request
|
||||
ret = process(request.parameters[:action])
|
||||
@_response.body = self.response_body
|
||||
@_response.body = self.response_body || " "
|
||||
@_response.prepare!
|
||||
set_test_assigns
|
||||
ret
|
||||
|
||||
@@ -11,11 +11,19 @@ require 'action_controller/new_base'
|
||||
require 'fixture_template'
|
||||
require 'action_controller/testing/process2'
|
||||
require 'action_view/test_case'
|
||||
require 'action_controller/testing/integration'
|
||||
require 'active_support/dependencies'
|
||||
|
||||
ActiveSupport::Dependencies.hook!
|
||||
|
||||
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
|
||||
|
||||
module ActionController
|
||||
|
||||
Base.session = {
|
||||
:key => '_testing_session',
|
||||
:secret => '8273f16463985e2b3747dc25e30f2528'
|
||||
}
|
||||
|
||||
class ActionControllerError < StandardError #:nodoc:
|
||||
end
|
||||
|
||||
@@ -126,6 +134,6 @@ module ActionController
|
||||
"Expected no partials to be rendered"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user