Make embed use serializer configuration

This commit is contained in:
Adrian Mugnolo and Santiago Pastorino 2014-01-13 14:26:52 -02:00 committed by Santiago Pastorino
parent 32318f9daf
commit 3db2fcec6c
3 changed files with 11 additions and 7 deletions

View File

@ -4,7 +4,7 @@ require 'active_model/serializer'
module ActiveModel
class Serializer
class Association
def initialize(name, options = {})
def initialize(name, options = {}, configuration = nil)
if options.has_key?(:include)
ActiveSupport::Deprecation.warn <<-WARN
** Notice: include was renamed to embed_in_root. **
@ -13,8 +13,8 @@ module ActiveModel
@name = name.to_s
@options = options
self.embed = options.fetch(:embed) { Configuration.global.embed }
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { Configuration.global.embed_in_root } }
self.embed = options.fetch(:embed) { configuration.embed }
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { configuration.embed_in_root } }
@embed_key = options[:embed_key] || :id
@key = options[:key]
@embedded_key = options[:root] || name

View File

@ -26,10 +26,14 @@ module ActiveModel
end
def embed(type, options = {})
Configuration.global.embed = type
Configuration.global.embed_in_root = true if options[:embed_in_root] || options[:include]
configuration.embed = type
configuration.embed_in_root = true if options[:embed_in_root] || options[:include]
end
extend Forwardable
def_delegators :serializer_class, :configuration
private
def associate(klass, *names)
@ -40,7 +44,7 @@ module ActiveModel
object.send name
end unless serializer_class.method_defined? name
serializer_class._associations[name] = klass.new name, options
serializer_class._associations[name] = klass.new name, options, configuration
end
end
end

View File

@ -5,7 +5,7 @@ module ActiveModel
class Association
class BuildSerializerTest < Minitest::Test
def setup
@association = Association::HasOne.new('post', serializer: PostSerializer)
@association = Association::HasOne.new('post', { serializer: PostSerializer }, PostSerializer.configuration)
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
end