Skip to content

Commit b709cd4

Browse files
yukideluxeYohan Robert
authored andcommitted
Improve type method documentation (#1967)
1 parent ce8dd36 commit b709cd4

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Misc:
3535
- [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@ScottKbka)
3636
- [#1909](https://github.com/rails-api/active_model_serializers/pull/1909) Add documentation for relationship links. (@vasilakisfil, @NullVoxPopuli)
3737
- [#1959](https://github.com/rails-api/active_model_serializers/pull/1959) Add documentation for root. (@shunsuke227ono)
38+
- [#1967](https://github.com/rails-api/active_model_serializers/pull/1967) Improve type method documentation. (@yukideluxe)
3839

3940
### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
4041

docs/general/serializers.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,25 @@ end
173173

174174
#### ::type
175175

176-
The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
176+
When using the `:json_api` adapter, the `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
177+
178+
When using the `:json` adapter, the `::type` method defines the name of the root element.
179+
177180
It either takes a `String` or `Symbol` as parameter.
178181

179-
Note: This method is useful only when using the `:json_api` adapter.
182+
Note: This method is useful only when using the `:json_api` or `:json` adapter.
180183

181184
Examples:
182185
```ruby
183186
class UserProfileSerializer < ActiveModel::Serializer
184187
type 'profile'
188+
189+
attribute :name
185190
end
186191
class AuthorProfileSerializer < ActiveModel::Serializer
187192
type :profile
193+
194+
attribute :name
188195
end
189196
```
190197

@@ -194,7 +201,20 @@ With the `:json_api` adapter, the previous serializers would be rendered as:
194201
{
195202
"data": {
196203
"id": "1",
197-
"type": "profile"
204+
"type": "profile",
205+
"attributes": {
206+
"name": "Julia"
207+
}
208+
}
209+
}
210+
```
211+
212+
With the `:json` adapter, the previous serializer would be rendered as:
213+
214+
``` json
215+
{
216+
"profile": {
217+
"name": "Julia"
198218
}
199219
}
200220
```

docs/howto/add_root_key.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ or if it returns a collection:
5151
]
5252
}
5353
```
54+
55+
[There are several ways to specify root](../general/serializers.md#root) when using the JSON adapter.

0 commit comments

Comments
 (0)