Skip to content
Browse files

Allow access to current object_counter variable from layout when rend…

…ering with partial + collection
  • Loading branch information...
1 parent d0c9c93 commit 0568fb5b9efa6923e9407552aae46b0a1210539d @carlosantoniodasilva carlosantoniodasilva committed Apr 26, 2012
View
3 actionpack/lib/action_view/renderer/partial_renderer.rb
@@ -283,7 +283,7 @@ def render_collection
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 @@ def render_collection
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
1 actionpack/test/fixtures/test/_b_layout_for_partial_with_object_counter.html.erb
@@ -0,0 +1 @@
+<b data-counter="<%= customer_counter %>"><%= yield %></b>
View
15 actionpack/test/template/render_test.rb
@@ -238,12 +238,19 @@ def test_render_partial_with_layout_using_collection_and_template
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

0 comments on commit 0568fb5

Please sign in to comment.
Something went wrong with that request. Please try again.