mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Remove nil relationships links (#1833)
This commit is contained in:
parent
91b37cee2c
commit
9a206a1f5d
@ -8,6 +8,8 @@ Features:
|
|||||||
|
|
||||||
Fixes:
|
Fixes:
|
||||||
|
|
||||||
|
- [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh)
|
||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
|
|
||||||
### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
|
### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
|
||||||
|
|||||||
@ -13,7 +13,8 @@ module ActiveModelSerializers
|
|||||||
@serializable_resource_options = serializable_resource_options
|
@serializable_resource_options = serializable_resource_options
|
||||||
@data = data_for(serializer)
|
@data = data_for(serializer)
|
||||||
@links = args.fetch(:links, {}).each_with_object({}) do |(key, value), hash|
|
@links = args.fetch(:links, {}).each_with_object({}) do |(key, value), hash|
|
||||||
hash[key] = ActiveModelSerializers::Adapter::JsonApi::Link.new(parent_serializer, value).as_json
|
result = Link.new(parent_serializer, value).as_json
|
||||||
|
hash[key] = result if result
|
||||||
end
|
end
|
||||||
meta = args.fetch(:meta, nil)
|
meta = args.fetch(:meta, nil)
|
||||||
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
||||||
|
|||||||
@ -13,8 +13,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
has_one :profile do
|
has_one :profile do
|
||||||
|
id = object.profile.id
|
||||||
link :related do
|
link :related do
|
||||||
"//example.com/profiles/#{object.profile.id}"
|
"//example.com/profiles/#{id}" if id != 123
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -113,6 +114,14 @@ module ActiveModel
|
|||||||
assert_relationship(:profile, expected)
|
assert_relationship(:profile, expected)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_relationship_nil_link
|
||||||
|
@author.profile.id = 123
|
||||||
|
expected = {
|
||||||
|
data: { id: '123', type: 'profiles' }
|
||||||
|
}
|
||||||
|
assert_relationship(:profile, expected)
|
||||||
|
end
|
||||||
|
|
||||||
def test_relationship_block_link_href
|
def test_relationship_block_link_href
|
||||||
expected = {
|
expected = {
|
||||||
data: [{ id: '1337', type: 'locations' }],
|
data: [{ id: '1337', type: 'locations' }],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user