Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic string-links in JsonApi adapter. #1406

Merged
merged 1 commit into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Breaking changes:

Features:

- [#1406](https://github.com/rails-api/active_model_serializers/pull/1406) Allow for custom dynamic values in JSON API links (@beauby)
- [#1270](https://github.com/rails-api/active_model_serializers/pull/1270) Adds `assert_response_schema` test helper (@maurogeorge)
- [#1099](https://github.com/rails-api/active_model_serializers/pull/1099) Adds `assert_serializer` test helper (@maurogeorge)
- [#1403](https://github.com/rails-api/active_model_serializers/pull/1403) Add support for if/unless on attributes/associations (@beauby)
Expand Down
10 changes: 1 addition & 9 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,7 @@ def add_included_resources_for(serializer, include_tree, primary_data, included)

def links_for(serializer)
serializer._links.each_with_object({}) do |(name, value), hash|
hash[name] =
if value.respond_to?(:call)
link = Link.new(serializer)
link.instance_eval(&value)

link.to_hash
else
value
end
hash[name] = Link.new(serializer, value).as_json
end
end

Expand Down
24 changes: 17 additions & 7 deletions lib/active_model/serializer/adapter/json_api/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@ class Serializer
module Adapter
class JsonApi
class Link
def initialize(serializer)
def initialize(serializer, value)
@object = serializer.object
@scope = serializer.scope

# Use the return value of the block unless it is nil.
if value.respond_to?(:call)
@value = instance_eval(&value)
else
@value = value
end
end

def href(value)
self._href = value
@href = value
nil
end

def meta(value)
self._meta = value
@meta = value
nil
end

def to_hash
hash = { href: _href }
hash.merge!(meta: _meta) if _meta
def as_json
return @value if @value

hash = { href: @href }
hash.merge!(meta: @meta) if @meta

hash
end

protected

attr_accessor :_href, :_meta
attr_reader :object, :scope
end
end
Expand Down
7 changes: 6 additions & 1 deletion test/adapter/json_api/links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class LinkAuthorSerializer < ActiveModel::Serializer
end

link :other, '//example.com/resource'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off topic: given that JSON API is undecided on relative links, I'd rather not fill our examples with them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, however this is not a relative link. //resource would be undefined, but //example.com/resource is a valid url.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beauby words... words.. I meant 'protocol relative'. Sorry I typo'ed again :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about this, although it is not clear from the discussion whether they are disallowing relative urls or protocol-relative urls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just updated my question to clarify that


link :yet_another do
"//example.com/resource/#{object.id}"
end
end

def setup
Expand Down Expand Up @@ -52,7 +56,8 @@ def test_resource_links
stuff: 'value'
}
},
other: '//example.com/resource'
other: '//example.com/resource',
yet_another: '//example.com/resource/1337'
}
assert_equal(expected, hash[:data][:links])
end
Expand Down