From f25071ca70fc25320c3b933307d5b5c4094675ae Mon Sep 17 00:00:00 2001 From: Hugo Almeida Date: Mon, 29 Jun 2015 10:48:12 +0900 Subject: [PATCH] Fixes virtual value not being used --- .../serializer/adapter/json_api.rb | 2 +- test/adapter/json_api/has_many_test.rb | 20 ++++++++++++++++++- test/adapter/json_api/has_one_test.rb | 20 +++++++++++++++++++ test/fixtures/poro.rb | 14 +++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index c99a20f41..c72d822dd 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -49,7 +49,7 @@ def add_relationships(resource, name, serializers) def add_relationship(resource, name, serializer, val=nil) resource[:relationships] ||= {} - resource[:relationships][name] = { data: nil } + resource[:relationships][name] = { data: val } if serializer && serializer.object resource[:relationships][name][:data] = { type: serializer.type, id: serializer.id.to_s } diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index 3cef3ef8b..5b14dda7e 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -31,6 +31,8 @@ def setup @post.tags = [@tag] @serializer = PostSerializer.new(@post) @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) + + @virtual_value = VirtualValue.new(id: 1) end def test_includes_comment_ids @@ -115,7 +117,23 @@ def test_has_many_with_no_serializer id: "1", type: "posts", relationships: { - tags: { data: nil } + tags: { data: [@tag.as_json]} + } + } + }, adapter.serializable_hash) + end + + def test_has_many_with_virtual_value + serializer = VirtualValueSerializer.new(@virtual_value) + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) + + assert_equal({ + data: { + id: "1", + type: "virtual_values", + relationships: { + maker: {data: {id: 1}}, + reviews: {data: [{id: 1}, {id: 2}]} } } }, adapter.serializable_hash) diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index 195d56820..f71977cf5 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -25,6 +25,8 @@ def setup @author.posts = [] @author.roles = [] + @virtual_value = VirtualValue.new(id: 1) + @serializer = AuthorSerializer.new(@author) @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio,posts') end @@ -54,6 +56,24 @@ def test_includes_linked_bio assert_equal(expected, @adapter.serializable_hash[:included]) end + + def test_has_one_with_virtual_value + serializer = VirtualValueSerializer.new(@virtual_value) + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) + + expected = { + data: { + id: "1", + type: "virtual_values", + relationships: { + maker: {data: {id: 1}}, + reviews: {data: [{id: 1}, {id: 2}]} + } + } + } + + assert_equal(expected, adapter.serializable_hash) + end end end end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 4f0b513a3..89603863d 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -77,6 +77,7 @@ class ProfilePreviewSerializer < ActiveModel::Serializer Location = Class.new(Model) Place = Class.new(Model) Tag = Class.new(Model) +VirtualValue = Class.new(Model) Comment = Class.new(Model) do # Uses a custom non-time-based cache key def cache_key @@ -231,6 +232,19 @@ def self.root_name has_many :tags end +VirtualValueSerializer = Class.new(ActiveModel::Serializer) do + attributes :id + + has_many :reviews, virtual_value: [{id: 1}, {id: 2}] + has_one :maker, virtual_value: {id: 1} + + def reviews + end + + def maker + end +end + Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do attributes :id end