Fix fragment caching inherited serializers to use distinct per-serializer caches.

This commit is contained in:
Logan Serman
2016-03-25 16:24:43 -05:00
parent 5af7d96294
commit d0389ca765
3 changed files with 24 additions and 7 deletions

View File

@@ -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