mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
First try to implement ArraySerializer
This commit is contained in:
parent
1b718b6d48
commit
c1fdfc1cdc
@ -108,12 +108,7 @@ module ActiveModel
|
|||||||
self.class._associations.dup.each_with_object({}) do |(name, value), hash|
|
self.class._associations.dup.each_with_object({}) do |(name, value), hash|
|
||||||
association = object.send(name)
|
association = object.send(name)
|
||||||
serializer_class = ActiveModel::Serializer.serializer_for(association)
|
serializer_class = ActiveModel::Serializer.serializer_for(association)
|
||||||
if serializer_class
|
hash[name] = serializer_class.new(association)
|
||||||
serializer = serializer_class.new(association)
|
|
||||||
hash[name] = serializer
|
|
||||||
else
|
|
||||||
hash[name] = association
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,20 +3,21 @@ module ActiveModel
|
|||||||
class Adapter
|
class Adapter
|
||||||
class JsonApiAdapter < Adapter
|
class JsonApiAdapter < Adapter
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = {})
|
||||||
hash = serializer.attributes.each_with_object({}) do |(attr, value), h|
|
hash = serializer.attributes
|
||||||
h[attr] = value
|
|
||||||
end
|
|
||||||
|
|
||||||
serializer.associations(only: [:id]).each_with_object({}) do |(attr, value), h|
|
associations = serializer.associations(only: [:id]).each_with_object({}) do |(attr, value), h|
|
||||||
case value
|
h[attr] = case value
|
||||||
when ActiveModel::Serializer::ArraySerializer
|
when ActiveModel::Serializer::ArraySerializer
|
||||||
# process has_many association
|
value.attributes(options).map do |item|
|
||||||
when ActiveModel::Serializer
|
item.id
|
||||||
# process belongs_to association
|
end.to_a
|
||||||
else
|
when ActiveModel::Serializer
|
||||||
# what?
|
# process belongs_to association
|
||||||
end
|
else
|
||||||
|
# what?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
hash.merge(associations)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,6 +4,13 @@ module ActiveModel
|
|||||||
def initialize(object)
|
def initialize(object)
|
||||||
@object = object
|
@object = object
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def attributes(options = {})
|
||||||
|
object.map do |item|
|
||||||
|
serializer_class = ActiveModel::Serializer.serializer_for(item)
|
||||||
|
serializer_class.new(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,24 +5,19 @@ module ActiveModel
|
|||||||
class Adapter
|
class Adapter
|
||||||
class JsonApiTest < Minitest::Test
|
class JsonApiTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@post = ::Model.new(title: 'New Post', body: 'Body')
|
@post = Post.new(title: 'New Post', body: 'Body')
|
||||||
@first_comment = ::Model.new(id: 1, body: 'ZOMG A COMMENT')
|
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
@second_comment = ::Model.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||||
@post.comments = [@first_comment, @second_comment]
|
@post.comments = [@first_comment, @second_comment]
|
||||||
@first_comment.post = @post
|
@first_comment.post = @post
|
||||||
@second_comment.post = @post
|
@second_comment.post = @post
|
||||||
|
|
||||||
@post_serializer_class = def_serializer do
|
@post_serializer = PostSerializer.new(@post)
|
||||||
attributes :title, :body
|
|
||||||
has_many :comments
|
|
||||||
end
|
|
||||||
|
|
||||||
@post_serializer = @post_serializer_class.new(@post)
|
|
||||||
@adapter = ActiveModel::Serializer::Adapter::JsonApiAdapter.new(@post_serializer)
|
@adapter = ActiveModel::Serializer::Adapter::JsonApiAdapter.new(@post_serializer)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
assert_equal(['1', '2'], @adapter.serializable_hash[:comments])
|
assert_equal([1, 2], @adapter.serializable_hash[:comments])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
21
test/fixtures/poro.rb
vendored
21
test/fixtures/poro.rb
vendored
@ -5,12 +5,16 @@ class Model
|
|||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
def read_attribute_for_serialization(name)
|
||||||
if name == :id || name == 'id'
|
if name == :id || name == 'id'
|
||||||
object_id
|
id
|
||||||
else
|
else
|
||||||
@attributes[name]
|
@attributes[name]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def id
|
||||||
|
@attributes[:id] || @attributes['id'] || object_id
|
||||||
|
end
|
||||||
|
|
||||||
def method_missing(meth, *args)
|
def method_missing(meth, *args)
|
||||||
if meth.to_s =~ /^(.*)=$/
|
if meth.to_s =~ /^(.*)=$/
|
||||||
@attributes[$1.to_sym] = args[0]
|
@attributes[$1.to_sym] = args[0]
|
||||||
@ -28,3 +32,18 @@ end
|
|||||||
class ProfileSerializer < ActiveModel::Serializer
|
class ProfileSerializer < ActiveModel::Serializer
|
||||||
attributes :name, :description
|
attributes :name, :description
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Post = Class.new(Model)
|
||||||
|
Comment = Class.new(Model)
|
||||||
|
|
||||||
|
PostSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :title, :body, :id
|
||||||
|
|
||||||
|
has_many :comments
|
||||||
|
end
|
||||||
|
|
||||||
|
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :id, :body
|
||||||
|
|
||||||
|
belongs_to :post
|
||||||
|
end
|
||||||
|
|||||||
@ -3,10 +3,6 @@ require 'test_helper'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class AssocationsTest < Minitest::Test
|
class AssocationsTest < Minitest::Test
|
||||||
def def_serializer(&block)
|
|
||||||
Class.new(ActiveModel::Serializer, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
class Model
|
class Model
|
||||||
def initialize(hash={})
|
def initialize(hash={})
|
||||||
@attributes = hash
|
@attributes = hash
|
||||||
@ -26,19 +22,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Post = Class.new(Model)
|
|
||||||
Comment = Class.new(Model)
|
|
||||||
PostSerializer = Class.new(ActiveModel::Serializer) do
|
|
||||||
attributes :title, :body
|
|
||||||
|
|
||||||
has_many :comments
|
|
||||||
end
|
|
||||||
|
|
||||||
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
|
||||||
attributes :id, :body
|
|
||||||
|
|
||||||
belongs_to :post
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user