Class: ActiveModel::ArraySerializer

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/active_model/array_serializer.rb

Constant Summary

Constants included from Serializable

Serializable::INSTRUMENTATION_KEY

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Serializable

#as_json, included, #namespace, #serializable_data, #serializable_object_with_notification

Constructor Details

#initialize(object, options = {}) ⇒ ArraySerializer

Returns a new instance of ArraySerializer.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_model/array_serializer.rb', line 14

def initialize(object, options={})
  @object          = object
  @scope           = options[:scope]
  @root            = options.fetch(:root, self.class._root)
  @polymorphic     = options.fetch(:polymorphic, false)
  @meta_key        = options[:meta_key] || :meta
  @meta            = options[@meta_key]
  @each_serializer = options[:each_serializer]
  @resource_name   = options[:resource_name]
  @only            = options[:only] ? Array(options[:only]) : nil
  @except          = options[:except] ? Array(options[:except]) : nil
  @context         = options[:context]
  @namespace       = options[:namespace]
  @key_format      = options[:key_format] || options[:each_serializer].try(:key_format)
end

Class Attribute Details

._rootObject

Returns the value of attribute _root.



9
10
11
# File 'lib/active_model/array_serializer.rb', line 9

def _root
  @_root
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



29
30
31
# File 'lib/active_model/array_serializer.rb', line 29

def context
  @context
end

#key_formatObject

Returns the value of attribute key_format.



29
30
31
# File 'lib/active_model/array_serializer.rb', line 29

def key_format
  @key_format
end

#metaObject

Returns the value of attribute meta.



29
30
31
# File 'lib/active_model/array_serializer.rb', line 29

def meta
  @meta
end

#meta_keyObject

Returns the value of attribute meta_key.



29
30
31
# File 'lib/active_model/array_serializer.rb', line 29

def meta_key
  @meta_key
end

#objectObject

Returns the value of attribute object.



29
30
31
# File 'lib/active_model/array_serializer.rb', line 29

def object
  @object
end

#rootObject

Returns the value of attribute root.



29
30
31
# File 'lib/active_model/array_serializer.rb', line 29

def root
  @root
end

#scopeObject

Returns the value of attribute scope.



29
30
31
# File 'lib/active_model/array_serializer.rb', line 29

def scope
  @scope
end

Instance Method Details

#embedded_in_root_associationsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_model/array_serializer.rb', line 49

def embedded_in_root_associations
  @object.each_with_object({}) do |item, hash|
    serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
      next if !objects || objects.flatten.empty?

      if hash.has_key?(type)
        case hash[type] when Hash
          hash[type].deep_merge!(objects){ |key, old, new| (Array(old) + Array(new)).uniq }
        else
          hash[type].concat(objects).uniq!
        end
      else
        hash[type] = objects
      end
    end
  end
end

#json_keyObject



31
32
33
34
35
# File 'lib/active_model/array_serializer.rb', line 31

def json_key
  key = root.nil? ? @resource_name : root

  key_format == :lower_camel && key.present? ? key.camelize(:lower) : key
end

#serializable_object(options = {}) ⇒ Object Also known as: serializable_array



42
43
44
45
46
# File 'lib/active_model/array_serializer.rb', line 42

def serializable_object(options={})
  @object.map do |item|
    serializer_for(item).serializable_object_with_notification(options)
  end
end

#serializer_for(item) ⇒ Object



37
38
39
40
# File 'lib/active_model/array_serializer.rb', line 37

def serializer_for(item)
  serializer_class = @each_serializer || Serializer.serializer_for(item, namespace: @namespace) || DefaultSerializer
  serializer_class.new(item, scope: scope, key_format: key_format, context: @context, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
end