mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Serialize associations that doesn't have an associated serializer
This commit is contained in:
15
test/unit/active_model/default_serializer_test.rb
Normal file
15
test/unit/active_model/default_serializer_test.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class DefaultSerializer
|
||||
class Test < ActiveModel::TestCase
|
||||
def test_serialize_objects
|
||||
assert_equal(nil, DefaultSerializer.new(nil).serializable_hash)
|
||||
assert_equal(1, DefaultSerializer.new(1).serializable_hash)
|
||||
assert_equal('hi', DefaultSerializer.new('hi').serializable_hash)
|
||||
obj = Struct.new(:a, :b).new(1, 2)
|
||||
assert_equal({ a: 1, b: 2 }, DefaultSerializer.new(obj).serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -54,6 +54,19 @@ module ActiveModel
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_nil_objects_serialization_using_as_json
|
||||
@association.embed = :objects
|
||||
@post.instance_eval do
|
||||
def comments
|
||||
[nil]
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [nil]
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||
@association.embed = :objects
|
||||
@association.embedded_key = 'root'
|
||||
|
||||
@@ -54,6 +54,19 @@ module ActiveModel
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_nil_objects_serialization_using_as_json
|
||||
@association.embed = :objects
|
||||
@user.instance_eval do
|
||||
def profile
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profiles' => [nil]
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||
@association.embed = :objects
|
||||
@association.embedded_key = 'root'
|
||||
|
||||
Reference in New Issue
Block a user