mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #1655 from liveeditor/applicationserializer_docs
[DOC] Document generator's auto-extending of ApplicationSerializer (if it exists)
This commit is contained in:
commit
d30aa4c44f
@ -80,6 +80,32 @@ end
|
||||
|
||||
ActiveModelSerializers will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`).
|
||||
|
||||
### Extending a Base `ApplicationSerializer`
|
||||
|
||||
By default, new serializers descend from `ActiveModel::Serializer`. However, if
|
||||
you wish to share behavior across your serializers, you can create an
|
||||
`ApplicationSerializer` at `app/serializers/application_serializer.rb`:
|
||||
|
||||
```ruby
|
||||
class ApplicationSerializer < ActiveModel::Serializer
|
||||
end
|
||||
```
|
||||
|
||||
Then any newly-generated serializers will automatically descend from
|
||||
`ApplicationSerializer`.
|
||||
|
||||
```
|
||||
$ rails g serializer post
|
||||
```
|
||||
|
||||
Now generates:
|
||||
|
||||
```ruby
|
||||
class PostSerializer < ApplicationSerializer
|
||||
attributes :id
|
||||
end
|
||||
````
|
||||
|
||||
## Rails Integration
|
||||
|
||||
ActiveModelSerializers will automatically integrate with your Rails app,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user