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

Conversation

bdmac
Copy link
Contributor

@bdmac bdmac commented Jan 22, 2016

We were not previously cloning the type setting into the dynamically
generated cached/non-cached serializers for a given fragment-cached
serializer. This led to the type generated for JsonApi having the wrong
value when fragment caching is enabled by adding either :except or :only
options to cache.

This pulls the type setting from the fragment-cached serializer forward
onto the dynamic caching classes so it is preserved in the output.

bdmac added 2 commits January 22, 2016 11:24
We were not previously cloning the type setting into the dynamically
generated cached/non-cached serializers for a given fragment-cached
serializer. This led to the type generated for JsonApi having the wrong
value when fragment caching is enabled by adding either :except or :only
options to cache.

This pulls the type setting from the fragment-cached serializer forward
onto the dynamic caching classes so it is preserved in the output.
The way CachedSerializer#fragment_cached? worked previously would return
true even in an environment where caching was disabled as defined by
`ActiveModelSerializers.config.perform_caching`.

Added check for `_cache` like in the `cached?` method before checking
whether `_cache_only` or `_cache_except` is set.
@@ -24,7 +24,7 @@ def cached?
end

def fragment_cached?
@klass._cache_only && !@klass._cache_except || !@klass._cache_only && @klass._cache_except
@klass._cache && (@klass._cache_only && !@klass._cache_except || !@klass._cache_only && @klass._cache_except)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be it's own PR. would you mind? TYMessaging@e9e2b57

@bf4
Copy link
Member

bf4 commented Jan 28, 2016

Awesome, thanks! 👍 some comments above

def test_fragment_fetch_with_type_override
type_serializer = TypedRoleSerializer.new(Role.new(name: 'Another Author'))
type_hash = ActiveModel::Serializer::Adapter::JsonApi.new(type_serializer).serializable_hash
assert_equal(type_hash[:data][:type], TypedRoleSerializer._type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use the serializable test helper (below may contain typos)

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

@bdmac
Copy link
Contributor Author

bdmac commented Jan 28, 2016

@bf4 comments addressed. Will open a secondary PR with the fix for fragment_cached?

bf4 added a commit that referenced this pull request Feb 10, 2016
@bf4
Copy link
Member

bf4 commented Feb 10, 2016

Rebase, merged, and changelog added in 527c2ae

Follow PR was #1477

@bf4
Copy link
Member

bf4 commented Feb 10, 2016

Closed by 527c2ae

@bf4
Copy link
Member

bf4 commented Feb 15, 2016

Merged in a409982

@bf4 bf4 closed this Feb 15, 2016
@bf4
Copy link
Member

bf4 commented Feb 15, 2016

This was merged, but GitHub didn't pick it up. I'm not sure why

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants