From 5816ebef052bc473182fb8494980eb83d45ededd Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sat, 20 Nov 2010 23:37:09 +0700 Subject: [PATCH 2/2] Allow `:spacer_template` and `:if_empty` to be supplied in the short-hand partial render form So, you'll be able to do this: render(@posts, :if_empty => "no_post") and it will render "_no_post.html.erb" for you if the `@posts` is empty. --- actionpack/lib/action_view/partials.rb | 5 +++++ .../lib/action_view/renderer/partial_renderer.rb | 5 +++++ actionpack/test/template/render_test.rb | 12 ++++++++++++ 3 files changed, 22 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index 8dc704e..919e617 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -124,6 +124,11 @@ module ActionView # # <%= render :partial => "posts/post", :collection => @posts %> # <%= render(@posts) %> # + # The short-hand version also support specifying :spacer_template and :if_empty option + # as a second argument. Examples: + # + # <%= render(@posts, :spacer_template => "post_separator", :if_empty => "no_post") %> + # # == Rendering partials with layouts # # Partials can have their own layouts applied to them. These layouts are different than the ones that are diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb index 40afe13..6b282e6 100644 --- a/actionpack/lib/action_view/renderer/partial_renderer.rb +++ b/actionpack/lib/action_view/renderer/partial_renderer.rb @@ -24,6 +24,11 @@ module ActionView @object = partial if @collection = collection_from_object || collection + # Extract some options from locals, as some options might fall into + # the +locals+ hash when calling +render+ in short-hand form + @options[:spacer_template] ||= @locals[:spacer_template] + @options[:if_empty] ||= @locals[:if_empty] + paths = @collection_data = @collection.map { |o| partial_path(o) } @path = paths.uniq.size == 1 ? paths.first : nil else diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index cbafa18..fa86dbe 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -203,6 +203,18 @@ module RenderTestCases @controller_view.render(customers, :greeting => "Hello") end + def test_render_partial_using_collection_with_empty_partial + customers = [ Customer.new("Amazon"), Customer.new("Yahoo") ] + assert_equal "Hello: AmazonHello: Yahoo", + @controller_view.render(customers, :greeting => "Hello", :if_empty => "partial_only") + end + + def test_render_partial_using_empty_collection_with_empty_partial + customers = [] + assert_equal "only partial", + @controller_view.render(customers, :greeting => "Hello", :if_empty => "partial_only") + end + # TODO: The reason for this test is unclear, improve documentation def test_render_partial_and_fallback_to_layout assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" }) -- 1.7.3.1