Fix bug displaying nil for relationship link href

When using the relationship link with a block, calling "meta" without "href", e.g.
has_one :bio do
  link :related do
    meta id: 1
  end
end

results in in a nil "href", e.g.
{ links: { posts: { related: { href: nil, meta: { id: 1 } }  } } }.
According to JSONAPI, we should be able to use meta without href
(http://jsonapi.org/format/#document-links).
This commit is contained in:
Yohan Robert 2016-02-10 21:38:30 +01:00
parent 2b673634d9
commit 3cbc0541c1
3 changed files with 45 additions and 23 deletions

View File

@ -14,6 +14,8 @@ Features:
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby) - [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
Fixes: Fixes:
- [#1516](https://github.com/rails-api/active_model_serializers/pull/1501) No longer return a nil href when only
adding meta to a relationship link. (@groyoh)
- [#1458](https://github.com/rails-api/active_model_serializers/pull/1458) Preserve the serializer - [#1458](https://github.com/rails-api/active_model_serializers/pull/1458) Preserve the serializer
type when fragment caching. (@bdmac) type when fragment caching. (@bdmac)
- [#1477](https://github.com/rails-api/active_model_serializers/pull/1477) Fix `fragment_cached?` - [#1477](https://github.com/rails-api/active_model_serializers/pull/1477) Fix `fragment_cached?`

View File

@ -28,8 +28,9 @@ module ActiveModel
def as_json def as_json
return @value if @value return @value if @value
hash = { href: @href } hash = {}
hash.merge!(meta: @meta) if @meta hash[:href] = @href if @href
hash[:meta] = @meta if @meta
hash hash
end end

View File

@ -19,19 +19,25 @@ module ActiveModel
has_many :locations do has_many :locations do
link :related do link :related do
ids = object.locations.map!(&:id).join(',') ids = object.locations.map(&:id).join(',')
href "//example.com/locations/#{ids}" href "//example.com/locations/#{ids}"
end end
end end
has_many :posts do has_many :posts do
link :related do link :related do
ids = object.posts.map!(&:id).join(',') ids = object.posts.map(&:id).join(',')
href "//example.com/posts/#{ids}" href "//example.com/posts/#{ids}"
meta ids: ids meta ids: ids
end end
end end
has_many :comments do
link :self do
meta ids: [1]
end
end
has_many :roles do has_many :roles do
meta count: object.posts.count meta count: object.posts.count
end end
@ -48,7 +54,7 @@ module ActiveModel
has_many :likes do has_many :likes do
link :related do link :related do
ids = object.likes.map!(&:id).join(',') ids = object.likes.map(&:id).join(',')
href "//example.com/likes/#{ids}" href "//example.com/likes/#{ids}"
meta ids: ids meta ids: ids
end end
@ -65,6 +71,7 @@ module ActiveModel
@profile = Profile.new(id: 1337) @profile = Profile.new(id: 1337)
@location = Location.new(id: 1337) @location = Location.new(id: 1337)
@reviewer = Author.new(id: 1337) @reviewer = Author.new(id: 1337)
@comment = Comment.new(id: 1337)
@author = RelationshipAuthor.new( @author = RelationshipAuthor.new(
id: 1337, id: 1337,
posts: [@post], posts: [@post],
@ -74,12 +81,12 @@ module ActiveModel
likes: [@like], likes: [@like],
roles: [@role], roles: [@role],
locations: [@location], locations: [@location],
profile: @profile profile: @profile,
comments: [@comment]
) )
end end
def test_relationship_simple_link def test_relationship_simple_link
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
data: { data: {
id: '1337', id: '1337',
@ -89,31 +96,28 @@ module ActiveModel
self: '//example.com/link_author/relationships/bio' self: '//example.com/link_author/relationships/bio'
} }
} }
assert_equal(expected, hash[:data][:relationships][:bio]) assert_relationship(:bio, expected)
end end
def test_relationship_block_link def test_relationship_block_link
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
data: { id: '1337', type: 'profiles' }, data: { id: '1337', type: 'profiles' },
links: { related: '//example.com/profiles/1337' } links: { related: '//example.com/profiles/1337' }
} }
assert_equal(expected, hash[:data][:relationships][:profile]) assert_relationship(:profile, expected)
end end
def test_relationship_block_link_href def test_relationship_block_link_href
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
data: [{ id: '1337', type: 'locations' }], data: [{ id: '1337', type: 'locations' }],
links: { links: {
related: { href: '//example.com/locations/1337' } related: { href: '//example.com/locations/1337' }
} }
} }
assert_equal(expected, hash[:data][:relationships][:locations]) assert_relationship(:locations, expected)
end end
def test_relationship_block_link_meta def test_relationship_block_link_href_and_meta
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
data: [{ id: '1337', type: 'posts' }], data: [{ id: '1337', type: 'posts' }],
links: { links: {
@ -123,37 +127,45 @@ module ActiveModel
} }
} }
} }
assert_equal(expected, hash[:data][:relationships][:posts]) assert_relationship(:posts, expected)
end
def test_relationship_block_link_meta
expected = {
data: [{ id: '1337', type: 'comments' }],
links: {
self: {
meta: { ids: [1] }
}
}
}
assert_relationship(:comments, expected)
end end
def test_relationship_meta def test_relationship_meta
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
data: [{ id: '1337', type: 'roles' }], data: [{ id: '1337', type: 'roles' }],
meta: { count: 1 } meta: { count: 1 }
} }
assert_equal(expected, hash[:data][:relationships][:roles]) assert_relationship(:roles, expected)
end end
def test_relationship_not_including_data def test_relationship_not_including_data
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
links: { self: '//example.com/link_author/relationships/blog' } links: { self: '//example.com/link_author/relationships/blog' }
} }
assert_equal(expected, hash[:data][:relationships][:blog]) assert_relationship(:blog, expected)
end end
def test_relationship_including_data_explicit def test_relationship_including_data_explicit
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
data: { id: '1337', type: 'authors' }, data: { id: '1337', type: 'authors' },
meta: { name: 'Dan Brown' } meta: { name: 'Dan Brown' }
} }
assert_equal(expected, hash[:data][:relationships][:reviewer]) assert_relationship(:reviewer, expected)
end end
def test_relationship_with_everything def test_relationship_with_everything
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = { expected = {
data: [{ id: '1337', type: 'likes' }], data: [{ id: '1337', type: 'likes' }],
links: { links: {
@ -164,7 +176,14 @@ module ActiveModel
}, },
meta: { liked: true } meta: { liked: true }
} }
assert_equal(expected, hash[:data][:relationships][:likes]) assert_relationship(:likes, expected)
end
private
def assert_relationship(relationship_name, expected)
hash = serializable(@author, adapter: :json_api).serializable_hash
assert_equal(expected, hash[:data][:relationships][relationship_name])
end end
end end
end end