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

View File

@@ -32,13 +32,13 @@ module ActiveModel
case key
when :posts
assert_equal({}, options)
assert_equal({ include_data: true }, options)
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
when :bio
assert_equal({}, options)
assert_equal({ include_data: true }, options)
assert_nil serializer
when :roles
assert_equal({}, options)
assert_equal({ include_data: true }, options)
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
else
flunk "Unknown association: #{key}"
@@ -80,7 +80,7 @@ module ActiveModel
flunk "Unknown association: #{key}"
end
assert_equal({}, association.options)
assert_equal({ include_data: true }, association.options)
end
end