Skip to content

Commit

Permalink
Include 'linked' member for json-api collections
Browse files Browse the repository at this point in the history
  • Loading branch information
ggordon committed Oct 23, 2014
1 parent 47deb87 commit 46b7580
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ test/tmp
test/version_tmp
tmp
*.swp
.ruby-version
.ruby-version
36 changes: 24 additions & 12 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ class JsonApi < Adapter
def initialize(serializer, options = {})
super
serializer.root = true
@hash = {}
@top = options.fetch(:top) { @hash }
end

def serializable_hash(options = {})
@root = (options[:root] || serializer.json_key).to_s.pluralize.to_sym
@hash = {}

if serializer.respond_to?(:each)
@hash[@root] = serializer.map{|s| self.class.new(s).serializable_hash[@root] }
@hash[@root] = serializer.map do |s|
self.class.new(s, options.merge(top: @top)).serializable_hash[@root]
end
else
@hash[@root] = attributes_for_serializer(serializer, {})

serializer.each_association do |name, association, opts|
@hash[@root][:links] ||= {}
unless options[:embed] == :ids
@hash[:linked] ||= {}
end

if association.respond_to?(:each)
add_links(name, association, opts)
Expand All @@ -35,11 +35,13 @@ def serializable_hash(options = {})

def add_links(name, serializers, options)
@hash[@root][:links][name] ||= []
@hash[@root][:links][name] += serializers.map{|serializer| serializer.id.to_s }
@hash[@root][:links][name] += serializers.map { |serializer| serializer.id.to_s }

unless options[:embed] == :ids
@hash[:linked][name] ||= []
@hash[:linked][name] += serializers.map { |item| attributes_for_serializer(item, options) }
unless serializers.none? || options[:embed] == :ids
init_linked_for_resource(name)
serializers.each do |serializer|
add_linked(name, serializer, options)
end
end
end

Expand All @@ -48,19 +50,29 @@ def add_link(name, serializer, options)

unless options[:embed] == :ids
plural_name = name.to_s.pluralize.to_sym

@hash[:linked][plural_name] ||= []
@hash[:linked][plural_name].push attributes_for_serializer(serializer, options)
init_linked_for_resource(plural_name)
add_linked(plural_name, serializer, options)
end
end

def init_linked_for_resource(resource)
@top[:linked] ||= {}
@top[:linked][resource] ||= []
end

def add_linked(resource, serializer, options)
attrs = attributes_for_serializer(serializer, options)
@top[:linked][resource].push attrs unless @top[:linked][resource].include? attrs
end

private

def attributes_for_serializer(serializer, options)
attributes = serializer.attributes(options)
attributes[:id] = attributes[:id].to_s if attributes[:id]
attributes
end

end
end
end
Expand Down
17 changes: 15 additions & 2 deletions test/adapter/json_api/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ def setup

def test_include_multiple_posts
assert_equal([
{title: "Hello!!", body: "Hello, world!!", id: "1", links: {comments: []}},
{title: "New Post", body: "Body", id: "2", links: {comments: []}}
{title: "Hello!!", body: "Hello, world!!", id: "1", links: {comments: []}},
{title: "New Post", body: "Body", id: "2", links: {comments: []}}
], @adapter.serializable_hash[:posts])
refute @adapter.serializable_hash.key?(:linked)
end

def test_include_multiple_posts_and_linked
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@first_post.comments = [@first_comment, @second_comment]
assert_equal([
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: ['1', '2'] } },
{ title: "New Post", body: "Body", id: "2", links: { comments: [] } }
], @adapter.serializable_hash[:posts])
assert_equal({ :comments => [{ :id => "1", :body => "ZOMG A COMMENT" }, { :id => "2", :body => "ZOMG ANOTHER COMMENT" }] }, @adapter.serializable_hash[:linked])
end

end
end
end
Expand Down

0 comments on commit 46b7580

Please sign in to comment.