Skip to content
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

Merged
merged 3 commits into from
Feb 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tmp
.ruby-version
.ruby-gemset
vendor/bundle
tags
Copy link
Member

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? 😁

Copy link
Member

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 :)

Copy link
Contributor Author

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)

Copy link
Member

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.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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:

Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, How to add pagination links should be like

- [How to add top-level links](howto/add_top_level_links.md) (```JSON-API``` only)
  - [How to add pagination links](howto/add_pagination_links.md)

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correction, add_top_level_links.md because individual objects may have links

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


## Integrations

Expand Down
40 changes: 40 additions & 0 deletions docs/howto/add_top_level_links.md
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link should be relative, ../general/adapters.md#jsonapi. Also, I'm thinking this might be better placed in the Rendering, adapter_opts, links section (which didn't previously exist :( ) https://github.com/rails-api/active_model_serializers/blob/72c2c9f0d7c665ce1b32dbbd4ca5687b1afd320e/docs/general/rendering.md#links

thoughts on doing it now or in a follow-up PR?

33 changes: 33 additions & 0 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ def render_array_using_implicit_serializer_and_meta
render json: @profiles, meta: { total: 10 }
end

def render_array_using_implicit_serializer_and_links
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
@profiles = [
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
]

render json: @profiles, links: { self: 'http://example.com/api/profiles/1' }
end
end

def render_object_with_cache_enabled
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@author = Author.new(id: 1, name: 'Joao Moura.')
Expand Down Expand Up @@ -254,6 +264,29 @@ def test_render_array_using_implicit_serializer_and_meta
assert_equal expected.to_json, @response.body
end

def test_render_array_using_implicit_serializer_and_links
get :render_array_using_implicit_serializer_and_links

expected = {
data: [
{
id: assigns(:profiles).first.id.to_s,
type: 'profiles',
attributes: {
name: 'Name 1',
description: 'Description 1'
}
}
],
links: {
self: 'http://example.com/api/profiles/1'
}
}

assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_render_with_cache_enable
expected = {
id: 1,
Expand Down
9 changes: 9 additions & 0 deletions test/adapter/json_api/links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we test effect with :json adapter? Also, I'm wondering if we should even add a links key if the value is null. Not sure per spec though. not a blocker since this pr has been around for a bit


def test_resource_links
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = {
Expand Down