Skip to content

Commit

Permalink
Remove nil relationships links
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Robert committed Jul 12, 2016
1 parent 91b37ce commit 0f53e6c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Features:

Fixes:

- [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh)

Misc:

### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def initialize(parent_serializer, serializer, serializable_resource_options, arg
@serializable_resource_options = serializable_resource_options
@data = data_for(serializer)
@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
meta = args.fetch(:meta, nil)
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
Expand Down
11 changes: 10 additions & 1 deletion test/adapter/json_api/relationships_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class RelationshipAuthorSerializer < ActiveModel::Serializer
end

has_one :profile do
id = object.profile.id
link :related do
"//example.com/profiles/#{object.profile.id}"
"//example.com/profiles/#{id}" if id != 123
end
end

Expand Down Expand Up @@ -113,6 +114,14 @@ def test_relationship_block_link
assert_relationship(:profile, expected)
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
expected = {
data: [{ id: '1337', type: 'locations' }],
Expand Down

0 comments on commit 0f53e6c

Please sign in to comment.