mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Fix fragment caching inherited serializers to use distinct per-serializer caches.
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
require 'test_helper'
|
||||
require 'tmpdir'
|
||||
require 'tempfile'
|
||||
|
||||
InheritedRoleSerializer = Class.new(RoleSerializer) do
|
||||
cache key: 'inherited_role', only: [:name, :special_attribute]
|
||||
attribute :special_attribute
|
||||
end
|
||||
|
||||
module ActiveModelSerializers
|
||||
class CacheTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@@ -150,6 +156,14 @@ module ActiveModelSerializers
|
||||
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
|
||||
end
|
||||
|
||||
def test_fragment_cache_with_inheritance
|
||||
inherited = render_object_with_cache(@role, serializer: InheritedRoleSerializer)
|
||||
base = render_object_with_cache(@role)
|
||||
|
||||
assert_includes(inherited.keys, :special_attribute)
|
||||
refute_includes(base.keys, :special_attribute)
|
||||
end
|
||||
|
||||
def test_uses_file_digest_in_cache_key
|
||||
render_object_with_cache(@blog)
|
||||
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
||||
@@ -242,8 +256,8 @@ module ActiveModelSerializers
|
||||
|
||||
private
|
||||
|
||||
def render_object_with_cache(obj)
|
||||
ActiveModel::SerializableResource.new(obj).serializable_hash
|
||||
def render_object_with_cache(obj, options = {})
|
||||
ActiveModel::SerializableResource.new(obj, options).serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user