mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add support for resource-level JSON API links.
This commit is contained in:
@@ -5,8 +5,19 @@ module ActiveModel
|
||||
module Adapter
|
||||
class JsonApi
|
||||
class LinksTest < Minitest::Test
|
||||
LinkAuthor = Class.new(::Model)
|
||||
class LinkAuthorSerializer < ActiveModel::Serializer
|
||||
link :self do
|
||||
href "//example.com/link_author/#{object.id}"
|
||||
meta stuff: 'value'
|
||||
end
|
||||
|
||||
link :other, '//example.com/resource'
|
||||
end
|
||||
|
||||
def setup
|
||||
@post = Post.new(id: 1337, comments: [], author: nil)
|
||||
@author = LinkAuthor.new(id: 1337)
|
||||
end
|
||||
|
||||
def test_toplevel_links
|
||||
@@ -15,16 +26,36 @@ module ActiveModel
|
||||
adapter: :json_api,
|
||||
links: {
|
||||
self: {
|
||||
href: '//posts'
|
||||
href: '//example.com/posts',
|
||||
meta: {
|
||||
stuff: 'value'
|
||||
}
|
||||
}
|
||||
}).serializable_hash
|
||||
expected = {
|
||||
self: {
|
||||
href: '//posts'
|
||||
href: '//example.com/posts',
|
||||
meta: {
|
||||
stuff: 'value'
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_equal(expected, hash[:links])
|
||||
end
|
||||
|
||||
def test_resource_links
|
||||
hash = serializable(@author, adapter: :json_api).serializable_hash
|
||||
expected = {
|
||||
self: {
|
||||
href: '//example.com/link_author/1337',
|
||||
meta: {
|
||||
stuff: 'value'
|
||||
}
|
||||
},
|
||||
other: '//example.com/resource'
|
||||
}
|
||||
assert_equal(expected, hash[:data][:links])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user