Class: ActiveModel::Serializer::CollectionSerializer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_model/serializer/collection_serializer.rb

Direct Known Subclasses

ArraySerializer

Defined Under Namespace

Classes: CannotInferRootKeyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources, options = {}) ⇒ CollectionSerializer

Returns a new instance of CollectionSerializer.



11
12
13
14
15
16
# File 'lib/active_model/serializer/collection_serializer.rb', line 11

def initialize(resources, options = {})
  @object                  = resources
  @options                 = options
  @root                    = options[:root]
  @serializers             = serializers_from_resources
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#json_keyObject

TODO: unify naming of root, json_key, and _type. Right now, a serializer’s json_key comes from the root option or the object’s model name, by default. But, if a dev defines a custom ‘json_key` method with an explicit value, we have no simple way to know that it is safe to call that instance method. (which is really a class property at this point, anyhow). rubocop:disable Metrics/CyclomaticComplexity Disabling cop since it’s good to highlight the complexity of this method by including all the logic right here.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_model/serializer/collection_serializer.rb', line 39

def json_key
  return root if root
  # 1. get from options[:serializer] for empty resource collection
  key = object.empty? &&
    (explicit_serializer_class = options[:serializer]) &&
    explicit_serializer_class._type
  # 2. get from first serializer instance in collection
  key ||= (serializer = serializers.first) && serializer.json_key
  # 3. get from collection name, if a named collection
  key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
  # 4. key may be nil for empty collection and no serializer option
  key &&= key.pluralize
  if raise_cannot_infer_root_key_error?
    # 5. fail if the key cannot be determined
    key || fail(CannotInferRootKeyError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
  end
  key
end

#paginated?Boolean

rubocop:enable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/active_model/serializer/collection_serializer.rb', line 59

def paginated?
  ActiveModelSerializers.config.jsonapi_pagination_links_enabled &&
    object.respond_to?(:current_page) &&
    object.respond_to?(:total_pages) &&
    object.respond_to?(:size)
end

#serializable_hash(adapter_options, options, adapter_instance) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
26
27
28
29
# File 'lib/active_model/serializer/collection_serializer.rb', line 23

def serializable_hash(adapter_options, options, adapter_instance)
  options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
  options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, options[:include_directive])
  serializers.map do |serializer|
    serializer.serializable_hash(adapter_options, options, adapter_instance)
  end
end

#success?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/active_model/serializer/collection_serializer.rb', line 18

def success?
  true
end