mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add support for relationship-level links and meta.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user