docs/general/serializers.md in active_model_serializers-0.10.2 vs docs/general/serializers.md in active_model_serializers-0.10.3

- old
+ new

@@ -138,11 +138,11 @@ class PictureSerializer < ActiveModel::Serializer has_one :imageable end ``` -For more context, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter. +You can specify the serializers by [overriding serializer_for](serializers.md#overriding-association-serializer-lookup). For more context about polymorphic relationships, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter. ### Caching #### ::cache @@ -171,36 +171,56 @@ ### Other #### ::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 ``` With the `:json_api` adapter, the previous serializers would be rendered as: ``` json { "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" + } +} +``` + #### ::link ```ruby link :self do href "https://example.com/link_author/#{object.id}" @@ -215,10 +235,20 @@ The object being serialized. #### #root -PR please :) +Resource root which is included in `JSON` adapter. As you can see at [Adapters Document](adapters.md), `Attribute` adapter (default) and `JSON API` adapter does not include root at top level. +By default, the resource root comes from the `model_name` of the serialized object's class. + +There are several ways to specify root: +* [Overriding the root key](rendering.md#overriding-the-root-key) +* [Setting `type`](serializers.md#type) +* Specifying the `root` option, e.g. `root: 'specific_name'`, during the serializer's initialization: + +```ruby +ActiveModelSerializers::SerializableResource.new(foo, root: 'bar') +``` #### #scope Allows you to include in the serializer access to an external method.