From 38bf2fcfcc6ae04e65e809003737087afdb1bcba Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sat, 20 Nov 2010 23:37:09 +0700 Subject: [PATCH] Allow `:spacer_template` to be supplied in the short-hand partial render form So, you'll be able to do this: render(@posts, :spacer_template => "spacer") and it will render "_spacer.html.erb" for you between each post --- actionpack/lib/action_view/partials.rb | 5 +++++ .../lib/action_view/renderer/partial_renderer.rb | 8 ++++++-- actionpack/test/template/render_test.rb | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index cd3f130..51fea8a 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -123,6 +123,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") %> + # # == 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 317479a..7452a0f 100644 --- a/actionpack/lib/action_view/renderer/partial_renderer.rb +++ b/actionpack/lib/action_view/renderer/partial_renderer.rb @@ -24,6 +24,10 @@ 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] + paths = @collection_data = @collection.map { |o| partial_path(o) } @path = paths.uniq.size == 1 ? paths.first : nil else @@ -59,7 +63,7 @@ module ActionView def render_collection return nil if @collection.blank? - if @options.key?(:spacer_template) + if @options[:spacer_template] spacer = find_template(@options[:spacer_template]).render(@view, @locals) end @@ -108,7 +112,7 @@ module ActionView locals << @variable_counter if @collection find_template(path, locals) end - end + end def find_template(path=@path, locals=@locals.keys) prefix = @view.controller_prefix unless path.include?(?/) diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 8087429..d9165b9 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -191,6 +191,12 @@ module RenderTestCases @controller_view.render(customers, :greeting => "Hello") end + def test_render_partial_using_collection_with_shorthand_spacer + customers = [ Customer.new("Amazon"), Customer.new("Yahoo") ] + assert_equal "Hello: Amazononly partialHello: Yahoo", + @controller_view.render(customers, :greeting => "Hello", :spacer_template => "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