Skip to content

Commit d30aa4c

Browse files
committed
Merge pull request #1655 from liveeditor/applicationserializer_docs
[DOC] Document generator's auto-extending of ApplicationSerializer (if it exists)
2 parents 22f88ef + dba85f2 commit d30aa4c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

docs/general/getting_started.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ end
8080

8181
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`).
8282

83+
### Extending a Base `ApplicationSerializer`
84+
85+
By default, new serializers descend from `ActiveModel::Serializer`. However, if
86+
you wish to share behavior across your serializers, you can create an
87+
`ApplicationSerializer` at `app/serializers/application_serializer.rb`:
88+
89+
```ruby
90+
class ApplicationSerializer < ActiveModel::Serializer
91+
end
92+
```
93+
94+
Then any newly-generated serializers will automatically descend from
95+
`ApplicationSerializer`.
96+
97+
```
98+
$ rails g serializer post
99+
```
100+
101+
Now generates:
102+
103+
```ruby
104+
class PostSerializer < ApplicationSerializer
105+
attributes :id
106+
end
107+
````
108+
83109
## Rails Integration
84110

85111
ActiveModelSerializers will automatically integrate with your Rails app,

0 commit comments

Comments
 (0)