diff --git a/CHANGELOG.md b/CHANGELOG.md index 48f235fc..2fba2b01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index 59e4b7f2..9925744f 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -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" } } ``` diff --git a/docs/howto/add_root_key.md b/docs/howto/add_root_key.md index c27f47f6..82a4ab6f 100644 --- a/docs/howto/add_root_key.md +++ b/docs/howto/add_root_key.md @@ -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.