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 module ActiveModel
class Serializer class Serializer
class Association class Association
def initialize(name, options = {}) def initialize(name, options = {}, configuration = nil)
if options.has_key?(:include) if options.has_key?(:include)
ActiveSupport::Deprecation.warn <<-WARN ActiveSupport::Deprecation.warn <<-WARN
** Notice: include was renamed to embed_in_root. ** ** Notice: include was renamed to embed_in_root. **
@ -13,8 +13,8 @@ module ActiveModel
@name = name.to_s @name = name.to_s
@options = options @options = options
self.embed = options.fetch(:embed) { Configuration.global.embed } self.embed = options.fetch(:embed) { configuration.embed }
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { Configuration.global.embed_in_root } } @embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { configuration.embed_in_root } }
@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

View File

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

View File

@ -5,7 +5,7 @@ module ActiveModel
class Association class Association
class BuildSerializerTest < Minitest::Test class BuildSerializerTest < Minitest::Test
def setup 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' }) @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
end end