mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
parent
df5ef33ae6
commit
5c6541145a
@ -7,9 +7,8 @@ module ActiveModel
|
|||||||
def initialize(name, options={})
|
def initialize(name, options={})
|
||||||
@name = name.to_s
|
@name = name.to_s
|
||||||
@options = options
|
@options = options
|
||||||
|
self.embed = options.fetch(:embed) { CONFIG.embed }
|
||||||
self.embed = options[:embed] || CONFIG.embed || :objects
|
@embed_in_root = options.fetch(:include) { CONFIG.include }
|
||||||
@embed_in_root = @embed_ids && (options[:include] || CONFIG.include)
|
|
||||||
@embed_key = options[:embed_key] || :id
|
@embed_key = options[:embed_key] || :id
|
||||||
@key = options[:key]
|
@key = options[:key]
|
||||||
@embedded_key = options[:root] || name
|
@embedded_key = options[:root] || name
|
||||||
|
|||||||
@ -48,5 +48,6 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
CONFIG = Config.new
|
CONFIG = Config.new
|
||||||
|
CONFIG.embed = :objects
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -98,6 +98,20 @@ module ActiveModel
|
|||||||
CONFIG.clear
|
CONFIG.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_nothing_including_objects_serialization_using_as_json
|
||||||
|
CONFIG.embed = nil
|
||||||
|
CONFIG.include = true
|
||||||
|
|
||||||
|
PostSerializer._associations[:comments].send :initialize, @association.name, @association.options
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
'post' => { title: 'Title 1', body: 'Body 1' },
|
||||||
|
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
||||||
|
}, @post_serializer.as_json)
|
||||||
|
ensure
|
||||||
|
CONFIG.clear
|
||||||
|
end
|
||||||
|
|
||||||
def test_associations_using_a_given_serializer
|
def test_associations_using_a_given_serializer
|
||||||
@association.embed_in_root = true
|
@association.embed_in_root = true
|
||||||
@post_serializer.root = nil
|
@post_serializer.root = nil
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user