Add support for relationship-level links and meta.

This commit is contained in:
Lucas Hosseini
2016-01-21 01:10:02 +01:00
parent 20ddc5e102
commit 061f1c0f59
7 changed files with 175 additions and 50 deletions

View File

@@ -17,11 +17,23 @@ module ActiveModel
link :yet_another do
"//example.com/resource/#{object.id}"
end
has_many :posts do
link :self do
href '//example.com/link_author/relationships/posts'
meta stuff: 'value'
end
link :related do
href '//example.com/link_author/posts'
meta count: object.posts.count
end
include_data false
end
end
def setup
@post = Post.new(id: 1337, comments: [], author: nil)
@author = LinkAuthor.new(id: 1337)
@author = LinkAuthor.new(id: 1337, posts: [@post])
end
def test_toplevel_links
@@ -61,6 +73,23 @@ module ActiveModel
}
assert_equal(expected, hash[:data][:links])
end
def test_relationship_links
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = {
links: {
self: {
href: '//example.com/link_author/relationships/posts',
meta: { stuff: 'value' }
},
related: {
href: '//example.com/link_author/posts',
meta: { count: 1 }
}
}
}
assert_equal(expected, hash[:data][:relationships][:posts])
end
end
end
end