Skip to content

Commit af304f1

Browse files
committed
support passing links hash to a relationship and more tests
1 parent b7019e0 commit af304f1

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,19 @@ def relationship_value_for(serializer, options = {})
177177
end
178178

179179
def relationship_links_for(name, serializer, options = {})
180-
{ related: url_helper.url_for([serializer.object, name]) }
180+
if options[:links] == true
181+
{ related: url_helper.url_for([serializer.object, name]) }
182+
else
183+
options[:links].inject({}) do |hash, (key, link)|
184+
hash[key] = if link.is_a? Proc
185+
link.call(serializer.object, name)
186+
else
187+
link
188+
end
189+
190+
hash
191+
end
192+
end
181193
end
182194

183195
def relationships_for(serializer)

test/adapter/json_api/has_many_url_test.rb

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,38 @@
22

33
module ActionController
44
module Serialization
5-
class JsonApiHasManyUrlTest < ActionController::TestCase
5+
class JsonApiHasManyUrlNoDataLinksTest < ActionController::TestCase
6+
class MyController < ActionController::Base
7+
8+
def render_resource_with_url_association
9+
@tag = Tag.new(id: 1)
10+
@tag.posts = []
11+
render json: @tag, adapter: :json_api, serializer: LinkNoDataTagSerializer
12+
end
13+
end
14+
15+
tests MyController
16+
17+
def test_render_resource_with_url_association
18+
get :render_resource_with_url_association
19+
expected = {
20+
data: {
21+
id: "1",
22+
type: "tags",
23+
relationships: {
24+
posts: {
25+
links: {
26+
related: "http://test.host/tags/1/posts"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
assert_equal expected.to_json, response.body
33+
end
34+
end
35+
36+
class JsonApiHasManyUrlDataLinksTest < ActionController::TestCase
637
class MyController < ActionController::Base
738

839
def render_resource_with_url_association
@@ -22,6 +53,7 @@ def test_render_resource_with_url_association
2253
type: "tags",
2354
relationships: {
2455
posts: {
56+
data: [],
2557
links: {
2658
related: "http://test.host/tags/1/posts"
2759
}
@@ -32,5 +64,67 @@ def test_render_resource_with_url_association
3264
assert_equal expected.to_json, response.body
3365
end
3466
end
67+
68+
class JsonApiHasManyUrlLinkTest < ActionController::TestCase
69+
class MyController < ActionController::Base
70+
71+
def render_resource_with_url_association
72+
@tag = Tag.new(id: 1)
73+
@tag.posts = []
74+
render json: @tag, adapter: :json_api, serializer: LinkUrlTagSerializer
75+
end
76+
end
77+
78+
tests MyController
79+
80+
def test_render_resource_with_url_association
81+
get :render_resource_with_url_association
82+
expected = {
83+
data: {
84+
id: "1",
85+
type: "tags",
86+
relationships: {
87+
posts: {
88+
links: {
89+
related: "http://test.host/post_tags"
90+
}
91+
}
92+
}
93+
}
94+
}
95+
assert_equal expected.to_json, response.body
96+
end
97+
end
98+
99+
class JsonApiHasManyUrlLinkProcTest < ActionController::TestCase
100+
class MyController < ActionController::Base
101+
102+
def render_resource_with_url_association
103+
@tag = Tag.new(id: 1)
104+
@tag.posts = []
105+
render json: @tag, adapter: :json_api, serializer: LinkProcTagSerializer
106+
end
107+
end
108+
109+
tests MyController
110+
111+
def test_render_resource_with_url_association
112+
get :render_resource_with_url_association
113+
expected = {
114+
data: {
115+
id: "1",
116+
type: "tags",
117+
relationships: {
118+
posts: {
119+
links: {
120+
related: "http://test.host/tags/1/posts/all"
121+
}
122+
}
123+
}
124+
}
125+
}
126+
assert_equal expected.to_json, response.body
127+
end
128+
end
35129
end
36-
end
130+
end

test/fixtures/poro.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,21 @@ def self.root_name
253253
end
254254

255255
LinkTagSerializer = Class.new(ActiveModel::Serializer) do
256+
has_many :posts, links: true
257+
end
258+
259+
LinkNoDataTagSerializer = Class.new(ActiveModel::Serializer) do
256260
has_many :posts, links: true, data: false
257261
end
258262

263+
LinkUrlTagSerializer = Class.new(ActiveModel::Serializer) do
264+
has_many :posts, links: { related: 'http://test.host/post_tags' }, data: false
265+
end
266+
267+
LinkProcTagSerializer = Class.new(ActiveModel::Serializer) do
268+
has_many :posts, links: { related: -> (object, name) { "http://test.host/tags/#{object.id}/#{name}/all" } }, data: false
269+
end
270+
259271
VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
260272
attributes :id
261273

0 commit comments

Comments
 (0)