aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/template
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/test/template
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/test/template')
-rw-r--r--actionview/test/template/render_test.rb26
1 files changed, 25 insertions, 1 deletions
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