Allow to embed nothing but still side loading

Closes #361, #399, #401
This commit is contained in:
Santiago Pastorino 2013-10-24 13:24:24 -02:00
parent df5ef33ae6
commit 5c6541145a
3 changed files with 17 additions and 3 deletions

View File

@ -7,9 +7,8 @@ module ActiveModel
def initialize(name, options={})
@name = name.to_s
@options = options
self.embed = options[:embed] || CONFIG.embed || :objects
@embed_in_root = @embed_ids && (options[:include] || CONFIG.include)
self.embed = options.fetch(:embed) { CONFIG.embed }
@embed_in_root = options.fetch(:include) { CONFIG.include }
@embed_key = options[:embed_key] || :id
@key = options[:key]
@embedded_key = options[:root] || name

View File

@ -48,5 +48,6 @@ module ActiveModel
end
CONFIG = Config.new
CONFIG.embed = :objects
end
end

View File

@ -98,6 +98,20 @@ module ActiveModel
CONFIG.clear
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
@association.embed_in_root = true
@post_serializer.root = nil