From 97808e04762f484224b39d87232ef1d19f0665ac Mon Sep 17 00:00:00 2001 From: Tekin Suleyman Date: Thu, 14 Aug 2008 23:01:41 +0100 Subject: [PATCH] Add collection_size as a local variable in partials rendered from collections. Also adds first_in_collection and last_in_collection booleans and assigns a counter variable based on the :as parameter if specified. --- actionpack/lib/action_view/partials.rb | 3 +++ actionpack/lib/action_view/renderable_partial.rb | 5 ++++- .../test/_partial_with_collection_size.html.erb | 1 + actionpack/test/template/render_test.rb | 7 ++++++- 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 actionpack/test/fixtures/test/_partial_with_collection_size.html.erb diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index 894b885..06c7bfe 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -138,6 +138,9 @@ module ActionView path = find_partial_path(_partial_path) template = pick_template(path) local_assigns[template.counter_name] = index + local_assigns[:collection_size] = collection.size + local_assigns[:first_in_collection] = index == 0 + local_assigns[:last_in_collection] = index == collection.size-1 result = template.render_partial(self, object, local_assigns, as) index += 1 result diff --git a/actionpack/lib/action_view/renderable_partial.rb b/actionpack/lib/action_view/renderable_partial.rb index 342850f..63e6aae 100644 --- a/actionpack/lib/action_view/renderable_partial.rb +++ b/actionpack/lib/action_view/renderable_partial.rb @@ -28,7 +28,10 @@ module ActionView # Ensure correct object is reassigned to other accessors local_assigns[:object] = local_assigns[variable_name] = object - local_assigns[as] = object if as + if as + local_assigns[as] = object + local_assigns["#{as}_counter".to_sym] = local_assigns[counter_name] + end render_template(view, local_assigns) end diff --git a/actionpack/test/fixtures/test/_partial_with_collection_size.html.erb b/actionpack/test/fixtures/test/_partial_with_collection_size.html.erb new file mode 100644 index 0000000..8b44426 --- /dev/null +++ b/actionpack/test/fixtures/test/_partial_with_collection_size.html.erb @@ -0,0 +1 @@ +<%= collection_size -%>-<%= first_in_collection %>-<%= last_in_collection %>, \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 31cfdce..361df58 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -83,9 +83,14 @@ class ViewRenderTest < Test::Unit::TestCase end def test_render_partial_collection_without_as - assert_equal "local_inspector,local_inspector_counter,object", + assert_equal "collection_size,first_in_collection,last_in_collection,local_inspector,local_inspector_counter,object", @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ]) end + + def test_render_partial_collection_size + assert_equal "3-true-false,3-false-false,3-false-true,", + @view.render(:partial => "test/partial_with_collection_size", :collection => [ Customer.new("david"), Customer.new("josh"), Customer.new("mary") ]) + end # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout -- 1.5.5.4