Skip to content

Commit ec15fa9

Browse files
cgmckeeverNullVoxPopuli
authored andcommitted
Adds documentation for overriding default serializer based on conditions (#1730)
suggested changes update changelog
1 parent b75db81 commit ec15fa9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixes:
2222

2323
Misc:
2424
- [#1673](https://github.com/rails-api/active_model_serializers/pull/1673) Adds "How to" guide on using AMS with POROs (@DrSayre)
25+
- [#1730](https://github.com/rails-api/active_model_serializers/pull/1730) Adds documentation for overriding default serializer based on conditions (@groyoh/@cgmckeever)
2526

2627
### [v0.10.0.rc5 (2016-04-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc4...v0.10.0.rc5)
2728

docs/general/serializers.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,19 @@ class PostSerializer < ActiveModel::Serializer
370370
end
371371
end
372372
```
373+
374+
## Overriding association serializer lookup
375+
376+
If you want to define a specific serializer lookup for your associations, you can override
377+
the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions.
378+
379+
```ruby
380+
class MySerializer < ActiveModel::Serializer
381+
def self.serializer_for(model, options)
382+
return SparseAdminSerializer if model.class == 'Admin'
383+
super
384+
end
385+
386+
# the rest of the serializer
387+
end
388+
```

0 commit comments

Comments
 (0)