Skip to content

Commit b6c382f

Browse files
committed
support for top-level links limited to jsonapi adapter
1 parent 516d89b commit b6c382f

File tree

5 files changed

+35
-73
lines changed

5 files changed

+35
-73
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ test/version_tmp
1919
tmp
2020
*.swp
2121
.ruby-version
22+
tags

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
* adds cache support to attributes and associations [@joaomdmoura]
1010
* uses model name to determine the type [@lsylvester]
1111
* remove root key option and split JSON adapter [@joaomdmoura]
12-
* adds FlattenJSON as default adapter [@joaomdmoura]
12+
* adds FlattenJSON as default adapter [@joaomdmoura]
13+
* adds support for `links` at top level of JsonApi adapter [@leandrocp]

lib/active_model/serializer/adapter.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ def serializable_hash(options = nil)
2222

2323
def as_json(options = nil)
2424
hash = serializable_hash(options)
25-
unless self.class == FlattenJson
26-
include_meta(hash)
27-
include_links(hash)
28-
end
25+
include_meta(hash) unless self.class == FlattenJson
2926
hash
3027
end
3128

@@ -89,10 +86,6 @@ def meta_key
8986
serializer.meta_key || "meta"
9087
end
9188

92-
def links
93-
serializer.links if serializer.respond_to?(:links)
94-
end
95-
9689
def root
9790
serializer.json_key.to_sym if serializer.json_key
9891
end
@@ -101,11 +94,6 @@ def include_meta(json)
10194
json[meta_key] = meta if meta
10295
json
10396
end
104-
105-
def include_links(json)
106-
json["links"] = links if links
107-
json
108-
end
10997
end
11098
end
11199
end

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def serializable_hash(options = nil)
3131
@hash[:data] = attributes_for_serializer(serializer, options)
3232
add_resource_relationships(@hash[:data], serializer)
3333
end
34+
@hash[:links] = attributes_for_top_level_links(serializer) if serializer.links
3435
@hash
3536
end
3637

@@ -157,6 +158,10 @@ def add_resource_relationships(attrs, serializer, options = {})
157158
end
158159
end
159160
end
161+
162+
def attributes_for_top_level_links(serializer)
163+
serializer.links
164+
end
160165
end
161166
end
162167
end

test/serializers/links_test.rb

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,84 +12,51 @@ def setup
1212
end
1313

1414
def test_links_is_present_with_root
15-
serializer = AlternateBlogSerializer.new(@blog, :links => {"self" => "/blogs/1"})
16-
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
15+
serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"})
16+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
1717
expected = {
18-
blog: {
19-
id: 1,
20-
title: "AMS Hints"
18+
data: {
19+
id: "1",
20+
type: "blogs",
21+
attributes: {
22+
title: "AMS Hints"
23+
}
2124
},
22-
"links" => {
23-
"self" => "/blogs/1"
25+
links: {
26+
self: "/blogs/1"
2427
}
2528
}
2629
assert_equal expected, adapter.as_json
2730
end
2831

29-
def test_links_is_not_included_when_root_is_missing
30-
# load_adapter uses FlattenJson Adapter
31-
adapter = load_adapter(links: {"self" => "/blogs/1"})
32+
def test_links_is_not_present_when_not_declared
33+
serializer = AlternateBlogSerializer.new(@blog)
34+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
3235
expected = {
33-
id: 1,
34-
title: "AMS Hints"
36+
data: {
37+
id: "1",
38+
type: "blogs",
39+
attributes: {
40+
title: "AMS Hints"
41+
}
42+
}
3543
}
3644
assert_equal expected, adapter.as_json
3745
end
3846

39-
def test_links_is_not_present_on_arrays_without_root
40-
serializer = ArraySerializer.new([@blog], links: {"self" => "/blogs/1"})
41-
# FlattenJSON doesn't have support to root
47+
def test_links_is_not_present_on_flattenjson_adapter
48+
serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"})
4249
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
43-
expected = [{
44-
id: 1,
45-
name: "AMS Hints",
46-
writer: {
47-
id: 2,
48-
name: "Steve"
49-
},
50-
articles: [{
51-
id: 3,
52-
title: "AMS",
53-
body: nil
54-
}]
55-
}]
50+
expected = {:id=>1, :title=>"AMS Hints"}
5651
assert_equal expected, adapter.as_json
5752
end
5853

59-
def test_links_is_present_on_arrays_with_root
60-
serializer = ArraySerializer.new([@blog], links: {"self" => "/blogs/1"})
61-
# JSON adapter adds root by default
54+
def test_links_is_not_present_on_json_adapter
55+
serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"})
6256
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
63-
expected = {
64-
blogs: [{
65-
id: 1,
66-
name: "AMS Hints",
67-
writer: {
68-
id: 2,
69-
name: "Steve"
70-
},
71-
articles: [{
72-
id: 3,
73-
title: "AMS",
74-
body: nil
75-
}]
76-
}],
77-
"links" => {
78-
"self" => "/blogs/1"
79-
}
80-
}
57+
expected = {:blog=>{:id=>1, :title=>"AMS Hints"}}
8158
assert_equal expected, adapter.as_json
8259
end
83-
84-
private
85-
86-
def load_adapter(options)
87-
adapter_opts, serializer_opts =
88-
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
89-
90-
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
91-
ActiveModel::Serializer::Adapter::FlattenJson.new(serializer, adapter_opts)
92-
end
9360
end
9461
end
9562
end

0 commit comments

Comments
 (0)