mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16: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]
|
||||
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
|
||||
alias embed_ids? embed_ids
|
||||
alias embed_objects? embed_objects
|
||||
@ -49,7 +49,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def embedded_key
|
||||
name.pluralize
|
||||
@options[:root] || name.pluralize
|
||||
end
|
||||
end
|
||||
|
||||
@ -59,7 +59,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def embedded_key
|
||||
name
|
||||
@options[:root] || name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -9,6 +9,7 @@ module ActiveModel
|
||||
@association = PostSerializer._associations[0]
|
||||
@association.include = false
|
||||
@association.embed = :ids
|
||||
@association.options[:root] = nil
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
@ -43,6 +44,14 @@ module ActiveModel
|
||||
}, @post_serializer.as_json)
|
||||
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
|
||||
@association.include = true
|
||||
assert_equal({
|
||||
|
||||
@ -9,6 +9,7 @@ module ActiveModel
|
||||
@association = UserSerializer._associations[0]
|
||||
@association.include = false
|
||||
@association.embed = :ids
|
||||
@association.options[:root] = nil
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
@ -43,6 +44,14 @@ module ActiveModel
|
||||
}, @user_serializer.as_json)
|
||||
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
|
||||
@association.include = true
|
||||
assert_equal({
|
||||
|
||||
Loading…
Reference in New Issue
Block a user