mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Split root construction and serialization logic
This commit is contained in:
parent
49ab359a34
commit
1eea008336
@ -14,9 +14,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
@root = options[:root]
|
@root = options.fetch(:root, self.class._root)
|
||||||
@root = self.class._root if @root.nil?
|
|
||||||
@root = options[:resource_name] if @root.nil?
|
|
||||||
@meta_key = options[:meta_key] || :meta
|
@meta_key = options[:meta_key] || :meta
|
||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
@each_serializer = options[:each_serializer]
|
@each_serializer = options[:each_serializer]
|
||||||
@ -24,6 +22,14 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
attr_accessor :object, :root, :meta_key, :meta
|
attr_accessor :object, :root, :meta_key, :meta
|
||||||
|
|
||||||
|
def json_key
|
||||||
|
if root.nil?
|
||||||
|
@options[:resource_name]
|
||||||
|
else
|
||||||
|
root
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def serializable_array
|
def serializable_array
|
||||||
@object.map do |item|
|
@object.map do |item|
|
||||||
serializer = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
serializer = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
module Serializable
|
module Serializable
|
||||||
def as_json(options={})
|
def as_json(options={})
|
||||||
if root = options[:root] || self.root
|
if root = options.fetch(:root, json_key)
|
||||||
hash = { root => serializable_object }
|
hash = { root => serializable_object }
|
||||||
hash.merge!(serializable_data)
|
hash.merge!(serializable_data)
|
||||||
hash
|
hash
|
||||||
|
|||||||
@ -103,17 +103,18 @@ end
|
|||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object = object
|
@object = object
|
||||||
@scope = options[:scope]
|
@scope = options[:scope]
|
||||||
self.root = options[:root]
|
@root = options.fetch(:root, self.class._root)
|
||||||
@meta_key = options[:meta_key] || :meta
|
@meta_key = options[:meta_key] || :meta
|
||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :meta_key, :meta
|
attr_accessor :object, :scope, :meta_key, :meta, :root
|
||||||
attr_reader :root
|
|
||||||
|
|
||||||
def root=(root)
|
def json_key
|
||||||
@root = root
|
if root == true || root.nil?
|
||||||
@root = self.class._root if @root.nil?
|
self.class.root_name
|
||||||
@root = self.class.root_name if @root == true || @root.nil?
|
else
|
||||||
|
root
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user