Skip to content

Commit da7e6dc

Browse files
committed
Merge pull request #1247 from beauby/jsonapi-toplevel-links
Add support for toplevel JSON API links.
2 parents 6018ef1 + 54303b6 commit da7e6dc

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

lib/active_model/serializable_resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'set'
22
module ActiveModel
33
class SerializableResource
4-
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key])
4+
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
55

66
# Primary interface to composing a resource with a serializer and adapter.
77
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def serializable_hash(options = nil)
6565

6666
ApiObjects::JsonApi.add!(hash)
6767

68+
if instance_options[:links]
69+
hash[:links] ||= {}
70+
hash[:links].update(instance_options[:links])
71+
end
72+
6873
hash
6974
end
7075

test/adapter/json_api/links_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
module Adapter
6+
class JsonApi
7+
class LinksTest < Minitest::Test
8+
def setup
9+
@post = Post.new(id: 1337, comments: [], author: nil)
10+
end
11+
12+
def test_toplevel_links
13+
hash = ActiveModel::SerializableResource.new(
14+
@post,
15+
adapter: :json_api,
16+
links: {
17+
self: {
18+
href: '//posts'
19+
}
20+
}).serializable_hash
21+
expected = {
22+
self: {
23+
href: '//posts'
24+
}
25+
}
26+
assert_equal(expected, hash[:links])
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)