Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes virtual value not being used #975

Merged
merged 1 commit into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
20 changes: 19 additions & 1 deletion test/adapter/json_api/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions test/adapter/json_api/has_one_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down