Refactor the default rendering out to a method called default_render to provide a hook for plugin authors. Closes #9953 [cjheath]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8011 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Michael Koziarski 2007-10-25 06:38:01 +00:00
parent 7c3581cba2
commit 12d8d48b71
2 changed files with 31 additions and 5 deletions

View File

@ -70,7 +70,7 @@ module ActionController #:nodoc:
end
class DoubleRenderError < ActionControllerError #:nodoc:
DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and only once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\". Finally, note that to cause a before filter to halt execution of the rest of the filter chain, the filter must return false, explicitly, so \"render(...) and return false\"."
DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\". Finally, note that to cause a before filter to halt execution of the rest of the filter chain, the filter must return false, explicitly, so \"render(...) and return false\"."
def initialize(message = nil)
super(message || DEFAULT_MESSAGE)
@ -226,7 +226,7 @@ module ActionController #:nodoc:
#
# == Calling multiple redirects or renders
#
# An action should conclude with a single render or redirect. Attempting to try to do either again will result in a DoubleRenderError:
# An action may contain only a single render or a single redirect. Attempting to try to do either again will result in a DoubleRenderError:
#
# def do_something
# redirect_to :action => "elsewhere"
@ -1125,15 +1125,19 @@ module ActionController #:nodoc:
end
end
def default_render #:nodoc:
render
end
def perform_action
if self.class.action_methods.include?(action_name)
send(action_name)
render unless performed?
default_render unless performed?
elsif respond_to? :method_missing
method_missing action_name
render unless performed?
default_render unless performed?
elsif template_exists? && template_public?
render
default_render
else
raise UnknownAction, "No action responded to #{action_name}", caller
end

View File

@ -152,6 +152,23 @@ class TestController < ActionController::Base
end
end
def default_render
if @alternate_default_render
@alternate_default_render.call
else
render
end
end
def render_alternate_default
# For this test, the method "default_render" is overridden:
@alternate_default_render = lambda {
render :update do |page|
page.replace :foo, :partial => 'partial'
end
}
end
def rescue_action(e) raise end
private
@ -413,6 +430,11 @@ class RenderTest < Test::Unit::TestCase
assert_equal 'partial js', @response.body
end
def test_should_render_with_alternate_default_render
xhr :get, :render_alternate_default
assert_equal %(Element.replace("foo", "partial html");), @response.body
end
protected
def etag_for(text)