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

Included resource fixes #858

Merged
merged 2 commits into from
Mar 25, 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
17 changes: 11 additions & 6 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class JsonApi < Adapter
def initialize(serializer, options = {})
super
serializer.root = true
@hash = {}
@top = @options.fetch(:top) { @hash }
@hash = { data: [] }

if fields = options.delete(:fields)
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
Expand All @@ -17,8 +16,14 @@ def initialize(serializer, options = {})

def serializable_hash(options = {})
if serializer.respond_to?(:each)
@hash[:data] = serializer.map do |s|
self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[:data]
serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash
@hash[:data] << result[:data]

if result[:included]
@hash[:included] ||= []
@hash[:included] |= result[:included]
end
end
else
@hash = cached_object do
Expand Down Expand Up @@ -53,14 +58,14 @@ def add_included(resource_name, serializers, parent = nil)
resource_path = [parent, resource_name].compact.join('.')

if include_assoc?(resource_path)
@top[:included] ||= []
@hash[:included] ||= []

serializers.each do |serializer|
attrs = attributes_for_serializer(serializer, @options)

add_resource_links(attrs, serializer, add_included: false)

@top[:included].push(attrs) unless @top[:included].include?(attrs)
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
end
end

Expand Down
142 changes: 86 additions & 56 deletions test/adapter/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setup
@bio2.author = @author2
end

def test_include_multiple_posts_and_linked
def test_include_multiple_posts_and_linked_array
serializer = ArraySerializer.new([@first_post, @second_post])
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
serializer,
Expand All @@ -51,80 +51,80 @@ def test_include_multiple_posts_and_linked
)

expected = {
linked: {
comments: [
{
id: "1",
body: "ZOMG A COMMENT",
links: {
post: { linkage: { type: "posts", id: "1" } },
author: { linkage: nil }
}
}, {
id: "2",
body: "ZOMG ANOTHER COMMENT",
links: {
post: { linkage: { type: "posts", id: "1" } },
author: { linkage: nil }
}
}
],
authors: [
{
id: "1",
name: "Steve K.",
links: {
posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "1" } }
}
}, {
id: "2",
name: "Tenderlove",
links: {
posts: { linkage: [ { type: "posts", id:"2" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "2" } }
}
}
],
bios: [
{
id: "1",
content: "AMS Contributor",
links: {
author: { linkage: { type: "authors", id: "1" } }
}
}, {
id: "2",
content: "Rails Contributor",
links: {
author: { linkage: { type: "authors", id: "2" } }
}
}
]
},
posts: [
data: [
{
id: "10",
title: "Hello!!",
body: "Hello, world!!",
type: "posts",
links: {
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
blog: { linkage: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } }
}
},
{
id: "2",
id: "20",
title: "New Post",
body: "Body",
type: "posts",
links: {
comments: { linkage: [] },
blog: { linkage: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "2" } }
}
}
],
included: [
{
id: "1",
body: "ZOMG A COMMENT",
type: "comments",
links: {
post: { linkage: { type: "posts", id: "10" } },
author: { linkage: nil }
}
}, {
id: "2",
body: "ZOMG ANOTHER COMMENT",
type: "comments",
links: {
post: { linkage: { type: "posts", id: "10" } },
author: { linkage: nil }
}
}, {
id: "1",
name: "Steve K.",
type: "authors",
links: {
posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "1" } }
}
}, {
id: "1",
content: "AMS Contributor",
type: "bios",
links: {
author: { linkage: { type: "authors", id: "1" } }
}
}, {
id: "2",
name: "Tenderlove",
type: "authors",
links: {
posts: { linkage: [ { type: "posts", id:"20" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "2" } }
}
}, {
id: "2",
content: "Rails Contributor",
type: "bios",
links: {
author: { linkage: { type: "authors", id: "2" } }
}
}
]
}
assert_equal expected, adapter.serializable_hash
Expand Down Expand Up @@ -195,6 +195,36 @@ def test_ignore_model_namespace_for_linked_resource_type
}
assert_equal expected, links
end

def test_multiple_references_to_same_resource
serializer = ArraySerializer.new([@first_comment, @second_comment])
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
serializer,
include: ['post']
)

expected = [
{
id: "10",
title: "Hello!!",
body: "Hello, world!!",
type: "posts",
links: {
comments: {
linkage: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
},
blog: {
linkage: {type: "blogs", id: "999"}
},
author: {
linkage: {type: "authors", id: "1"}
}
}
}
]

assert_equal expected, adapter.serializable_hash[:included]
end
end
end
end
Expand Down