Skip to content

Commit

Permalink
Adds documentation for overriding default serializer based on conditi…
Browse files Browse the repository at this point in the history
…ons (#1730)

suggested changes

update changelog
  • Loading branch information
cgmckeever authored and NullVoxPopuli committed May 17, 2016
1 parent b75db81 commit ec15fa9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions docs/general/serializers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit ec15fa9

Please sign in to comment.