mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Move options to configuration
This commit is contained in:
parent
3db2fcec6c
commit
1e5ea0b356
@ -7,44 +7,49 @@ module ActiveModel
|
|||||||
include Serializable
|
include Serializable
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :_root
|
def configuration
|
||||||
alias root _root=
|
@configuration ||=
|
||||||
alias root= _root=
|
if self == ArraySerializer
|
||||||
|
Serializer::Configuration.global
|
||||||
|
else
|
||||||
|
superclass.configuration.build
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(object, options={})
|
extend Forwardable
|
||||||
|
|
||||||
|
def_delegators :configuration, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name
|
||||||
|
|
||||||
|
attr_accessor :object, :configuration
|
||||||
|
|
||||||
|
def initialize(object, options = {})
|
||||||
@object = object
|
@object = object
|
||||||
@scope = options[:scope]
|
@configuration = self.class.configuration.build options
|
||||||
@root = options.fetch(:root, self.class._root)
|
|
||||||
@meta_key = options[:meta_key] || :meta
|
|
||||||
@meta = options[@meta_key]
|
|
||||||
@each_serializer = options[:each_serializer]
|
|
||||||
@resource_name = options[:resource_name]
|
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :root, :meta_key, :meta
|
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root.nil?
|
if root.nil?
|
||||||
@resource_name
|
resource_name
|
||||||
else
|
else
|
||||||
root
|
root
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializer_for(item)
|
def serializer_for(item)
|
||||||
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
serializer_class = each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||||
serializer_class.new(item, scope: scope)
|
serializer_class.new(item, scope: scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_object
|
def serializable_object
|
||||||
@object.map do |item|
|
object.map do |item|
|
||||||
serializer_for(item).serializable_object
|
serializer_for(item).serializable_object
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias_method :serializable_array, :serializable_object
|
alias_method :serializable_array, :serializable_object
|
||||||
|
|
||||||
def embedded_in_root_associations
|
def embedded_in_root_associations
|
||||||
@object.each_with_object({}) do |item, hash|
|
object.each_with_object({}) do |item, hash|
|
||||||
serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
|
serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
|
||||||
next if !objects || objects.flatten.empty?
|
next if !objects || objects.flatten.empty?
|
||||||
|
|
||||||
|
|||||||
@ -7,13 +7,14 @@ module ActiveModel
|
|||||||
class DefaultSerializer
|
class DefaultSerializer
|
||||||
include ActiveModel::Serializable
|
include ActiveModel::Serializable
|
||||||
|
|
||||||
attr_reader :object
|
attr_reader :object, :configuration
|
||||||
|
|
||||||
def initialize(object, options=nil)
|
def initialize(object, options = nil)
|
||||||
@object = object
|
@object = object
|
||||||
|
@configuration = Serializer::Configuration::Null.instance
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_json(options={})
|
def as_json(options = {})
|
||||||
@object.as_json
|
@object.as_json
|
||||||
end
|
end
|
||||||
alias serializable_hash as_json
|
alias serializable_hash as_json
|
||||||
|
|||||||
@ -12,7 +12,6 @@ module ActiveModel
|
|||||||
|
|
||||||
class << self
|
class << self
|
||||||
def inherited(base)
|
def inherited(base)
|
||||||
base._root = _root
|
|
||||||
base._attributes = (_attributes || []).dup
|
base._attributes = (_attributes || []).dup
|
||||||
base._associations = (_associations || {}).dup
|
base._associations = (_associations || {}).dup
|
||||||
end
|
end
|
||||||
@ -39,9 +38,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :_root, :_attributes, :_associations
|
attr_accessor :_attributes, :_associations
|
||||||
alias root _root=
|
|
||||||
alias root= _root=
|
|
||||||
|
|
||||||
def root_name
|
def root_name
|
||||||
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
||||||
@ -49,10 +46,15 @@ module ActiveModel
|
|||||||
|
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
def_delegators :dsl, :attributes, :has_one, :has_many, :embed
|
def_delegators :dsl, :attributes, :has_one, :has_many, :embed, :root
|
||||||
|
|
||||||
def configuration
|
def configuration
|
||||||
@configuration ||= Configuration.global.build
|
@configuration ||=
|
||||||
|
if self == Serializer
|
||||||
|
Configuration.global
|
||||||
|
else
|
||||||
|
superclass.configuration.build
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@ -62,15 +64,14 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :object, :scope, :root, :meta_key, :meta, :configuration
|
extend Forwardable
|
||||||
|
|
||||||
def initialize(object, options={})
|
def_delegators :configuration, :scope, :root, :meta_key, :meta, :wrap_in_array
|
||||||
|
|
||||||
|
attr_accessor :object, :configuration
|
||||||
|
|
||||||
|
def initialize(object, options = {})
|
||||||
@object = object
|
@object = object
|
||||||
@scope = options[:scope]
|
|
||||||
@root = options.fetch(:root, self.class._root)
|
|
||||||
@meta_key = options[:meta_key] || :meta
|
|
||||||
@meta = options[@meta_key]
|
|
||||||
@wrap_in_array = options[:_wrap_in_array]
|
|
||||||
@configuration = self.class.configuration.build options
|
@configuration = self.class.configuration.build options
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -149,7 +150,7 @@ module ActiveModel
|
|||||||
return nil if object.nil?
|
return nil if object.nil?
|
||||||
hash = attributes
|
hash = attributes
|
||||||
hash.merge! associations
|
hash.merge! associations
|
||||||
@wrap_in_array ? [hash] : hash
|
wrap_in_array ? [hash] : hash
|
||||||
end
|
end
|
||||||
alias_method :serializable_hash, :serializable_object
|
alias_method :serializable_hash, :serializable_object
|
||||||
end
|
end
|
||||||
|
|||||||
@ -58,8 +58,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_serializer(object, options = {})
|
def build_serializer(object, options = {})
|
||||||
options[:_wrap_in_array] = embed_in_root?
|
super.tap do |instance|
|
||||||
super
|
instance.configuration.wrap_in_array = embed_in_root?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def default_options
|
def default_options
|
||||||
{ embed: :objects }
|
{ embed: :objects, meta_key: :meta }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -31,11 +31,21 @@ module ActiveModel
|
|||||||
self.class.new options, self
|
self.class.new options, self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_reader :scope, :each_serializer, :resource_name
|
||||||
|
attr_writer :root, :meta, :meta_key
|
||||||
|
attr_accessor :wrap_in_array
|
||||||
|
|
||||||
def initialize(options = {}, parent = Null.instance)
|
def initialize(options = {}, parent = Null.instance)
|
||||||
|
@parent = parent
|
||||||
@root = read_option options, :root
|
@root = read_option options, :root
|
||||||
@embed = read_option options, :embed
|
@embed = read_option options, :embed
|
||||||
@embed_in_root = read_option options, :embed_in_root
|
@embed_in_root = read_option options, :embed_in_root
|
||||||
@parent = parent
|
@scope = options[:scope]
|
||||||
|
@meta_key = read_option options, :meta_key
|
||||||
|
@meta = read_option options, meta_key
|
||||||
|
@wrap_in_array = options[:_wrap_in_array]
|
||||||
|
@each_serializer = options[:each_serializer]
|
||||||
|
@resource_name = options[:resource_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
def root
|
def root
|
||||||
@ -50,6 +60,14 @@ module ActiveModel
|
|||||||
return_first @embed_in_root, parent.embed_in_root
|
return_first @embed_in_root, parent.embed_in_root
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def meta_key
|
||||||
|
return_first @meta_key, parent.meta_key
|
||||||
|
end
|
||||||
|
|
||||||
|
def meta
|
||||||
|
return_first @meta, parent.meta
|
||||||
|
end
|
||||||
|
|
||||||
# FIXME: Get rid of this mess.
|
# FIXME: Get rid of this mess.
|
||||||
def embed_objects=(value)
|
def embed_objects=(value)
|
||||||
@embed = :objects if value
|
@embed = :objects if value
|
||||||
|
|||||||
@ -30,6 +30,10 @@ module ActiveModel
|
|||||||
configuration.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
|
||||||
|
|
||||||
|
def root(value)
|
||||||
|
configuration.root = value
|
||||||
|
end
|
||||||
|
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
def_delegators :serializer_class, :configuration
|
def_delegators :serializer_class, :configuration
|
||||||
|
|||||||
@ -11,7 +11,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_meta
|
def test_meta
|
||||||
@serializer.meta = { total: 10 }
|
@serializer.configuration.meta = { total: 10 }
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'profiles' => [
|
'profiles' => [
|
||||||
@ -30,8 +30,8 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_using_meta_key
|
def test_meta_using_meta_key
|
||||||
@serializer.meta_key = :my_meta
|
@serializer.configuration.meta_key = :my_meta
|
||||||
@serializer.meta = { total: 10 }
|
@serializer.configuration.meta = { total: 10 }
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'profiles' => [
|
'profiles' => [
|
||||||
|
|||||||
@ -4,14 +4,14 @@ module ActiveModel
|
|||||||
class ArraySerializer
|
class ArraySerializer
|
||||||
class RootAsOptionTest < Minitest::Test
|
class RootAsOptionTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@old_root = ArraySerializer._root
|
@old_root = ArraySerializer.configuration.root
|
||||||
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
||||||
@serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
|
@serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
ArraySerializer._root = @old_root
|
ArraySerializer.configuration.root = @old_root
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_root_is_not_displayed_using_serializable_array
|
def test_root_is_not_displayed_using_serializable_array
|
||||||
@ -40,7 +40,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_using_false_root_in_initialize_takes_precedence
|
def test_using_false_root_in_initialize_takes_precedence
|
||||||
ArraySerializer._root = 'root'
|
ArraySerializer.configuration.root = 'root'
|
||||||
@serializer = ArraySerializer.new([@profile1, @profile2], root: false)
|
@serializer = ArraySerializer.new([@profile1, @profile2], root: false)
|
||||||
|
|
||||||
assert_equal([
|
assert_equal([
|
||||||
@ -52,8 +52,8 @@ module ActiveModel
|
|||||||
|
|
||||||
class RootInSerializerTest < Minitest::Test
|
class RootInSerializerTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@old_root = ArraySerializer._root
|
@old_root = ArraySerializer.configuration.root
|
||||||
ArraySerializer._root = :in_serializer
|
ArraySerializer.configuration.root = :in_serializer
|
||||||
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
||||||
@serializer = ArraySerializer.new([@profile1, @profile2])
|
@serializer = ArraySerializer.new([@profile1, @profile2])
|
||||||
@ -61,7 +61,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
ArraySerializer._root = @old_root
|
ArraySerializer.configuration.root = @old_root
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_root_is_not_displayed_using_serializable_hash
|
def test_root_is_not_displayed_using_serializable_hash
|
||||||
|
|||||||
@ -4,14 +4,14 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class RootAsOptionTest < Minitest::Test
|
class RootAsOptionTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@old_root = ProfileSerializer._root
|
@old_root = ProfileSerializer.configuration.root
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
@serializer = ProfileSerializer.new(@profile, root: :initialize)
|
@serializer = ProfileSerializer.new(@profile, root: :initialize)
|
||||||
ProfileSerializer._root = true
|
ProfileSerializer.root true
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
ProfileSerializer._root = @old_root
|
ProfileSerializer.root @old_root
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_root_is_not_displayed_using_serializable_hash
|
def test_root_is_not_displayed_using_serializable_hash
|
||||||
@ -47,7 +47,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_using_false_root_in_initializer_takes_precedence
|
def test_using_false_root_in_initializer_takes_precedence
|
||||||
ProfileSerializer._root = 'root'
|
ProfileSerializer.root 'root'
|
||||||
@serializer = ProfileSerializer.new(@profile, root: false)
|
@serializer = ProfileSerializer.new(@profile, root: false)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
@ -56,31 +56,31 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_root_inheritance
|
def test_root_inheritance
|
||||||
ProfileSerializer._root = 'profile'
|
ProfileSerializer.root 'profile'
|
||||||
|
|
||||||
inherited_serializer_klass = Class.new(ProfileSerializer)
|
inherited_serializer_klass = Class.new(ProfileSerializer)
|
||||||
inherited_serializer_klass._root = 'inherited_profile'
|
inherited_serializer_klass.root 'inherited_profile'
|
||||||
|
|
||||||
another_inherited_serializer_klass = Class.new(ProfileSerializer)
|
another_inherited_serializer_klass = Class.new(ProfileSerializer)
|
||||||
|
|
||||||
assert_equal('inherited_profile',
|
assert_equal('inherited_profile',
|
||||||
inherited_serializer_klass._root)
|
inherited_serializer_klass.configuration.root)
|
||||||
assert_equal('profile',
|
assert_equal('profile',
|
||||||
another_inherited_serializer_klass._root)
|
another_inherited_serializer_klass.configuration.root)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class RootInSerializerTest < Minitest::Test
|
class RootInSerializerTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@old_root = ProfileSerializer._root
|
@old_root = ProfileSerializer.configuration.root
|
||||||
ProfileSerializer._root = :in_serializer
|
ProfileSerializer.root :in_serializer
|
||||||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
@serializer = ProfileSerializer.new(profile)
|
@serializer = ProfileSerializer.new(profile)
|
||||||
@rooted_serializer = ProfileSerializer.new(profile, root: :initialize)
|
@rooted_serializer = ProfileSerializer.new(profile, root: :initialize)
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
ProfileSerializer._root = @old_root
|
ProfileSerializer.root @old_root
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_root_is_not_displayed_using_serializable_hash
|
def test_root_is_not_displayed_using_serializable_hash
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user