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

Ticket #3287: capture_helper_works_for_non_erb.patch

File capture_helper_works_for_non_erb.patch, 2.6 kB (added by Brian Takita, 3 years ago)

Initial Patch

  • actionpack/test/controller/capture_test.rb

    old new  
    77  def content_for 
    88    render :layout => "talk_from_action" 
    99  end 
     10   
     11  def content_for_non_erb_block 
     12    render :layout => "talk_from_action" 
     13  end 
    1014 
    1115  def rescue_action(e) raise end 
    1216end 
     
    3640    get :content_for 
    3741    assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body 
    3842  end 
     43   
     44  def test_content_for_non_erb_block 
     45    get :content_for_non_erb_block 
     46    assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body 
     47  end 
    3948 
    4049  def test_update_element_with_capture 
    4150    get :update_element_with_capture 
  • actionpack/lib/action_view/helpers/capture_helper.rb

    old new  
    4343      # instance variable. You can use this instance variable anywhere 
    4444      # in your templates and even in your layout.  
    4545      #  
    46       # Example
     46      # Example of capture being used in a .rhtml page
    4747      #  
    4848      #   <% @greeting = capture do %> 
    4949      #     Welcome To my shiny new web page! 
    50       #   <% end %>       
     50      #   <% end %> 
     51      # 
     52      # Example of capture being used in a .rxml page: 
     53      #  
     54      #   @greeting = capture do 
     55      #     'Welcome To my shiny new web page!' 
     56      #   end 
    5157      def capture(*args, &block) 
    5258        # execute the block 
    53         buffer = eval("_erbout", block.binding) 
    54         pos = buffer.length 
    55         block.call(*args) 
     59        begin 
     60          buffer = eval("_erbout", block.binding) 
     61        rescue 
     62          buffer = nil 
     63        end 
    5664         
    57         # extract the block  
    58         data = buffer[pos..-1] 
    59          
    60         # replace it in the original with empty string 
    61         buffer[pos..-1] = '' 
    62          
    63         data 
     65        if buffer.nil? 
     66          block.call(*args) 
     67        else 
     68          pos = buffer.length 
     69          block.call(*args) 
     70           
     71          # extract the block  
     72          data = buffer[pos..-1] 
     73           
     74          # replace it in the original with empty string 
     75          buffer[pos..-1] = '' 
     76           
     77          data 
     78        end 
    6479      end 
    6580       
    6681      # Content_for will store the given block