Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8011

Show
Ignore:
Timestamp:
10/25/07 06:38:01 (7 months ago)
Author:
nzkoz
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/base.rb

    r7999 r8011  
    7171 
    7272  class DoubleRenderError < ActionControllerError #:nodoc: 
    73     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\"." 
     73    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\"." 
    7474 
    7575    def initialize(message = nil) 
     
    227227  # == Calling multiple redirects or renders 
    228228  # 
    229   # An action should conclude with a single render or redirect. Attempting to try to do either again will result in a DoubleRenderError: 
     229  # An action may contain only a single render or a single redirect. Attempting to try to do either again will result in a DoubleRenderError: 
    230230  # 
    231231  #   def do_something 
     
    11261126      end 
    11271127 
     1128      def default_render #:nodoc: 
     1129        render 
     1130      end 
     1131 
    11281132      def perform_action 
    11291133        if self.class.action_methods.include?(action_name) 
    11301134          send(action_name) 
    1131           render unless performed? 
     1135          default_render unless performed? 
    11321136        elsif respond_to? :method_missing 
    11331137          method_missing action_name 
    1134           render unless performed? 
     1138          default_render unless performed? 
    11351139        elsif template_exists? && template_public? 
    1136           render 
     1140          default_render 
    11371141        else 
    11381142          raise UnknownAction, "No action responded to #{action_name}", caller 
  • trunk/actionpack/test/controller/render_test.rb

    r7719 r8011  
    153153  end 
    154154 
     155  def default_render 
     156    if @alternate_default_render 
     157      @alternate_default_render.call 
     158    else 
     159      render 
     160    end 
     161  end 
     162 
     163  def render_alternate_default 
     164    # For this test, the method "default_render" is overridden: 
     165    @alternate_default_render = lambda { 
     166        render :update do |page| 
     167          page.replace :foo, :partial => 'partial' 
     168        end 
     169      } 
     170  end 
     171 
    155172  def rescue_action(e) raise end 
    156173 
     
    414431  end 
    415432 
     433  def test_should_render_with_alternate_default_render 
     434    xhr :get, :render_alternate_default 
     435    assert_equal %(Element.replace("foo", "partial html");), @response.body 
     436  end 
     437 
    416438  protected 
    417439