-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Adds documentation for overriding default serializer based on conditions #1730
Conversation
## Overriding default serializer | ||
|
||
You can use the `serializer_for` method to return a different serializer based on | ||
use/scope specific conditions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wording welcome!
@cgmckeever thanks for this PR! 💯 |
end | ||
|
||
# the rest of the serializer | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@groyoh much better suggestion - updated
This is super handy to document! |
LGTM |
suggested changes update changelog
@@ -19,6 +19,7 @@ Fixes: | |||
|
|||
Misc: | |||
- [#1673](https://github.com/rails-api/active_model_serializers/pull/1673) Adds "How to" guide on using AMS with POROs (@DrSayre) | |||
- [#1730](https://github.com/rails-api/active_model_serializers/pull/1730) Adds documentation for overriding default serializer based on conditions (@groyoh/@cgmckeever) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NullVoxPopuli added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wooooo
edit: the issue is we are on v0.8.3. oops @groyoh hey! so I don't think this documentation is correct. I tried implementing this and it doesn't seem to have the desired effect.
Override the serializer_for method on MySerializer doesn't do anything, because the code always uses the definition of
So I think the way it is currently structured, it is impossible to have custom serializers for polymorphic associations in a given parent serializer. edit: I spoke too soon... so I guess it should be using the calling class's definition of serializer_for:
but I am still not able to get the described behavior... |
Purpose
Conversation on 'dynamic serializers' spawned an interesting solution via @groyoh
Just documenting it here as its a 💯 #protip
Changes
Updated serializer docs
Related GitHub issues
#1719