aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/caching_test.rb
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 /actionpack/test/controller/caching_test.rb
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 'actionpack/test/controller/caching_test.rb')
-rw-r--r--actionpack/test/controller/caching_test.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index 4760ec1698..2d6607041d 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -1,5 +1,6 @@
require 'fileutils'
require 'abstract_unit'
+require 'lib/controller/fake_models'
CACHE_DIR = 'test_cache'
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
@@ -349,3 +350,60 @@ class ViewCacheDependencyTest < ActionController::TestCase
assert_equal %w(trombone flute), HasDependenciesController.new.view_cache_dependencies
end
end
+
+class CollectionCacheController < ActionController::Base
+ def index
+ @customers = [Customer.new('david', params[:id] || 1)]
+ end
+
+ def index_ordered
+ @customers = [Customer.new('david', 1), Customer.new('david', 2), Customer.new('david', 3)]
+ render 'index'
+ end
+
+ def index_explicit_render
+ @customers = [Customer.new('david', 1)]
+ render partial: 'customers/customer', collection: @customers
+ end
+
+ def index_with_comment
+ @customers = [Customer.new('david', 1)]
+ render partial: 'customers/commented_customer', collection: @customers, as: :customer
+ end
+end
+
+class AutomaticCollectionCacheTest < ActionController::TestCase
+ def setup
+ super
+ @controller = CollectionCacheController.new
+ @controller.perform_caching = true
+ @controller.cache_store = ActiveSupport::Cache::MemoryStore.new
+ end
+
+ def test_collection_fetches_cached_views
+ get :index
+
+ ActionView::PartialRenderer.expects(:collection_with_template).never
+ get :index
+ end
+
+ def test_preserves_order_when_reading_from_cache_plus_rendering
+ get :index, params: { id: 2 }
+ get :index_ordered
+
+ assert_select ':root', "david, 1\n david, 2\n david, 3"
+ end
+
+ def test_explicit_render_call_with_options
+ get :index_explicit_render
+
+ assert_select ':root', "david, 1"
+ end
+
+ def test_caching_works_with_beginning_comment
+ get :index_with_comment
+
+ ActionView::PartialRenderer.expects(:collection_with_template).never
+ get :index_with_comment
+ end
+end