Adds documentation for overriding default serializer based on conditions (#1730)

suggested changes

update changelog
This commit is contained in:
cgmckeever 2016-05-17 14:28:54 -05:00 committed by L. Preston Sego III
parent b75db81ca4
commit ec15fa9de3
2 changed files with 17 additions and 0 deletions

View File

@ -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)

View File

@ -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
```