mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Serialize associations that doesn't have an associated serializer
This commit is contained in:
parent
513e7f2166
commit
7405baafd7
@ -1,3 +1,4 @@
|
|||||||
|
require 'active_model/default_serializer'
|
||||||
require 'active_model/serializable'
|
require 'active_model/serializable'
|
||||||
require 'active_model/serializer'
|
require 'active_model/serializer'
|
||||||
|
|
||||||
@ -22,12 +23,8 @@ module ActiveModel
|
|||||||
|
|
||||||
def serializable_array
|
def serializable_array
|
||||||
@object.map do |item|
|
@object.map do |item|
|
||||||
serializer = @options[:each_serializer] || Serializer.serializer_for(item)
|
serializer = @options[:each_serializer] || Serializer.serializer_for(item) || DefaultSerializer
|
||||||
if serializer
|
serializer.new(item).serializable_object(@options.merge(root: nil))
|
||||||
serializer.new(item).serializable_object(@options.merge(root: nil))
|
|
||||||
else
|
|
||||||
item.as_json
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias serializable_object serializable_array
|
alias serializable_object serializable_array
|
||||||
|
|||||||
17
lib/active_model/default_serializer.rb
Normal file
17
lib/active_model/default_serializer.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
module ActiveModel
|
||||||
|
# DefaultSerializer
|
||||||
|
#
|
||||||
|
# Provides a constant interface for all items
|
||||||
|
class DefaultSerializer
|
||||||
|
attr_reader :object
|
||||||
|
|
||||||
|
def initialize(object, options=nil)
|
||||||
|
@object = object
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializable_hash(*)
|
||||||
|
@object.as_json
|
||||||
|
end
|
||||||
|
alias serializable_object serializable_hash
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
require 'active_model/default_serializer'
|
||||||
require 'active_model/serializer'
|
require 'active_model/serializer'
|
||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
@ -32,13 +33,8 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_serializer(object)
|
def build_serializer(object)
|
||||||
@serializer_class ||= Serializer.serializer_for(object)
|
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
||||||
|
@serializer_class.new(object, @options)
|
||||||
if @serializer_class
|
|
||||||
@serializer_class.new(object, @options)
|
|
||||||
else
|
|
||||||
object
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasOne < Association
|
class HasOne < Association
|
||||||
|
|||||||
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)
|
}, @post_serializer.as_json)
|
||||||
end
|
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
|
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
@association.embedded_key = 'root'
|
@association.embedded_key = 'root'
|
||||||
|
|||||||
@ -54,6 +54,19 @@ module ActiveModel
|
|||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
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
|
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
@association.embedded_key = 'root'
|
@association.embedded_key = 'root'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user