mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Make embed use serializer configuration
This commit is contained in:
parent
32318f9daf
commit
3db2fcec6c
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user