Skip to content

Commit b9f4720

Browse files
committed
Merge pull request #1406 from beauby/dynamic-jsonapi-string-links
Add support for custom dynamic valued links in JsonApi adapter.
2 parents 316026e + 30d8414 commit b9f4720

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Breaking changes:
1616

1717
Features:
1818

19+
- [#1406](https://github.com/rails-api/active_model_serializers/pull/1406) Allow for custom dynamic values in JSON API links (@beauby)
1920
- [#1270](https://github.com/rails-api/active_model_serializers/pull/1270) Adds `assert_response_schema` test helper (@maurogeorge)
2021
- [#1099](https://github.com/rails-api/active_model_serializers/pull/1099) Adds `assert_serializer` test helper (@maurogeorge)
2122
- [#1403](https://github.com/rails-api/active_model_serializers/pull/1403) Add support for if/unless on attributes/associations (@beauby)

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,7 @@ def add_included_resources_for(serializer, include_tree, primary_data, included)
210210

211211
def links_for(serializer)
212212
serializer._links.each_with_object({}) do |(name, value), hash|
213-
hash[name] =
214-
if value.respond_to?(:call)
215-
link = Link.new(serializer)
216-
link.instance_eval(&value)
217-
218-
link.to_hash
219-
else
220-
value
221-
end
213+
hash[name] = Link.new(serializer, value).as_json
222214
end
223215
end
224216

lib/active_model/serializer/adapter/json_api/link.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,39 @@ class Serializer
33
module Adapter
44
class JsonApi
55
class Link
6-
def initialize(serializer)
6+
def initialize(serializer, value)
77
@object = serializer.object
88
@scope = serializer.scope
9+
10+
# Use the return value of the block unless it is nil.
11+
if value.respond_to?(:call)
12+
@value = instance_eval(&value)
13+
else
14+
@value = value
15+
end
916
end
1017

1118
def href(value)
12-
self._href = value
19+
@href = value
20+
nil
1321
end
1422

1523
def meta(value)
16-
self._meta = value
24+
@meta = value
25+
nil
1726
end
1827

19-
def to_hash
20-
hash = { href: _href }
21-
hash.merge!(meta: _meta) if _meta
28+
def as_json
29+
return @value if @value
30+
31+
hash = { href: @href }
32+
hash.merge!(meta: @meta) if @meta
2233

2334
hash
2435
end
2536

2637
protected
2738

28-
attr_accessor :_href, :_meta
2939
attr_reader :object, :scope
3040
end
3141
end

test/adapter/json_api/links_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class LinkAuthorSerializer < ActiveModel::Serializer
1313
end
1414

1515
link :other, '//example.com/resource'
16+
17+
link :yet_another do
18+
"//example.com/resource/#{object.id}"
19+
end
1620
end
1721

1822
def setup
@@ -52,7 +56,8 @@ def test_resource_links
5256
stuff: 'value'
5357
}
5458
},
55-
other: '//example.com/resource'
59+
other: '//example.com/resource',
60+
yet_another: '//example.com/resource/1337'
5661
}
5762
assert_equal(expected, hash[:data][:links])
5863
end

0 commit comments

Comments
 (0)