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

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