From 61d4d738e1f56fef1fd0ee00e5ff2047b8e03ea0 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 26 Jul 2016 14:14:41 -0300 Subject: [PATCH] Merge pull request #25912 from stevenharman/fix_render_partial_collection_to_allow_custom_collection Changed partial rendering to allow collections which don't implement `#to_ary`. --- actionview/CHANGELOG.md | 11 +++++++++++ .../lib/action_view/renderer/partial_renderer.rb | 2 +- actionview/test/template/render_test.rb | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index ba030f3fe1b15..0978ef8b98840 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,5 +1,16 @@ ## Rails 5.0.0 (June 30, 2016) ## +* Changed partial rendering with a collection to allow collections which + implement `to_a`. + + Extracting the collection option had an optimization to avoid unnecessary + queries of ActiveRecord Relations by calling `#to_ary` on the given + collection. Instances of `Enumerator` or `Enumerable` are valid + collections, but they do not implement `#to_ary`. By changing this to + `#to_a`, they will now be extracted and rendered as expected. + + *Steven Harman* + * Change `datetime_field` and `datetime_field_tag` to generate `datetime-local` fields. As a new specification of the HTML 5 the text field type `datetime` will no longer exist diff --git a/actionview/lib/action_view/renderer/partial_renderer.rb b/actionview/lib/action_view/renderer/partial_renderer.rb index 13b4ec6133a19..398d961fff00d 100644 --- a/actionview/lib/action_view/renderer/partial_renderer.rb +++ b/actionview/lib/action_view/renderer/partial_renderer.rb @@ -403,7 +403,7 @@ def setup(context, options, block) def collection_from_options if @options.key?(:collection) collection = @options[:collection] - collection.respond_to?(:to_ary) ? collection.to_ary : [] + collection ? collection.to_a : [] end end diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb index 25b21850b15ec..68574d4adb721 100644 --- a/actionview/test/template/render_test.rb +++ b/actionview/test/template/render_test.rb @@ -309,6 +309,14 @@ def test_render_partial_with_nil_collection_should_return_nil assert_nil @view.render(:partial => "test/customer", :collection => nil) end + def test_render_partial_collection_for_non_array + customers = Enumerator.new do |y| + y.yield(Customer.new("david")) + y.yield(Customer.new("mary")) + end + assert_equal "Hello: davidHello: mary", @view.render(partial: "test/customer", collection: customers) + end + def test_render_partial_without_object_does_not_put_partial_name_to_local_assigns assert_equal 'false', @view.render(partial: 'test/partial_name_in_local_assigns') end