From 5a45446cff0daf4ca747257a8779dcd5d9cae1d7 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 15 May 2009 17:49:11 -0700 Subject: Ported Rescuable to new base --- .../lib/action_controller/base/chained/filters.rb | 2 +- .../lib/action_controller/dispatch/middlewares.rb | 1 + actionpack/lib/action_controller/new_base.rb | 1 + actionpack/lib/action_controller/new_base/base.rb | 50 +++++++++++--------- .../action_controller/new_base/compatibility.rb | 13 ++++++ actionpack/lib/action_controller/new_base/http.rb | 4 +- .../lib/action_controller/new_base/rescuable.rb | 53 ++++++++++++++++++++++ .../lib/action_controller/new_base/testing.rb | 2 +- actionpack/test/abstract_unit2.rb | 12 ++++- 9 files changed, 111 insertions(+), 27 deletions(-) create mode 100644 actionpack/lib/action_controller/new_base/rescuable.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base/chained/filters.rb b/actionpack/lib/action_controller/base/chained/filters.rb index 9022b8b279..98fe306fd5 100644 --- a/actionpack/lib/action_controller/base/chained/filters.rb +++ b/actionpack/lib/action_controller/base/chained/filters.rb @@ -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 diff --git a/actionpack/lib/action_controller/dispatch/middlewares.rb b/actionpack/lib/action_controller/dispatch/middlewares.rb index f99637b109..0e4ab6fa39 100644 --- a/actionpack/lib/action_controller/dispatch/middlewares.rb +++ b/actionpack/lib/action_controller/dispatch/middlewares.rb @@ -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) } diff --git a/actionpack/lib/action_controller/new_base.rb b/actionpack/lib/action_controller/new_base.rb index 1f215bb6f1..bc47713529 100644 --- a/actionpack/lib/action_controller/new_base.rb +++ b/actionpack/lib/action_controller/new_base.rb @@ -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" diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index 3ee17769a8..c25ffd5c84 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -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 \ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/compatibility.rb b/actionpack/lib/action_controller/new_base/compatibility.rb index 9884ed12e5..0a283887b6 100644 --- a/actionpack/lib/action_controller/new_base/compatibility.rb +++ b/actionpack/lib/action_controller/new_base/compatibility.rb @@ -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(*) diff --git a/actionpack/lib/action_controller/new_base/http.rb b/actionpack/lib/action_controller/new_base/http.rb index e9a1d5b12f..8891a2a8c3 100644 --- a/actionpack/lib/action_controller/new_base/http.rb +++ b/actionpack/lib/action_controller/new_base/http.rb @@ -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 diff --git a/actionpack/lib/action_controller/new_base/rescuable.rb b/actionpack/lib/action_controller/new_base/rescuable.rb new file mode 100644 index 0000000000..29ffe19d5f --- /dev/null +++ b/actionpack/lib/action_controller/new_base/rescuable.rb @@ -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 + # local_request? method in your own controller. Custom rescue + # behavior is achieved by overriding the rescue_action_in_public + # and rescue_action_locally 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 diff --git a/actionpack/lib/action_controller/new_base/testing.rb b/actionpack/lib/action_controller/new_base/testing.rb index 106990b9ba..b39d8d539d 100644 --- a/actionpack/lib/action_controller/new_base/testing.rb +++ b/actionpack/lib/action_controller/new_base/testing.rb @@ -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 diff --git a/actionpack/test/abstract_unit2.rb b/actionpack/test/abstract_unit2.rb index 932f594ad2..519e6bea36 100644 --- a/actionpack/test/abstract_unit2.rb +++ b/actionpack/test/abstract_unit2.rb @@ -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 -- cgit v1.2.3