mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
First try to implement ArraySerializer
This commit is contained in:
@@ -5,24 +5,19 @@ module ActiveModel
|
||||
class Adapter
|
||||
class JsonApiTest < Minitest::Test
|
||||
def setup
|
||||
@post = ::Model.new(title: 'New Post', body: 'Body')
|
||||
@first_comment = ::Model.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@second_comment = ::Model.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||
@post.comments = [@first_comment, @second_comment]
|
||||
@first_comment.post = @post
|
||||
@second_comment.post = @post
|
||||
|
||||
@post_serializer_class = def_serializer do
|
||||
attributes :title, :body
|
||||
has_many :comments
|
||||
end
|
||||
|
||||
@post_serializer = @post_serializer_class.new(@post)
|
||||
@post_serializer = PostSerializer.new(@post)
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApiAdapter.new(@post_serializer)
|
||||
end
|
||||
|
||||
def test_includes_comment_ids
|
||||
assert_equal(['1', '2'], @adapter.serializable_hash[:comments])
|
||||
assert_equal([1, 2], @adapter.serializable_hash[:comments])
|
||||
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)
|
||||
if name == :id || name == 'id'
|
||||
object_id
|
||||
id
|
||||
else
|
||||
@attributes[name]
|
||||
end
|
||||
end
|
||||
|
||||
def id
|
||||
@attributes[:id] || @attributes['id'] || object_id
|
||||
end
|
||||
|
||||
def method_missing(meth, *args)
|
||||
if meth.to_s =~ /^(.*)=$/
|
||||
@attributes[$1.to_sym] = args[0]
|
||||
@@ -28,3 +32,18 @@ end
|
||||
class ProfileSerializer < ActiveModel::Serializer
|
||||
attributes :name, :description
|
||||
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
|
||||
class Serializer
|
||||
class AssocationsTest < Minitest::Test
|
||||
def def_serializer(&block)
|
||||
Class.new(ActiveModel::Serializer, &block)
|
||||
end
|
||||
|
||||
class Model
|
||||
def initialize(hash={})
|
||||
@attributes = hash
|
||||
@@ -26,19 +22,7 @@ module ActiveModel
|
||||
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
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
|
||||
Reference in New Issue
Block a user