mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Improve type method documentation (#1967)
This commit is contained in:
parent
ce8dd36633
commit
b709cd41e6
@ -35,6 +35,7 @@ Misc:
|
||||
- [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@ScottKbka)
|
||||
- [#1909](https://github.com/rails-api/active_model_serializers/pull/1909) Add documentation for relationship links. (@vasilakisfil, @NullVoxPopuli)
|
||||
- [#1959](https://github.com/rails-api/active_model_serializers/pull/1959) Add documentation for root. (@shunsuke227ono)
|
||||
- [#1967](https://github.com/rails-api/active_model_serializers/pull/1967) Improve type method documentation. (@yukideluxe)
|
||||
|
||||
### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
|
||||
|
||||
|
||||
@ -173,18 +173,25 @@ end
|
||||
|
||||
#### ::type
|
||||
|
||||
The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
|
||||
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.
|
||||
|
||||
When using the `:json` adapter, the `::type` method defines the name of the root element.
|
||||
|
||||
It either takes a `String` or `Symbol` as parameter.
|
||||
|
||||
Note: This method is useful only when using the `:json_api` adapter.
|
||||
Note: This method is useful only when using the `:json_api` or `:json` adapter.
|
||||
|
||||
Examples:
|
||||
```ruby
|
||||
class UserProfileSerializer < ActiveModel::Serializer
|
||||
type 'profile'
|
||||
|
||||
attribute :name
|
||||
end
|
||||
class AuthorProfileSerializer < ActiveModel::Serializer
|
||||
type :profile
|
||||
|
||||
attribute :name
|
||||
end
|
||||
```
|
||||
|
||||
@ -194,7 +201,20 @@ With the `:json_api` adapter, the previous serializers would be rendered as:
|
||||
{
|
||||
"data": {
|
||||
"id": "1",
|
||||
"type": "profile"
|
||||
"type": "profile",
|
||||
"attributes": {
|
||||
"name": "Julia"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
With the `:json` adapter, the previous serializer would be rendered as:
|
||||
|
||||
``` json
|
||||
{
|
||||
"profile": {
|
||||
"name": "Julia"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -51,3 +51,5 @@ or if it returns a collection:
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
[There are several ways to specify root](../general/serializers.md#root) when using the JSON adapter.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user