From 4d5abf3f7b54425a39a61d5f34a69f52bd9f0723 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Wed, 13 Aug 2008 20:02:54 +0100 Subject: [PATCH] Add the :empty option when rendering collections --- actionpack/lib/action_controller/base.rb | 11 +++++- actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/partials.rb | 16 ++++++-- actionpack/test/controller/new_render_test.rb | 37 ++++++++++++++++++++ .../test/fixtures/test/_nothing_found.html.erb | 1 + 5 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 actionpack/test/fixtures/test/_nothing_found.html.erb diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 0fdbcbd..71e5b10 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -738,6 +738,15 @@ 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 specified text if the collection is empty + # render :partial => "person", :collection => @winners, :empty => "No results" + # + # # Renders a collection of partials but renders the specified inline templace if the collection is empty + # render :partial => "person", :collection => @winners, :empty => {:inline => "No results found for <%= h query %>"} + # + # # Renders a collection of partials but renders the no results partial if the collection is empty + # render :partial => "person", :collection => @winners, :empty => {:partial => "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 +930,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], 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..b9ff843 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], 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..afd3233 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,9 +126,17 @@ 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 = nil, local_assigns = {}, as = nil) #:nodoc: + if collection.empty? + return case empty + when String + empty + when Hash + render empty + else + ' ' + end + end 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..f433ff6 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -160,6 +160,23 @@ class NewRenderTestController < ActionController::Base render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ] end + def partial_with_empty_text_and_non_empty_collection + render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary")], :empty => 'Nothing found' + end + + def partial_with_empty_text_and_empty_collection + render :partial => "customer", :collection => [], :empty => 'Nothing found' + end + + def partial_with_empty_partial_and_empty_collection + render :partial => "customer", :collection => [], :empty => {:partial => 'nothing_found'} + end + + def partial_with_empty_inline_and_empty_collection + @message = 'Nothing found' + render :partial => "customer", :collection => [], :empty => {:inline => '<%= @message%>'} + end + def partial_collection_with_counter render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ] end @@ -829,6 +846,26 @@ EOS end end + def test_partial_with_empty_text_and_non_empty_collection + get :partial_with_empty_text_and_non_empty_collection + assert_equal "Hello: davidHello: mary", @response.body + end + + def test_partial_with_empty_text_and_empty_collection + get :partial_with_empty_text_and_empty_collection + assert_equal "Nothing found", @response.body + end + + def test_partial_with_empty_inline_and_empty_collection + get :partial_with_empty_inline_and_empty_collection + assert_equal "Nothing found", @response.body + end + + def test_partial_with_empty_partial_and_empty_collection + get :partial_with_empty_partial_and_empty_collection + assert_equal "Nothing found", @response.body + end + def test_render_text_with_assigns get :render_text_with_assigns assert_equal "world", assigns["hello"] diff --git a/actionpack/test/fixtures/test/_nothing_found.html.erb b/actionpack/test/fixtures/test/_nothing_found.html.erb new file mode 100644 index 0000000..fd7481b --- /dev/null +++ b/actionpack/test/fixtures/test/_nothing_found.html.erb @@ -0,0 +1 @@ +Nothing found \ No newline at end of file -- 1.5.4.4