diff options
Diffstat (limited to 'actionview/lib/action_view/renderer')
-rw-r--r-- | actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb | 17 |
1 files changed, 15 insertions, 2 deletions
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 0f2ab887bc..b77c884e66 100644 --- a/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb +++ b/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb @@ -26,11 +26,24 @@ module ActionView end def cache_collection? - @options[:cache].present? + @options.fetch(:cache, automatic_cache_eligible?) + end + + def automatic_cache_eligible? + single_template_render? && !callable_cache_key? && + @template.eligible_for_collection_caching?(as: @options[:as]) + end + + def single_template_render? + @template # Template is only set when a collection renders one template. + end + + def callable_cache_key? + @options[:cache].respond_to?(:call) end def collection_by_cache_keys - seed = @options[:cache].respond_to?(:call) ? @options[:cache] : ->(i) { i } + seed = callable_cache_key? ? @options[:cache] : ->(i) { i } @collection.each_with_object({}) do |item, hash| hash[expanded_cache_key(seed.call(item))] = item |