mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
This functionality used to exist in v0.8.1. This adds the ability to pass other options that will be usable in the serializer via the options accessor. This works by adding an attr_accessor for options so it is available and is set by the remaining options in the provided options hash during initialization.
52 lines
1.3 KiB
Ruby
52 lines
1.3 KiB
Ruby
require 'active_model/default_serializer'
|
|
require 'active_model/serializable'
|
|
require 'active_model/serializer'
|
|
|
|
module ActiveModel
|
|
class ArraySerializer
|
|
include Serializable
|
|
|
|
class << self
|
|
attr_accessor :_root
|
|
alias root _root=
|
|
alias root= _root=
|
|
end
|
|
|
|
def initialize(object, options={})
|
|
@object = object
|
|
@root = options.fetch(:root, self.class._root)
|
|
@meta_key = options[:meta_key] || :meta
|
|
@meta = options[@meta_key]
|
|
@each_serializer = options[:each_serializer]
|
|
@options = options.merge(root: nil)
|
|
end
|
|
attr_accessor :object, :root, :meta_key, :meta, :options
|
|
|
|
def json_key
|
|
if root.nil?
|
|
@options[:resource_name]
|
|
else
|
|
root
|
|
end
|
|
end
|
|
|
|
def serializer_for(item)
|
|
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
|
serializer_class.new(item, @options)
|
|
end
|
|
|
|
def serializable_array
|
|
@object.map do |item|
|
|
serializer_for(item).serializable_object
|
|
end
|
|
end
|
|
alias_method :serializable_object, :serializable_array
|
|
|
|
def embedded_in_root_associations
|
|
@object.each_with_object({}) do |item, hash|
|
|
hash.merge!(serializer_for(item).embedded_in_root_associations)
|
|
end
|
|
end
|
|
end
|
|
end
|