Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the serializer type when fragment caching #1458

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/active_model/serializer/adapter/fragment_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def fragment_serializer(name, klass)

cached.constantize.cache(klass._cache_options)

# Preserve the type setting in the cached/non-cached serializer classes
cached.constantize.type(klass._type)
non_cached.constantize.type(klass._type)

cached.constantize.fragmented(serializer)
non_cached.constantize.fragmented(serializer)

Expand Down
14 changes: 13 additions & 1 deletion test/adapter/fragment_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ module ActiveModel
class Serializer
module Adapter
class FragmentCacheTest < ActiveSupport::TestCase
TypedRoleSerializer = Class.new(ActiveModel::Serializer) do
type 'my-roles'
cache only: [:name], skip_digest: true
attributes :id, :name, :description

belongs_to :author
end

def setup
super
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
Expand Down Expand Up @@ -31,8 +39,12 @@ def test_fragment_fetch_with_namespaced_object
}
assert_equal(@spam_hash.fetch, expected_result)
end

def test_fragment_fetch_with_type_override
serialization = serializable(Role.new(name: 'Another Author'), serializer: TypedRoleSerializer, adapter: :json_api).serializable_hash
assert_equal(TypedRoleSerializer._type, serialization.fetch(:data).fetch(:type))
end
end
end
end
end