aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/renderer
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2015-02-15 22:39:04 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2015-02-21 16:06:57 +0100
commit11644fd0ceb99f3f0529323df5ad625c596b3f21 (patch)
treed81f01715fd7966d193a657cc452edd308234fb0 /actionview/lib/action_view/renderer
parente56c63542780fe2fb804636a875f95cae08ab3f4 (diff)
downloadrails-11644fd0ceb99f3f0529323df5ad625c596b3f21.tar.gz
rails-11644fd0ceb99f3f0529323df5ad625c596b3f21.tar.bz2
rails-11644fd0ceb99f3f0529323df5ad625c596b3f21.zip
Collections automatically cache and fetch partials.
Collections can take advantage of `multi_read` if they render one template and their partials begin with a cache call. The cache call must correspond to either what the collections elements are rendered as, or match the inferred name of the partial. So with a notifications/_notification.html.erb template like: ```ruby <% cache notification %> <%# ... %> <% end %> ``` A collection would be able to use `multi_read` if rendered like: ```ruby <%= render @notifications %> <%= render partial: 'notifications/notification', collection: @notifications, as: :notification %> ```
Diffstat (limited to 'actionview/lib/action_view/renderer')
-rw-r--r--actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb17
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