mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge branch 'lserman-master'
Followup needed: - Update code comments https://github.com/rails-api/active_model_serializers/pull/1622#discussion_r57750471 - Move test class into test scope https://github.com/rails-api/active_model_serializers/pull/1622#discussion_r57659150
This commit is contained in:
commit
355e0f6a37
@ -27,6 +27,8 @@ Features:
|
||||
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
|
||||
|
||||
Fixes:
|
||||
- [#1622](https://github.com/rails-api/active_model_serializers/pull/1622) Fragment cache changed from per-record to per-serializer.
|
||||
Now, two serializers that use the same model may be separately cached. (@lserman)
|
||||
- [#1478](https://github.com/rails-api/active_model_serializers/pull/1478) Cache store will now be correctly set when serializers are
|
||||
loaded *before* Rails initializes. (@bf4)
|
||||
- [#1570](https://github.com/rails-api/active_model_serializers/pull/1570) Fixed pagination issue with last page size. (@bmorrall)
|
||||
|
||||
@ -15,7 +15,7 @@ module ActiveModelSerializers
|
||||
object = serializer.object
|
||||
|
||||
# It will split the serializer into two, one that will be cached and one that will not
|
||||
serializers = fragment_serializer(object.class.name)
|
||||
serializers = fragment_serializer
|
||||
|
||||
# Get serializable hash from both
|
||||
cached_hash = serialize(object, serializers[:cached])
|
||||
@ -76,13 +76,15 @@ module ActiveModelSerializers
|
||||
# @example
|
||||
# When +name+ is <tt>User::Admin</tt>
|
||||
# creates the Serializer classes (if they don't exist).
|
||||
# User_AdminCachedSerializer
|
||||
# User_AdminNonCachedSerializer
|
||||
# CachedUser_AdminSerializer
|
||||
# NonCachedUser_AdminSerializer
|
||||
#
|
||||
def fragment_serializer(name)
|
||||
klass = serializer.class
|
||||
cached = "#{to_valid_const_name(name)}CachedSerializer"
|
||||
non_cached = "#{to_valid_const_name(name)}NonCachedSerializer"
|
||||
def fragment_serializer
|
||||
klass = serializer.class
|
||||
serializer_class_name = to_valid_const_name(klass.name)
|
||||
|
||||
cached = "Cached#{serializer_class_name}"
|
||||
non_cached = "NonCached#{serializer_class_name}"
|
||||
|
||||
cached_serializer = get_or_create_serializer(cached)
|
||||
non_cached_serializer = get_or_create_serializer(non_cached)
|
||||
|
||||
@ -1,8 +1,14 @@
|
||||
require 'test_helper'
|
||||
require 'tmpdir'
|
||||
require 'tempfile'
|
||||
|
||||
module ActiveModelSerializers
|
||||
class CacheTest < ActiveSupport::TestCase
|
||||
InheritedRoleSerializer = Class.new(RoleSerializer) do
|
||||
cache key: 'inherited_role', only: [:name, :special_attribute]
|
||||
attribute :special_attribute
|
||||
end
|
||||
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user