mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Add support for embed: :ids option for in associations
This commit is contained in:
parent
97023db904
commit
188336522f
@ -2,12 +2,14 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class Adapter
|
class Adapter
|
||||||
class JsonApi < Adapter
|
class JsonApi < Adapter
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(opts = {})
|
||||||
@hash = serializer.attributes
|
@hash = serializer.attributes
|
||||||
|
|
||||||
serializer.each_association do |name, association, options|
|
serializer.each_association do |name, association, options|
|
||||||
@hash[:links] ||= {}
|
@hash[:links] ||= {}
|
||||||
@hash[:linked] ||= {}
|
unless options[:embed] == :ids
|
||||||
|
@hash[:linked] ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
if association.respond_to?(:each)
|
if association.respond_to?(:each)
|
||||||
add_links(name, association, options)
|
add_links(name, association, options)
|
||||||
@ -21,17 +23,23 @@ module ActiveModel
|
|||||||
|
|
||||||
def add_links(name, serializers, options)
|
def add_links(name, serializers, options)
|
||||||
@hash[:links][name] ||= []
|
@hash[:links][name] ||= []
|
||||||
@hash[:linked][name] ||= []
|
|
||||||
@hash[:links][name] += serializers.map(&:id)
|
@hash[:links][name] += serializers.map(&:id)
|
||||||
@hash[:linked][name] += serializers.map { |item| item.attributes(options) }
|
|
||||||
|
unless options[:embed] == :ids
|
||||||
|
@hash[:linked][name] ||= []
|
||||||
|
@hash[:linked][name] += serializers.map { |item| item.attributes(options) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_link(name, serializer, options)
|
def add_link(name, serializer, options)
|
||||||
plural_name = name.to_s.pluralize.to_sym
|
|
||||||
@hash[:linked][plural_name] ||= []
|
|
||||||
|
|
||||||
@hash[:links][name] = serializer.id
|
@hash[:links][name] = serializer.id
|
||||||
@hash[:linked][plural_name].push serializer.attributes(options)
|
|
||||||
|
unless options[:embed] == :ids
|
||||||
|
plural_name = name.to_s.pluralize.to_sym
|
||||||
|
|
||||||
|
@hash[:linked][plural_name] ||= []
|
||||||
|
@hash[:linked][plural_name].push serializer.attributes(options)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
31
test/adapter/json_api/has_many_embed_ids.rb
Normal file
31
test/adapter/json_api/has_many_embed_ids.rb
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class Adapter
|
||||||
|
class JsonApi
|
||||||
|
class HasManyEmbedIdsTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@author = Author.new(name: 'Steve K.')
|
||||||
|
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
||||||
|
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
||||||
|
@author.posts = [@first_post, @second_post]
|
||||||
|
@first_post.author = @author
|
||||||
|
@second_post.author = @author
|
||||||
|
|
||||||
|
@serializer = AuthorSerializer.new(@author)
|
||||||
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_includes_comment_ids
|
||||||
|
assert_equal([1, 2], @adapter.serializable_hash[:links][:posts])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_no_includes_linked_comments
|
||||||
|
assert_nil @adapter.serializable_hash[:linked]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
7
test/fixtures/poro.rb
vendored
7
test/fixtures/poro.rb
vendored
@ -35,6 +35,7 @@ end
|
|||||||
|
|
||||||
Post = Class.new(Model)
|
Post = Class.new(Model)
|
||||||
Comment = Class.new(Model)
|
Comment = Class.new(Model)
|
||||||
|
Author = Class.new(Model)
|
||||||
|
|
||||||
PostSerializer = Class.new(ActiveModel::Serializer) do
|
PostSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
attributes :title, :body, :id
|
attributes :title, :body, :id
|
||||||
@ -47,3 +48,9 @@ CommentSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
|
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
end
|
end
|
||||||
|
|
||||||
|
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :id, :name
|
||||||
|
|
||||||
|
has_many :posts, embed: :ids
|
||||||
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user