1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow layout rendering to access current object being rendered when using partial + collection

This commit is contained in:
Carlos Antonio da Silva 2012-04-26 20:24:53 -03:00
parent 82029c7511
commit e72374d0ff
3 changed files with 18 additions and 8 deletions

View file

@ -283,12 +283,18 @@ module ActionView
end end
if layout = @options[:layout] if layout = @options[:layout]
layout = find_template(layout) layout = find_template(layout, @locals.keys + [@variable])
end end
result = @template ? collection_with_template : collection_without_template result = @template ? collection_with_template : collection_without_template
result.map!{|content| layout.render(@view, @locals) { content } } if layout if layout
locals = @locals
result.map! do |content|
locals[@variable] = @collection[result.index(content)]
layout.render(@view, @locals) { content }
end
end
result.join(spacer).html_safe result.join(spacer).html_safe
end end
@ -395,7 +401,6 @@ module ActionView
segments segments
end end
def collection_without_template def collection_without_template
segments, locals, collection_data = [], @locals, @collection_data segments, locals, collection_data = [], @locals, @collection_data
index, template, cache = -1, nil, {} index, template, cache = -1, nil, {}

View file

@ -0,0 +1 @@
<b class="<%= customer.name.downcase %>"><%= yield %></b>

View file

@ -238,6 +238,10 @@ module RenderTestCases
assert_equal "<b>Hello: Amazon</b><b>Hello: Yahoo</b>", @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ]) assert_equal "<b>Hello: Amazon</b><b>Hello: Yahoo</b>", @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ])
end end
def test_render_partial_with_layout_using_collection_and_template_makes_current_item_available_in_template
assert_equal '<b class="amazon">Hello: Amazon</b><b class="yahoo">Hello: Yahoo</b>', @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ])
end
def test_render_partial_with_empty_array_should_return_nil def test_render_partial_with_empty_array_should_return_nil
assert_nil @view.render(:partial => []) assert_nil @view.render(:partial => [])
end end