mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
move ArraySerializer to separate file
This commit is contained in:
parent
f9117072fb
commit
b8f01ba2f3
58
lib/active_model/array_serializer.rb
Normal file
58
lib/active_model/array_serializer.rb
Normal file
@ -0,0 +1,58 @@
|
||||
require "active_support/core_ext/class/attribute"
|
||||
|
||||
module ActiveModel
|
||||
# Active Model Array Serializer
|
||||
#
|
||||
# It serializes an Array, checking if each element that implements
|
||||
# the +active_model_serializer+ method.
|
||||
#
|
||||
# To disable serialization of root elements:
|
||||
#
|
||||
# ActiveModel::ArraySerializer.root = false
|
||||
#
|
||||
class ArraySerializer
|
||||
attr_reader :object, :options
|
||||
|
||||
class_attribute :root
|
||||
|
||||
def initialize(object, options={})
|
||||
@object, @options = object, options
|
||||
end
|
||||
|
||||
def serializable_array
|
||||
@object.map do |item|
|
||||
if @options.has_key? :each_serializer
|
||||
serializer = @options[:each_serializer]
|
||||
elsif item.respond_to?(:active_model_serializer)
|
||||
serializer = item.active_model_serializer
|
||||
end
|
||||
|
||||
if serializer
|
||||
serializer.new(item, @options)
|
||||
else
|
||||
item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def as_json(*args)
|
||||
@options[:hash] = hash = {}
|
||||
@options[:unique_values] = {}
|
||||
|
||||
array = serializable_array.map do |item|
|
||||
if item.respond_to?(:serializable_hash)
|
||||
item.serializable_hash
|
||||
else
|
||||
item.as_json
|
||||
end
|
||||
end
|
||||
|
||||
if root = @options[:root]
|
||||
hash.merge!(root => array)
|
||||
else
|
||||
array
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@ -27,60 +27,6 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
# Active Model Array Serializer
|
||||
#
|
||||
# It serializes an Array, checking if each element that implements
|
||||
# the +active_model_serializer+ method.
|
||||
#
|
||||
# To disable serialization of root elements:
|
||||
#
|
||||
# ActiveModel::ArraySerializer.root = false
|
||||
#
|
||||
class ArraySerializer
|
||||
attr_reader :object, :options
|
||||
|
||||
class_attribute :root
|
||||
|
||||
def initialize(object, options={})
|
||||
@object, @options = object, options
|
||||
end
|
||||
|
||||
def serializable_array
|
||||
@object.map do |item|
|
||||
if @options.has_key? :each_serializer
|
||||
serializer = @options[:each_serializer]
|
||||
elsif item.respond_to?(:active_model_serializer)
|
||||
serializer = item.active_model_serializer
|
||||
end
|
||||
|
||||
if serializer
|
||||
serializer.new(item, @options)
|
||||
else
|
||||
item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def as_json(*args)
|
||||
@options[:hash] = hash = {}
|
||||
@options[:unique_values] = {}
|
||||
|
||||
array = serializable_array.map do |item|
|
||||
if item.respond_to?(:serializable_hash)
|
||||
item.serializable_hash
|
||||
else
|
||||
item.as_json
|
||||
end
|
||||
end
|
||||
|
||||
if root = @options[:root]
|
||||
hash.merge!(root => array)
|
||||
else
|
||||
array
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Active Model Serializer
|
||||
#
|
||||
# Provides a basic serializer implementation that allows you to easily
|
||||
|
||||
@ -2,6 +2,7 @@ require "active_support"
|
||||
require "active_support/core_ext/string/inflections"
|
||||
require "active_support/notifications"
|
||||
require "active_model"
|
||||
require "active_model/array_serializer"
|
||||
require "active_model/serializer"
|
||||
require "set"
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user