Sha256: 34e5ad92a9c7313b1fe4dcd1099a1c21e94f4a14336e66e291770330e7e1be1a

Contents?: true

Size: 1.59 KB

Versions: 12

Compression:

Stored size: 1.59 KB

Contents

require 'active_model/serializable'
require 'active_model/serializer/caching'
require "active_support/core_ext/class/attribute"
require 'active_support/dependencies'
require 'active_support/descendants_tracker'

module ActiveModel
  # Active Model Array Serializer
  #
  # Serializes an Array, checking if each element implements
  # the +active_model_serializer+ method.
  #
  # To disable serialization of root elements:
  #
  #     ActiveModel::ArraySerializer.root = false
  #
  class ArraySerializer
    extend ActiveSupport::DescendantsTracker

    include ActiveModel::Serializable
    include ActiveModel::Serializer::Caching

    attr_reader :object, :options

    class_attribute :root

    class_attribute :cache
    class_attribute :perform_caching

    class << self
      # set perform caching like root
      def cached(value = true)
        self.perform_caching = value
      end
    end

    def initialize(object, options={})
      @object  = object
      @options = options
    end

    def serialize_object
      serializable_array
    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
        serializer ||= DefaultSerializer

        serializable = serializer.new(item, options.merge(root: nil))

        if serializable.respond_to?(:serializable_hash)
          serializable.serializable_hash
        else
          serializable.as_json
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
gpi-active_model_serializers-0.8.5 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.5.alpha1 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.4 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.4.alpha5 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.4.alpha4 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.4.alpha3 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.4.alpha2 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.4.alpha1 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.3.rc1 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.3.alpha2 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.3.alpha1 lib/active_model/array_serializer.rb
gpi-active_model_serializers-0.8.2 lib/active_model/array_serializer.rb