Skip to content

Commit

Permalink
Merge pull request #1247 from beauby/jsonapi-toplevel-links
Browse files Browse the repository at this point in the history
Add support for toplevel JSON API links.
  • Loading branch information
joaomdmoura committed Oct 10, 2015
2 parents 6018ef1 + 54303b6 commit da7e6dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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])

# 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

0 comments on commit da7e6dc

Please sign in to comment.