Skip to content

Commit 355e0f6

Browse files
committed
Merge branch 'lserman-master'
Followup needed: - Update code comments #1622 (comment) - Move test class into test scope #1622 (comment)
2 parents c7b2916 + d0389ca commit 355e0f6

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Features:
2727
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
2828

2929
Fixes:
30+
- [#1622](https://github.com/rails-api/active_model_serializers/pull/1622) Fragment cache changed from per-record to per-serializer.
31+
Now, two serializers that use the same model may be separately cached. (@lserman)
3032
- [#1478](https://github.com/rails-api/active_model_serializers/pull/1478) Cache store will now be correctly set when serializers are
3133
loaded *before* Rails initializes. (@bf4)
3234
- [#1570](https://github.com/rails-api/active_model_serializers/pull/1570) Fixed pagination issue with last page size. (@bmorrall)

lib/active_model_serializers/fragment_cache.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def fetch
1515
object = serializer.object
1616

1717
# It will split the serializer into two, one that will be cached and one that will not
18-
serializers = fragment_serializer(object.class.name)
18+
serializers = fragment_serializer
1919

2020
# Get serializable hash from both
2121
cached_hash = serialize(object, serializers[:cached])
@@ -76,13 +76,15 @@ def add_attributes_to_serializer(serializer, attributes, attributes_keys)
7676
# @example
7777
# When +name+ is <tt>User::Admin</tt>
7878
# creates the Serializer classes (if they don't exist).
79-
# User_AdminCachedSerializer
80-
# User_AdminNonCachedSerializer
79+
# CachedUser_AdminSerializer
80+
# NonCachedUser_AdminSerializer
8181
#
82-
def fragment_serializer(name)
83-
klass = serializer.class
84-
cached = "#{to_valid_const_name(name)}CachedSerializer"
85-
non_cached = "#{to_valid_const_name(name)}NonCachedSerializer"
82+
def fragment_serializer
83+
klass = serializer.class
84+
serializer_class_name = to_valid_const_name(klass.name)
85+
86+
cached = "Cached#{serializer_class_name}"
87+
non_cached = "NonCached#{serializer_class_name}"
8688

8789
cached_serializer = get_or_create_serializer(cached)
8890
non_cached_serializer = get_or_create_serializer(non_cached)

test/cache_test.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
require 'test_helper'
22
require 'tmpdir'
33
require 'tempfile'
4+
45
module ActiveModelSerializers
56
class CacheTest < ActiveSupport::TestCase
7+
InheritedRoleSerializer = Class.new(RoleSerializer) do
8+
cache key: 'inherited_role', only: [:name, :special_attribute]
9+
attribute :special_attribute
10+
end
11+
612
def setup
713
ActionController::Base.cache_store.clear
814
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@@ -150,6 +156,14 @@ def test_fragment_fetch_with_virtual_associations
150156
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
151157
end
152158

159+
def test_fragment_cache_with_inheritance
160+
inherited = render_object_with_cache(@role, serializer: InheritedRoleSerializer)
161+
base = render_object_with_cache(@role)
162+
163+
assert_includes(inherited.keys, :special_attribute)
164+
refute_includes(base.keys, :special_attribute)
165+
end
166+
153167
def test_uses_file_digest_in_cache_key
154168
render_object_with_cache(@blog)
155169
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
@@ -242,8 +256,8 @@ def test_warn_on_serializer_not_defined_in_file
242256

243257
private
244258

245-
def render_object_with_cache(obj)
246-
ActiveModel::SerializableResource.new(obj).serializable_hash
259+
def render_object_with_cache(obj, options = {})
260+
ActiveModel::SerializableResource.new(obj, options).serializable_hash
247261
end
248262
end
249263
end

0 commit comments

Comments
 (0)