Merge pull request #1981 from groyoh/link_doc

Fix relationship links doc
This commit is contained in:
Benjamin Fleischer 2017-01-09 23:57:37 -06:00 committed by GitHub
commit 2a6d373cb2
2 changed files with 25 additions and 21 deletions

View File

@ -14,6 +14,7 @@ Fixes:
Misc: Misc:
- [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Fix relationship link documentation. (@groyoh)
- [#1984](https://github.com/rails-api/active_model_serializers/pull/1984) Make test attributes explicit. Test models have 'associations' support. (@bf4) - [#1984](https://github.com/rails-api/active_model_serializers/pull/1984) Make test attributes explicit. Test models have 'associations' support. (@bf4)
### [v0.10.4 (2017-01-06)](https://github.com/rails-api/active_model_serializers/compare/v0.10.3...v0.10.4) ### [v0.10.4 (2017-01-06)](https://github.com/rails-api/active_model_serializers/compare/v0.10.3...v0.10.4)

View File

@ -15,40 +15,42 @@ class Api::V1::UsersController < ApplicationController
serializer: Api::V1::UserSerializer, serializer: Api::V1::UserSerializer,
include: [] include: []
end end
end
``` ```
Bear in mind though that ActiveModelSerializers are [framework-agnostic](outside_controller_use.md), Rails is just a common example here. Bear in mind though that ActiveModelSerializers are [framework-agnostic](outside_controller_use.md), Rails is just a common example here.
### Links as an attribute of a resource ### Links as an attribute of a resource
**This is applicable to JSONAPI, JSON and Attributes adapters** **This is applicable to JSON and Attributes adapters**
You can define an attribute in the resource, named `links`. You can define an attribute in the resource, named `links`.
```ruby ```ruby
class Api::V1::UserSerializer < ActiveModel::Serializer class Api::V1::UserSerializer < ActiveModel::Serializer
attributes :id, :name, :links include Rails.application.routes.url_helpers
def links attributes :id, :name
attribute :links do
id = object.id
{ {
self: api_v1_user_path(object.id), self: api_v1_user_path(id),
microposts: api_v1_microposts_path(user_id: object.id) microposts: api_v1_microposts_path(user_id: id)
} }
end end
end end
``` ```
This will result in (example is in JSONAPI adapter): Using the `JSON` adapter, this will result in:
```json ```json
{ {
"data": { "user": {
"id": "1", "id": "1",
"type": "users", "name": "John",
"attributes": { "links": {
"name": "Example User", "self": "/api/v1/users/1",
"links": { "microposts": "/api/v1/microposts?user_id=1"
"self": "/api/v1/users/1",
"microposts": "/api/v1/microposts?user_id=1"
}
} }
} }
} }
@ -58,7 +60,7 @@ This will result in (example is in JSONAPI adapter):
### Links as a property of the resource definiton ### Links as a property of the resource definiton
**This is only applicable to JSONAPI adapter** **This is only applicable to JSONAPI adapter**
You can use the `links` class method to define the links you need in the resource's primary data. You can use the `link` class method to define the links you need in the resource's primary data.
```ruby ```ruby
class Api::V1::UserSerializer < ActiveModel::Serializer class Api::V1::UserSerializer < ActiveModel::Serializer
@ -69,7 +71,8 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
end end
``` ```
This will result in (example is in JSONAPI adapter): Using the `JSONAPI` adapter, this will result in:
```json ```json
{ {
"data": { "data": {
@ -104,12 +107,12 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
has_many :microposts, serializer: Api::V1::MicropostSerializer do has_many :microposts, serializer: Api::V1::MicropostSerializer do
link(:related) { api_v1_microposts_path(user_id: object.id) } link(:related) { api_v1_microposts_path(user_id: object.id) }
end
#this is needed to avoid n+1, gem core devs are working to remove this necessity microposts = object.microposts
#more on: https://github.com/rails-api/active_model_serializers/issues/1325 # The following code is needed to avoid n+1 queries.
def microposts # Core devs are working to remove this necessity.
object.microposts.loaded ? object.microposts : object.microposts.none # See: https://github.com/rails-api/active_model_serializers/issues/1325
microposts.loaded? ? microposts : microposts.none
end end
end end
``` ```