From e56c63542780fe2fb804636a875f95cae08ab3f4 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 15 Feb 2015 20:34:18 +0100 Subject: Merge multi_fetch_fragments. Makes caching a collection of template partials faster using `read_multi` on the Rails cache store. Some caching implementations have optimized `read_multi` so we don't have to check in the cache store for every template. --- actionview/test/template/render_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'actionview/test/template') diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb index f77b81f0ee..73871cf974 100644 --- a/actionview/test/template/render_test.rb +++ b/actionview/test/template/render_test.rb @@ -597,3 +597,17 @@ class LazyViewRenderTest < ActiveSupport::TestCase silence_warnings { Encoding.default_external = old } end end + +class CachedCollectionViewRenderTest < CachedViewRenderTest + test "with custom key" do + customer = Customer.new("david") + key = ActionController::Base.new.fragment_cache_key([customer, 'key']) + + ActionView::PartialRenderer.collection_cache.write(key, 'Hello') + + assert_equal "Hello", + @view.render(partial: "test/customer", collection: [customer], cache: ->(item) { [item, 'key'] }) + + ActionView::PartialRenderer.collection_cache.clear + end +end -- cgit v1.2.3 From 11644fd0ceb99f3f0529323df5ad625c596b3f21 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 15 Feb 2015 22:39:04 +0100 Subject: 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 %> ``` --- actionview/test/template/render_test.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'actionview/test/template') diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb index 73871cf974..691636a8f9 100644 --- a/actionview/test/template/render_test.rb +++ b/actionview/test/template/render_test.rb @@ -599,6 +599,12 @@ class LazyViewRenderTest < ActiveSupport::TestCase end class CachedCollectionViewRenderTest < CachedViewRenderTest + class CachedCustomer < Customer; end + + teardown do + ActionView::PartialRenderer.collection_cache.clear + end + test "with custom key" do customer = Customer.new("david") key = ActionController::Base.new.fragment_cache_key([customer, 'key']) @@ -607,7 +613,25 @@ class CachedCollectionViewRenderTest < CachedViewRenderTest assert_equal "Hello", @view.render(partial: "test/customer", collection: [customer], cache: ->(item) { [item, 'key'] }) + end - ActionView::PartialRenderer.collection_cache.clear + test "automatic caching with inferred cache name" do + customer = CachedCustomer.new("david") + key = ActionController::Base.new.fragment_cache_key(customer) + + ActionView::PartialRenderer.collection_cache.write(key, 'Cached') + + assert_equal "Cached", + @view.render(partial: "test/cached_customer", collection: [customer]) + end + + test "automatic caching with as name" do + customer = CachedCustomer.new("david") + key = ActionController::Base.new.fragment_cache_key(customer) + + ActionView::PartialRenderer.collection_cache.write(key, 'Cached') + + assert_equal "Cached", + @view.render(partial: "test/cached_customer_as", collection: [customer], as: :buyer) end end -- cgit v1.2.3