From 148f8aa4741b8867328069926884a8568f848264 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Sat, 9 Aug 2008 14:51:20 +0100 Subject: [PATCH] Allow render :partial to specify an alternate template for when the collection is empty --- actionpack/lib/action_controller/base.rb | 6 +++++- actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/partials.rb | 6 +++--- actionpack/test/controller/new_render_test.rb | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 0fdbcbd..dc53c98 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -738,6 +738,10 @@ module ActionController #:nodoc: # # person_divider partial between each person partial. # render :partial => "person", :collection => @winners, :spacer_template => "person_divider" # + # # Renders a collection of partials but renders the no_results partial instead + # # if the collection is empty + # render :partial => "person", :collection => @winners, :empty_template => "no_results" + # # # Renders a collection of partials located in a view subfolder # # outside of our current controller. In this example we will be # # rendering app/views/shared/_note.r(html|xml) Inside the partial @@ -921,7 +925,7 @@ module ActionController #:nodoc: if collection = options[:collection] render_for_text( @template.send!(:render_partial_collection, partial, collection, - options[:spacer_template], options[:locals], options[:as]), options[:status] + options[:spacer_template], options[:empty_template], options[:locals], options[:as]), options[:status] ) else render_for_text( diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index ad59d92..829748e 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -257,7 +257,7 @@ module ActionView #:nodoc: elsif options[:file] render_file(options[:file], nil, options[:locals]) elsif options[:partial] && options[:collection] - render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals], options[:as]) + render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:empty_template], options[:locals], options[:as]) elsif options[:partial] render_partial(options[:partial], options[:object], options[:locals]) elsif options[:inline] diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index eb74d4a..808fc94 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -117,7 +117,7 @@ module ActionView when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope if partial_path.any? collection = partial_path - render_partial_collection(nil, collection, nil, local_assigns) + render_partial_collection(nil, collection, nil, nil, local_assigns) else "" end @@ -126,8 +126,8 @@ module ActionView end end - def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}, as = nil) #:nodoc: - return " " if collection.empty? + def render_partial_collection(partial_path, collection, partial_spacer_template = nil, empty_template = nil, local_assigns = {}, as = nil) #:nodoc: + return empty_template ? render(:partial => empty_template) : ' ' if collection.empty? local_assigns = local_assigns ? local_assigns.clone : {} spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : '' diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index d2a3a2b..8fa1615 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -160,6 +160,14 @@ class NewRenderTestController < ActionController::Base render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ] end + def partial_with_empty_template_and_non_empty_collection + render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary")], :empty_template => 'nothing_found' + end + + def partial_with_empty_template_and_empty_collection + render :partial => "customer", :collection => [], :empty_template => 'nothing_found' + end + def partial_collection_with_counter render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ] end @@ -828,6 +836,16 @@ EOS get :missing_partial end end + + def test_partial_with_empty_template_and_empty_collection + get :partial_with_empty_template_and_empty_collection + assert_equal "Nothing found", @response.body + end + + def test_partial_with_empty_template_and_non_empty_collection + get :partial_with_empty_template_and_non_empty_collection + assert_equal "Hello: davidHello: mary", @response.body + end def test_render_text_with_assigns get :render_text_with_assigns -- 1.5.4.4