mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Allow to set embed options from AM::Serializer
This commit is contained in:
parent
aa23e811cc
commit
10e882a14f
@ -17,6 +17,11 @@ module ActiveModel
|
||||
yield SETTINGS
|
||||
end
|
||||
|
||||
def embed(type, options={})
|
||||
SETTINGS[:embed] = type
|
||||
SETTINGS[:include] = true if options[:include]
|
||||
end
|
||||
|
||||
def serializer_for(resource)
|
||||
if resource.respond_to?(:to_ary)
|
||||
ArraySerializer
|
||||
|
||||
@ -8,8 +8,8 @@ module ActiveModel
|
||||
@name = name
|
||||
@options = options
|
||||
|
||||
self.embed = options[:embed]
|
||||
@embed_in_root = @embed_ids && options[:include]
|
||||
self.embed = options[:embed] || SETTINGS[:embed]
|
||||
@embed_in_root = @embed_ids && (options[:include] || SETTINGS[:include])
|
||||
@embed_key = options[:embed_key] || :id
|
||||
@key = options[:key]
|
||||
@embedded_key = options[:root]
|
||||
|
||||
@ -84,12 +84,16 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||
@association.embed_in_root = true
|
||||
PostSerializer.embed :ids, include: true
|
||||
PostSerializer._associations[0].send :initialize, @association.name, @association.options
|
||||
|
||||
@post_serializer.root = nil
|
||||
assert_equal({
|
||||
'post' => { 'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||
'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||
}, @post_serializer.as_json)
|
||||
ensure
|
||||
SETTINGS.clear
|
||||
end
|
||||
|
||||
def test_associations_using_a_given_serializer
|
||||
|
||||
Loading…
Reference in New Issue
Block a user