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

Ticket #9209: render_partial_with_layout_patch_2.diff

File render_partial_with_layout_patch_2.diff, 4.9 kB (added by antramm, 10 months ago)

New version of patch - tested against r8429

  • actionpack/test/controller/new_render_test.rb

    old new  
    361361    render :action => "calling_partial_with_layout" 
    362362  end 
    363363 
     364  def render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout 
     365    render :action => "calling_partial_with_layout" 
     366  end 
     367 
    364368  def render_using_layout_around_block 
    365369    render :action => "using_layout_around_block" 
    366370  end 
    367371 
     372  def render_using_layout_around_block_in_main_layout_and_within_content_for_layout 
     373    render :action => "using_layout_around_block" 
     374  end 
     375   
    368376  def rescue_action(e) raise end 
    369377     
    370378  private 
     
    387395          "layouts/builder" 
    388396        when "action_talk_to_layout", "layout_overriding_layout" 
    389397          "layouts/talk_from_action" 
     398        when "render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout" 
     399          "layouts/partial_with_layout" 
     400        when "render_using_layout_around_block_in_main_layout_and_within_content_for_layout" 
     401          "layouts/block_with_layout" 
    390402      end 
    391403    end 
    392404end 
     
    824836    get :render_call_to_partial_with_layout 
    825837    assert_equal "Before (David)\nInside from partial (David)\nAfter", @response.body 
    826838  end 
    827    
     839 
     840  def test_render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout 
     841    get :render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout 
     842    assert_equal "Before (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter", @response.body 
     843  end 
     844 
    828845  def test_using_layout_around_block 
    829     get :using_layout_around_block 
     846    get :render_using_layout_around_block 
    830847    assert_equal "Before (David)\nInside from block\nAfter", @response.body 
    831848  end 
     849 
     850  def test_using_layout_around_block_in_main_layout_and_within_content_for_layout 
     851    get :render_using_layout_around_block_in_main_layout_and_within_content_for_layout 
     852    assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body 
     853  end 
     854 
    832855end 
  • actionpack/test/fixtures/layouts/block_with_layout.erb

    old new  
     1<% render(:layout => "layout_for_partial", :locals => { :name => "Anthony" }) do %>Inside from first block in layout<% end %> 
     2<%= yield %> 
     3<% render(:layout => "layout_for_partial", :locals => { :name => "Ramm" }) do %>Inside from second block in layout<% end %> 
  • actionpack/test/fixtures/layouts/partial_with_layout.erb

    old new  
     1<%= render( :layout => "layout_for_partial", :partial => "partial_for_use_in_layout", :locals => {:name => 'Anthony' } ) %> 
     2<%= yield %> 
     3<%= render( :layout => "layout_for_partial", :partial => "partial_for_use_in_layout", :locals => {:name => 'Ramm' } ) %> 
  • actionpack/lib/action_view/base.rb

    old new  
    338338          path, partial_name = partial_pieces(options.delete(:layout)) 
    339339 
    340340          if block_given? 
    341             @content_for_layout = capture(&block) 
    342             concat(render(options.merge(:partial => "#{path}/#{partial_name}")), block.binding) 
     341            wrap_content_for_layout capture(&block) do  
     342              concat(render(options.merge(:partial => "#{path}/#{partial_name}")), block.binding) 
     343            end 
    343344          else 
    344             @content_for_layout = render(options) 
    345             render(options.merge(:partial => "#{path}/#{partial_name}")) 
     345            wrap_content_for_layout render(options) do 
     346              render(options.merge(:partial => "#{path}/#{partial_name}")) 
     347            end 
    346348          end 
    347349        elsif options[:file] 
    348350          render_file(options[:file], options[:use_full_path], options[:locals]) 
     
    441443    end 
    442444 
    443445    private 
     446      def wrap_content_for_layout(content) 
     447        original_content_for_layout = @content_for_layout 
     448        @content_for_layout = content 
     449        returning(yield) { @content_for_layout = original_content_for_layout } 
     450      end 
     451   
    444452      def find_full_template_path(template_path, extension) 
    445453        file_name = "#{template_path}.#{extension}" 
    446454        base_path = find_base_path_for(file_name)