mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22: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={})
|
||||
@object = object
|
||||
@root = options[:root]
|
||||
@root = self.class._root if @root.nil?
|
||||
@root = options[:resource_name] if @root.nil?
|
||||
@root = options.fetch(:root, self.class._root)
|
||||
@meta_key = options[:meta_key] || :meta
|
||||
@meta = options[@meta_key]
|
||||
@each_serializer = options[:each_serializer]
|
||||
@ -24,6 +22,14 @@ module ActiveModel
|
||||
end
|
||||
attr_accessor :object, :root, :meta_key, :meta
|
||||
|
||||
def json_key
|
||||
if root.nil?
|
||||
@options[:resource_name]
|
||||
else
|
||||
root
|
||||
end
|
||||
end
|
||||
|
||||
def serializable_array
|
||||
@object.map do |item|
|
||||
serializer = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
module ActiveModel
|
||||
module Serializable
|
||||
def as_json(options={})
|
||||
if root = options[:root] || self.root
|
||||
if root = options.fetch(:root, json_key)
|
||||
hash = { root => serializable_object }
|
||||
hash.merge!(serializable_data)
|
||||
hash
|
||||
|
||||
@ -103,17 +103,18 @@ end
|
||||
def initialize(object, options={})
|
||||
@object = object
|
||||
@scope = options[:scope]
|
||||
self.root = options[:root]
|
||||
@root = options.fetch(:root, self.class._root)
|
||||
@meta_key = options[:meta_key] || :meta
|
||||
@meta = options[@meta_key]
|
||||
end
|
||||
attr_accessor :object, :scope, :meta_key, :meta
|
||||
attr_reader :root
|
||||
attr_accessor :object, :scope, :meta_key, :meta, :root
|
||||
|
||||
def root=(root)
|
||||
@root = root
|
||||
@root = self.class._root if @root.nil?
|
||||
@root = self.class.root_name if @root == true || @root.nil?
|
||||
def json_key
|
||||
if root == true || root.nil?
|
||||
self.class.root_name
|
||||
else
|
||||
root
|
||||
end
|
||||
end
|
||||
|
||||
def attributes
|
||||
|
||||
Loading…
Reference in New Issue
Block a user