Skip to content

Commit

Permalink
Merge pull request #25912 from stevenharman/fix_render_partial_collec…
Browse files Browse the repository at this point in the history
…tion_to_allow_custom_collection

Changed partial rendering to allow collections which don't implement `#to_ary`.
  • Loading branch information
spastorino committed Jul 26, 2016
1 parent e2f749d commit 61d4d73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
11 changes: 11 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/renderer/partial_renderer.rb
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions actionview/test/template/render_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 61d4d73

Please sign in to comment.