From 535a33a33b9bb2e0115eb4b6c4638ba1034077eb Mon Sep 17 00:00:00 2001 From: twinturbo Date: Wed, 11 Jul 2012 16:51:15 +0200 Subject: [PATCH] Have to opt in with #cache_key --- lib/active_model/serializer.rb | 12 +----------- test/caching_test.rb | 12 ++++++++---- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 8415a8d3..3b71b758 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -441,17 +441,7 @@ module ActiveModel end def perform_caching? - perform_caching && cache && cache_key - end - - # Override this method if you want to create a key based on associations - # Here's an example: your serializer has associations and the scope - # - # def cache_key - # [ object, scope, scope.comments ] - # end - def cache_key - object.try :cache_key + perform_caching && cache && try(:cache_key) end def expand_cache_key(*args) diff --git a/test/caching_test.rb b/test/caching_test.rb index 5c31373f..f3a02144 100644 --- a/test/caching_test.rb +++ b/test/caching_test.rb @@ -30,10 +30,6 @@ class CachingTest < ActiveModel::TestCase %w(ruby) end - def cache_key - name - end - def read_attribute_for_serialization(name) send name end @@ -61,6 +57,10 @@ class CachingTest < ActiveModel::TestCase def self.to_s 'serializer' end + + def cache_key + object.name + end end serializer.cache = NullStore.new @@ -82,6 +82,10 @@ class CachingTest < ActiveModel::TestCase def self.to_s 'serializer' end + + def cache_key + object.name + end end serializer.cache = NullStore.new