From 44fe17e604f16536c5533b80c751bf92f5973046 Mon Sep 17 00:00:00 2001 From: Joseph ROUFF Date: Fri, 11 Sep 2009 08:56:24 +0200 Subject: [PATCH] When a collection rendered is empty it's now possible to assign to it a default template --- actionpack/lib/action_view/render/partials.rb | 16 +++++++++++++--- actionpack/test/fixtures/test/_no_customer.erb | 1 + actionpack/test/template/render_test.rb | 4 ++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 actionpack/test/fixtures/test/_no_customer.erb diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 7f10f54..12b62ba 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -35,6 +35,12 @@ module ActionView # iteration counter will automatically be made available to the template with a name of the form # +partial_name_counter+. In the case of the example above, the template would be fed +ad_counter+. # + # When a collection is empty it's possible to assign to it a default template : + # + # <%= render :partial => "ad", :collection => @advertisements, :default_template => "no_ad" %> + # + # This will render "advertiser/_no_ad.erb" only if the collection is empty. + # # NOTE: Due to backwards compatibility concerns, the collection can't be one of hashes. Normally you'd also # just keep domain objects, like Active Records, in there. # @@ -215,9 +221,13 @@ module ActionView def render_collection @template = template = find_template - - return nil if @collection.blank? - + + if @collection.blank? && @options.key?(:default_template) + return find_template(@options[:default_template]).render(@view, @locals) + elsif @collection.blank? + return nil + end + if @options.key?(:spacer_template) spacer = find_template(@options[:spacer_template]).render(@view, @locals) end diff --git a/actionpack/test/fixtures/test/_no_customer.erb b/actionpack/test/fixtures/test/_no_customer.erb new file mode 100644 index 0000000..3cebcec --- /dev/null +++ b/actionpack/test/fixtures/test/_no_customer.erb @@ -0,0 +1 @@ +You have not customers yet ! \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index c86d521..c1fb833 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -158,6 +158,10 @@ module RenderTestCases assert_equal "Hello: davidHello: Anonymous", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), nil ]) end + def test_render_partial_with_default_template + assert_equal "You have not customers yet !", @view.render(:partial => "test/customer", :collection => [ ], :default_template => "test/no_customer") + end + def test_render_partial_with_empty_array_should_return_nil assert_nil @view.render(:partial => []) end -- 1.6.2.1