-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for top-level links objects #1018
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ tmp | |
.ruby-version | ||
.ruby-gemset | ||
vendor/bundle | ||
tags | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,8 @@ Features: | |
CollectionSerializer for clarity, add ActiveModelSerializers.config.collection_serializer (@bf4) | ||
- [#1295](https://github.com/rails-api/active_model_serializers/pull/1295) Add config `serializer_lookup_enabled` that, | ||
when disabled, requires serializers to explicitly specified. (@trek) | ||
- [#1247](https://github.com/rails-api/active_model_serializers/pull/1247) Add top-level links (@beauby) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, Looks like you'll have to amend the changelog to stick this at the top of features in master since rc4 is out |
||
* Add more tests and docs for top-level links (@leandrocp) | ||
|
||
Fixes: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10. | |
- [How to add pagination links](howto/add_pagination_links.md) | ||
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md) | ||
- [Testing ActiveModelSerializers](howto/test.md) | ||
- [How to add top-level links](howto/add_top_level_links.md) (```JSON-API``` only) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or better yet, just one 'add_links.md' page since they're the same thing more or less There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correction, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
## Integrations | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# How to add top-level links | ||
|
||
JsonApi supports a [links object](http://jsonapi.org/format/#document-links) to be specified at top-level, that you can specify in the `render`: | ||
|
||
```ruby | ||
links_object = { | ||
href: "http://example.com/api/posts", | ||
meta: { | ||
count: 10 | ||
} | ||
} | ||
render json: @posts, links: links_object | ||
``` | ||
|
||
That's the result: | ||
|
||
```json | ||
{ | ||
"data": [ | ||
{ | ||
"type": "posts", | ||
"id": "1", | ||
"attributes": { | ||
"title": "JSON API is awesome!", | ||
"body": "You should be using JSON API", | ||
"created": "2015-05-22T14:56:29.000Z", | ||
"updated": "2015-05-22T14:56:28.000Z" | ||
} | ||
} | ||
], | ||
"links": { | ||
"href": "http://example.com/api/posts", | ||
"meta": { | ||
"count": 10 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
This feature is specific to JsonApi, so you have to use the use the [JsonApi Adapter](https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md#jsonapi) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. link should be relative, thoughts on doing it now or in a follow-up PR? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,15 @@ def test_toplevel_links | |
assert_equal(expected, hash[:links]) | ||
end | ||
|
||
def test_nil_toplevel_links | ||
hash = ActiveModel::SerializableResource.new( | ||
@post, | ||
adapter: :json_api, | ||
links: nil | ||
).serializable_hash | ||
assert_equal(nil, hash[:links]) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we test effect with |
||
|
||
def test_resource_links | ||
hash = serializable(@author, adapter: :json_api).serializable_hash | ||
expected = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this tags file? 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joaomdmoura you don't use exuberant ctags? such win
@leandrocp You can put tags in your global gitignore if this change doesn't make it in :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 good tip, I had forgotten about global gitignore.
@joaomdmoura do you prefer to remove this line from .gitignore ? (that's ok for me)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, I just checked it, seems that I should be using it as well. No blocking.