mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Adds documentation for overriding default serializer based on conditions (#1730)
suggested changes update changelog
This commit is contained in:
parent
b75db81ca4
commit
ec15fa9de3
@ -22,6 +22,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)
|
||||
|
||||
### [v0.10.0.rc5 (2016-04-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc4...v0.10.0.rc5)
|
||||
|
||||
|
||||
@ -370,3 +370,19 @@ class PostSerializer < ActiveModel::Serializer
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## Overriding association serializer lookup
|
||||
|
||||
If you want to define a specific serializer lookup for your associations, you can override
|
||||
the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions.
|
||||
|
||||
```ruby
|
||||
class MySerializer < ActiveModel::Serializer
|
||||
def self.serializer_for(model, options)
|
||||
return SparseAdminSerializer if model.class == 'Admin'
|
||||
super
|
||||
end
|
||||
|
||||
# the rest of the serializer
|
||||
end
|
||||
```
|
||||
|
||||
Loading…
Reference in New Issue
Block a user