mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Default the generated cache key to use custom #strftime instead of raw #to_s to achieve more accurate precision
This commit is contained in:
parent
c0a82648d5
commit
6892ca39c9
@ -72,7 +72,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def object_cache_key
|
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
|
end
|
||||||
|
|
||||||
def meta
|
def meta
|
||||||
|
|||||||
@ -98,13 +98,12 @@ module ActionController
|
|||||||
|
|
||||||
def render_fragment_changed_object_with_relationship
|
def render_fragment_changed_object_with_relationship
|
||||||
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
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.')
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
||||||
post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author })
|
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })
|
||||||
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 })
|
|
||||||
|
|
||||||
generate_cached_serializer(like)
|
generate_cached_serializer(like)
|
||||||
like.post = post2
|
like.likable = comment2
|
||||||
like.time = DateTime.now.to_s
|
like.time = DateTime.now.to_s
|
||||||
|
|
||||||
render json: like
|
render json: like
|
||||||
@ -229,7 +228,7 @@ module ActionController
|
|||||||
assert_equal expected.to_json, @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
|
|
||||||
get :render_changed_object_with_cache_enabled
|
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
|
ActionController::Base.cache_store.clear
|
||||||
get :render_changed_object_with_cache_enabled
|
get :render_changed_object_with_cache_enabled
|
||||||
@ -291,10 +290,9 @@ module ActionController
|
|||||||
expected_return = {
|
expected_return = {
|
||||||
"id"=>1,
|
"id"=>1,
|
||||||
"time"=>DateTime.now.to_s,
|
"time"=>DateTime.now.to_s,
|
||||||
"post" => {
|
"likeable" => {
|
||||||
"id"=>1,
|
"id"=>1,
|
||||||
"title"=>"New Post",
|
"body"=>"ZOMG A COMMENT"
|
||||||
"body"=>"Body"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
test/fixtures/poro.rb
vendored
13
test/fixtures/poro.rb
vendored
@ -10,7 +10,7 @@ class Model
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cache_key
|
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
|
end
|
||||||
|
|
||||||
def cache_key_with_digest
|
def cache_key_with_digest
|
||||||
@ -18,7 +18,7 @@ class Model
|
|||||||
end
|
end
|
||||||
|
|
||||||
def updated_at
|
def updated_at
|
||||||
@attributes[:updated_at] ||= DateTime.now.to_time.to_i
|
@attributes[:updated_at] ||= DateTime.now.to_time
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
def read_attribute_for_serialization(name)
|
||||||
@ -69,7 +69,6 @@ end
|
|||||||
|
|
||||||
Post = Class.new(Model)
|
Post = Class.new(Model)
|
||||||
Like = Class.new(Model)
|
Like = Class.new(Model)
|
||||||
Comment = Class.new(Model)
|
|
||||||
Author = Class.new(Model)
|
Author = Class.new(Model)
|
||||||
Bio = Class.new(Model)
|
Bio = Class.new(Model)
|
||||||
Blog = Class.new(Model)
|
Blog = Class.new(Model)
|
||||||
@ -77,6 +76,12 @@ Role = Class.new(Model)
|
|||||||
User = Class.new(Model)
|
User = Class.new(Model)
|
||||||
Location = Class.new(Model)
|
Location = Class.new(Model)
|
||||||
Place = 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
|
module Spam; end
|
||||||
Spam::UnrelatedLink = Class.new(Model)
|
Spam::UnrelatedLink = Class.new(Model)
|
||||||
@ -143,7 +148,7 @@ end
|
|||||||
LikeSerializer = Class.new(ActiveModel::Serializer) do
|
LikeSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
attributes :id, :time
|
attributes :id, :time
|
||||||
|
|
||||||
belongs_to :post
|
belongs_to :likeable
|
||||||
end
|
end
|
||||||
|
|
||||||
LocationSerializer = Class.new(ActiveModel::Serializer) do
|
LocationSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
|||||||
@ -48,7 +48,7 @@ module ActiveModel
|
|||||||
def test_cache_key_interpolation_with_updated_at
|
def test_cache_key_interpolation_with_updated_at
|
||||||
author = render_object_with_cache(@author)
|
author = render_object_with_cache(@author)
|
||||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
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
|
end
|
||||||
|
|
||||||
def test_default_cache_key_fallback
|
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.
|
# Generate a new Cache of Post object and each objects related to it.
|
||||||
render_object_with_cache(@post)
|
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(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||||
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user