From ce426ef811999a9d839486e516d1c48ef20659f7 Mon Sep 17 00:00:00 2001 From: James MacAulay Date: Thu, 30 Sep 2010 15:21:29 -0400 Subject: [PATCH] fix rendering a partial with an array as its :object [#5746 state:resolved] --- actionpack/lib/action_view/render/partials.rb | 10 +++++++--- .../test/fixtures/test/_object_inspector.erb | 1 + actionpack/test/template/render_test.rb | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 actionpack/test/fixtures/test/_object_inspector.erb diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index cc9b444..bcb4d91 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -236,7 +236,7 @@ module ActionView else @object = partial - if @collection = collection + if @collection = collection_from_object || collection paths = @collection_paths = @collection.map { |o| partial_path(o) } @path = paths.uniq.size == 1 ? paths.first : nil else @@ -335,10 +335,14 @@ module ActionView private def collection + if @options.key?(:collection) + @options[:collection] || [] + end + end + + def collection_from_object if @object.respond_to?(:to_ary) @object - elsif @options.key?(:collection) - @options[:collection] || [] end end diff --git a/actionpack/test/fixtures/test/_object_inspector.erb b/actionpack/test/fixtures/test/_object_inspector.erb new file mode 100644 index 0000000..53af593 --- /dev/null +++ b/actionpack/test/fixtures/test/_object_inspector.erb @@ -0,0 +1 @@ +<%= object_inspector.inspect -%> \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 205fdcf..756d8d0 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -127,6 +127,10 @@ module RenderTestCases assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david")) end + def test_render_object_with_array + assert_equal "[1, 2, 3]", @view.render(:partial => "test/object_inspector", :object => [1, 2, 3]) + end + def test_render_partial_collection assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ]) end -- 1.7.1