mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Allow to set root for associations through options
This commit is contained in:
parent
8a495b1d00
commit
48590a2e37
@ -14,7 +14,7 @@ module ActiveModel
|
|||||||
self.serializer_class = @options[:serializer]
|
self.serializer_class = @options[:serializer]
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :name, :embed_ids, :embed_objects, :embed_key, :serializer_class
|
attr_reader :name, :embed_ids, :embed_objects, :embed_key, :serializer_class, :options
|
||||||
attr_accessor :include
|
attr_accessor :include
|
||||||
alias embed_ids? embed_ids
|
alias embed_ids? embed_ids
|
||||||
alias embed_objects? embed_objects
|
alias embed_objects? embed_objects
|
||||||
@ -49,7 +49,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def embedded_key
|
def embedded_key
|
||||||
name.pluralize
|
@options[:root] || name.pluralize
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def embedded_key
|
def embedded_key
|
||||||
name
|
@options[:root] || name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -9,6 +9,7 @@ module ActiveModel
|
|||||||
@association = PostSerializer._associations[0]
|
@association = PostSerializer._associations[0]
|
||||||
@association.include = false
|
@association.include = false
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
|
@association.options[:root] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -43,6 +44,14 @@ module ActiveModel
|
|||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||||
|
@association.embed = :objects
|
||||||
|
@association.options[:root] = 'root'
|
||||||
|
assert_equal({
|
||||||
|
'title' => 'Title 1', 'body' => 'Body 1', 'root' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||||
|
}, @post_serializer.serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||||
@association.include = true
|
@association.include = true
|
||||||
assert_equal({
|
assert_equal({
|
||||||
|
|||||||
@ -9,6 +9,7 @@ module ActiveModel
|
|||||||
@association = UserSerializer._associations[0]
|
@association = UserSerializer._associations[0]
|
||||||
@association.include = false
|
@association.include = false
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
|
@association.options[:root] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -43,6 +44,14 @@ module ActiveModel
|
|||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||||
|
@association.embed = :objects
|
||||||
|
@association.options[:root] = 'root'
|
||||||
|
assert_equal({
|
||||||
|
'name' => 'Name 1', 'email' => 'mail@server.com', 'root' => [{ 'name' => 'N1', 'description' => 'D1' }]
|
||||||
|
}, @user_serializer.serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||||
@association.include = true
|
@association.include = true
|
||||||
assert_equal({
|
assert_equal({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user