From 8e331713f2496396405787e4364bad9e0ac529fa Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen <kaspth@gmail.com> Date: Fri, 12 Feb 2016 23:00:53 +0100 Subject: Write to collection cache where the template is rendered. Moves us closer to having access to a local template variable, we can ask for eligibility and its virtual_path. Currently we rely on `@template`, which we don't have available when rendering collections without a fixed template. --- actionview/lib/action_view/renderer/partial_renderer.rb | 7 +++++++ .../renderer/partial_renderer/collection_caching.rb | 17 ++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/renderer/partial_renderer.rb b/actionview/lib/action_view/renderer/partial_renderer.rb index bdbf03191a..a9bd257e76 100644 --- a/actionview/lib/action_view/renderer/partial_renderer.rb +++ b/actionview/lib/action_view/renderer/partial_renderer.rb @@ -428,6 +428,8 @@ module ActionView layout = find_template(layout, @template_keys) end + collection_cache_eligible = automatic_cache_eligible? + partial_iteration = PartialIteration.new(@collection.size) locals[iteration] = partial_iteration @@ -436,6 +438,11 @@ module ActionView locals[counter] = partial_iteration.index content = template.render(view, locals) + + if collection_cache_eligible + collection_cache_rendered_partial(content, object) + end + content = layout.render(view, locals) { content } if layout partial_iteration.iterate! content diff --git a/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb b/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb index c353eb0b31..a037424498 100644 --- a/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb +++ b/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb @@ -51,16 +51,15 @@ module ActionView end def fetch_or_cache_partial(cached_partials, order_by:) - rely_on_individual_cache_call = !callable_cache_key? - order_by.map do |key| - cached_partials.fetch(key) do - yield.tap do |rendered_partial| - unless rely_on_individual_cache_call - collection_cache.write(key, rendered_partial, @options[:cache_options]) - end - end - end + cached_partials.fetch(key) { yield } + end + end + + def collection_cache_rendered_partial(rendered_partial, key_by) + if callable_cache_key? + key = expanded_cache_key(@options[:cache].call(key_by)) + collection_cache.write(key, rendered_partial, @options[:cache_options]) end end end -- cgit v1.2.3