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

Allow access to current object_counter variable from layout when rendering with partial + collection

This commit is contained in:
Carlos Antonio da Silva 2012-04-26 20:47:22 -03:00
parent d0c9c93c4a
commit 0568fb5b9e
3 changed files with 14 additions and 5 deletions

View file

@ -283,7 +283,7 @@ module ActionView
end
if layout = @options[:layout]
layout = find_template(layout, @locals.keys + [@variable])
layout = find_template(layout, @locals.keys + [@variable, @variable_counter])
end
result = @template ? collection_with_template : collection_without_template
@ -292,6 +292,7 @@ module ActionView
locals = @locals
result.map! do |content|
locals[@variable] = @collection[result.index(content)]
locals[@variable_counter] = result.index(content)
layout.render(@view, @locals) { content }
end
end

View file

@ -0,0 +1 @@
<b data-counter="<%= customer_counter %>"><%= yield %></b>

View file

@ -238,12 +238,19 @@ 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") ])
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") ])
def test_render_partial_with_layout_using_collection_and_template_makes_current_item_available_in_layout
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_layout_using_object_and_template_makes_object_available_in_template
assert_equal '<b class="amazon">Hello: Amazon</b>', @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :object => Customer.new("Amazon"))
def test_render_partial_with_layout_using_collection_and_template_makes_current_item_counter_available_in_layout
assert_equal '<b data-counter="0">Hello: Amazon</b><b data-counter="1">Hello: Yahoo</b>',
@view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object_counter', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ])
end
def test_render_partial_with_layout_using_object_and_template_makes_object_available_in_layout
assert_equal '<b class="amazon">Hello: Amazon</b>',
@view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :object => Customer.new("Amazon"))
end
def test_render_partial_with_empty_array_should_return_nil