Skip to content

Commit

Permalink
Cached collections only work if there is one template
Browse files Browse the repository at this point in the history
Cached collections only work if there is one template.  If there are
more than one templates, the caching mechanism doesn't have a key.
  • Loading branch information
tenderlove committed Feb 13, 2019
1 parent 38f9e41 commit bfcdd46
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions actionview/lib/action_view/renderer/partial_renderer.rb
Expand Up @@ -308,6 +308,9 @@ def render(context, options, block)
template = find_partial(@path, @template_keys)
@variable ||= template.variable
else
if options[:cached]
raise NotImplementedError, "render caching requires a template. Please specify a partial when rendering"
end
template = nil
end

Expand Down Expand Up @@ -337,9 +340,14 @@ def render_collection(view, template)
spacer = find_template(@options[:spacer_template], @locals.keys).render(view, @locals)
end

cache_collection_render(payload, view, template) do
template ? collection_with_template(view, template) : collection_without_template(view)
end.join(spacer).html_safe
collection_body = if template
cache_collection_render(payload, view, template) do
collection_with_template(view, template)
end
else
collection_without_template(view)
end
collection_body.join(spacer).html_safe
end
end

Expand Down
11 changes: 11 additions & 0 deletions actionview/test/template/render_test.rb
Expand Up @@ -752,6 +752,17 @@ class CachedCustomer < Customer; end
@view.render(partial: "test/cached_customer", collection: [customer], cached: true)
end

test "collection caching does not work on multi-partials" do
a = Object.new
b = Object.new
def a.to_partial_path; "test/partial_iteration_1"; end
def b.to_partial_path; "test/partial_iteration_2"; end

assert_raises(NotImplementedError) do
@controller_view.render(partial: [a, b], cached: true)
end
end

private
def cache_key(*names, virtual_path)
digest = ActionView::Digestor.digest name: virtual_path, finder: @view.lookup_context, dependencies: []
Expand Down

0 comments on commit bfcdd46

Please sign in to comment.