From d2bfa1938538905bb841f215ae6c20abd04361c4 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 local variable in partials rendered from collections. Also assigns counter based on :as parameter if specified. --- actionpack/lib/action_view/partials.rb | 1 + actionpack/lib/action_view/renderable_partial.rb | 5 ++++- .../test/_customer_collection_size.html.erb | 1 + actionpack/test/template/render_test.rb | 7 ++++++- 4 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 actionpack/test/fixtures/test/_customer_collection_size.html.erb diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index 894b885..5c8e747 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -138,6 +138,7 @@ module ActionView path = find_partial_path(_partial_path) template = pick_template(path) local_assigns[template.counter_name] = index + local_assigns[:collection_size] = collection.size 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/_customer_collection_size.html.erb b/actionpack/test/fixtures/test/_customer_collection_size.html.erb new file mode 100644 index 0000000..732614f --- /dev/null +++ b/actionpack/test/fixtures/test/_customer_collection_size.html.erb @@ -0,0 +1 @@ +rendering <%= customer_counter + 1 %> of <%= collection_size -%> \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 31cfdce..030d063 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,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 "rendering 1 of 2rendering 2 of 2", + @view.render(:partial => "test/customer_collection_size", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer) + end # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout -- 1.5.5.4