Skip to content

Add support for toplevel JSON API links. #1247

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

Merged
merged 1 commit into from
Oct 10, 2015
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
2 changes: 1 addition & 1 deletion lib/active_model/serializable_resource.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'set'
module ActiveModel
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key])
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
Copy link
Member

Choose a reason for hiding this comment

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

nice! shouldn't this integrate with existing code such as pagination links?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, pagination is automagically added anyways. I'm up for any suggestion though.

Copy link
Member

Choose a reason for hiding this comment

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

but would both work well together? What do you think about adding a test covering it? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the pagination stuff just updates the links.


# Primary interface to composing a resource with a serializer and adapter.
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
Expand Down
5 changes: 5 additions & 0 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def serializable_hash(options = nil)

ApiObjects::JsonApi.add!(hash)

if instance_options[:links]
hash[:links] ||= {}
hash[:links].update(instance_options[:links])
end

hash
end

Expand Down
32 changes: 32 additions & 0 deletions test/adapter/json_api/links_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'test_helper'

module ActiveModel
class Serializer
module Adapter
class JsonApi
class LinksTest < Minitest::Test
def setup
@post = Post.new(id: 1337, comments: [], author: nil)
end

def test_toplevel_links
hash = ActiveModel::SerializableResource.new(
@post,
adapter: :json_api,
links: {
self: {
href: '//posts'
}
}).serializable_hash
expected = {
self: {
href: '//posts'
}
}
assert_equal(expected, hash[:links])
end
end
end
end
end
end