From 6892ca39c90a96f9c05f2d4fcd7e376274294b54 Mon Sep 17 00:00:00 2001 From: Aaron Lerch Date: Thu, 25 Jun 2015 23:40:18 -0400 Subject: [PATCH] Default the generated cache key to use custom #strftime instead of raw #to_s to achieve more accurate precision --- lib/active_model/serializer/adapter.rb | 4 +++- test/action_controller/serialization_test.rb | 14 ++++++-------- test/fixtures/poro.rb | 13 +++++++++---- test/serializers/cache_test.rb | 4 ++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/active_model/serializer/adapter.rb b/lib/active_model/serializer/adapter.rb index 5750214e..852fa53e 100644 --- a/lib/active_model/serializer/adapter.rb +++ b/lib/active_model/serializer/adapter.rb @@ -72,7 +72,9 @@ module ActiveModel end def object_cache_key - (@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{@cached_serializer.object.updated_at}" : @cached_serializer.object.cache_key + object_time_safe = @cached_serializer.object.updated_at + object_time_safe = object_time_safe.strftime("%Y%m%d%H%M%S%9N") if object_time_safe.respond_to?(:strftime) + (@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key end def meta diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index e84c4b7e..08b32035 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -98,13 +98,12 @@ module ActionController def render_fragment_changed_object_with_relationship comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) + comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' }) author = Author.new(id: 1, name: 'Joao Moura.') - post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author }) - post2 = Post.new({ id: 1, title: 'New Post2', body: 'Body2', comments: [comment], author: author }) - like = Like.new({ id: 1, post: post, time: 3.days.ago }) + like = Like.new({ id: 1, likeable: comment, time: 3.days.ago }) generate_cached_serializer(like) - like.post = post2 + like.likable = comment2 like.time = DateTime.now.to_s render json: like @@ -229,7 +228,7 @@ module ActionController assert_equal expected.to_json, @response.body get :render_changed_object_with_cache_enabled - assert_equal expected.to_json, @response.body + assert_not_equal expected.to_json, @response.body ActionController::Base.cache_store.clear get :render_changed_object_with_cache_enabled @@ -291,10 +290,9 @@ module ActionController expected_return = { "id"=>1, "time"=>DateTime.now.to_s, - "post" => { + "likeable" => { "id"=>1, - "title"=>"New Post", - "body"=>"Body" + "body"=>"ZOMG A COMMENT" } } diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 29e193ee..ee1913ec 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -10,7 +10,7 @@ class Model end def cache_key - "#{self.class.name.downcase}/#{self.id}-#{self.updated_at}" + "#{self.class.name.downcase}/#{self.id}-#{self.updated_at.strftime("%Y%m%d%H%M%S%9N")}" end def cache_key_with_digest @@ -18,7 +18,7 @@ class Model end def updated_at - @attributes[:updated_at] ||= DateTime.now.to_time.to_i + @attributes[:updated_at] ||= DateTime.now.to_time end def read_attribute_for_serialization(name) @@ -69,7 +69,6 @@ end Post = Class.new(Model) Like = Class.new(Model) -Comment = Class.new(Model) Author = Class.new(Model) Bio = Class.new(Model) Blog = Class.new(Model) @@ -77,6 +76,12 @@ Role = Class.new(Model) User = Class.new(Model) Location = Class.new(Model) Place = Class.new(Model) +Comment = Class.new(Model) do + # Uses a custom non-time-based cache key + def cache_key + "#{self.class.name.downcase}/#{self.id}" + end +end module Spam; end Spam::UnrelatedLink = Class.new(Model) @@ -143,7 +148,7 @@ end LikeSerializer = Class.new(ActiveModel::Serializer) do attributes :id, :time - belongs_to :post + belongs_to :likeable end LocationSerializer = Class.new(ActiveModel::Serializer) do diff --git a/test/serializers/cache_test.rb b/test/serializers/cache_test.rb index eb0f9e0d..37254ac4 100644 --- a/test/serializers/cache_test.rb +++ b/test/serializers/cache_test.rb @@ -48,7 +48,7 @@ module ActiveModel def test_cache_key_interpolation_with_updated_at author = render_object_with_cache(@author) assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key)) - assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at}").to_json) + assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json) end def test_default_cache_key_fallback @@ -85,7 +85,7 @@ module ActiveModel # Generate a new Cache of Post object and each objects related to it. render_object_with_cache(@post) - # Check if if cache the objects separately + # Check if it cached the objects separately assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key)) assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))