Class: JsonApiServer::ResourcesSerializer

Inherits:
ResourceSerializer show all
Defined in:
lib/json_api_server/resources_serializer.rb

Overview

Description

Serializer for a collection/array of resources. Inherits from JsonApiServer::ResourceSerializer.

Example

Given a resource serializer for Topic:

class TopicSerializer < JsonApiServer::ResourceSerializer
 resource_type 'topics'

  def links
    { self: File.join(base_url, "/topics/#{@object.id}") }
  end

  def data
    {
      type: self.class.type,
      id: @object.id,
      attributes: attributes
    }
  end

  protected

  def attributes
    attributes_builder
      .add('book', @object.book)
      .add('author', @object.author)
      .add('quote', @object.quote)
      .add('character', @object.character)
      .add('location', @object.location)
      .add('published', @object.published)
      .add('created_at', @object.created_at.try(:iso8601, 0))
      .add('updated_at', @object.updated_at.try(:iso8601, 0))
      .attributes
  end
end

Create a Topics serializer like so:

class TopicsSerializer < JsonApiServer::ResourcesSerializer
  serializer TopicSerializer
end

Create an instance from builder:

builder = JsonApiServer::Builder.new(request, Topic.all)
  .add_pagination(pagination_options)
  .add_filter(filter_options)
  .add_sort(sort_options)
  .add_include(include_options)
  .add_fields

# populates links with pagination info, merges data from each
# Topic serializer instance.
serializer = TopicsSerializer.from_builder(builder)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from ResourceSerializer

#fields, #includes

Attributes inherited from BaseSerializer

#as_json_options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ResourceSerializer

from_builder, resource_type, type

Methods inherited from BaseSerializer

#as_json, #meta

Methods included from ApiVersion

#jsonapi

Methods included from Serializer

#as_json, #serializer_options, #to_json

Constructor Details

#initialize(objects, **options) ⇒ ResourcesSerializer

  • objects - An array of objects. If #serializer is specified, the objects will be converted to this class.

  • options - Hash:

    • filter - Instance of JsonApiServer::Filter or nil. Sets #filter.

    • :paginator - Instance of JsonApiServer::Fields or nil. Sets #paginator.

    • :as_json_options - See options at JsonApiServer::BaseSerializer#as_json_options.



89
90
91
92
93
94
95
# File 'lib/json_api_server/resources_serializer.rb', line 89

def initialize(objects, **options)
  super(nil, options)
  remove_instance_variable(:@object)
  @paginator = options[:paginator]
  @filter = options[:filter]
  @objects = initalize_objects(objects)
end

Class Attribute Details

.objects_serializerObject (readonly)

Returns the value of attribute objects_serializer.



74
75
76
# File 'lib/json_api_server/resources_serializer.rb', line 74

def objects_serializer
  @objects_serializer
end

Instance Attribute Details

#filterObject (readonly)

Instance of JsonApiServer::Filter or nil (default). Based on filter params. Extracted via JsonApiServer::Filter and available through JsonApiServer::Builder#filter. Set in initializer options.



71
72
73
# File 'lib/json_api_server/resources_serializer.rb', line 71

def filter
  @filter
end

#paginatorObject (readonly)

Instance of JsonApiServer::Paginator or nil (default). Based on pagination params. Extracted via JsonApiServer::Pagination and available through JsonApiServer::Builder#paginator. Set in initializer options.



66
67
68
# File 'lib/json_api_server/resources_serializer.rb', line 66

def paginator
  @paginator
end

Class Method Details

.serializer(klass) ⇒ Object

A serializer class. If set,‘objects’ will be converted to instances of this serializer.



78
79
80
# File 'lib/json_api_server/resources_serializer.rb', line 78

def serializer(klass)
  @objects_serializer = klass
end

Instance Method Details

#dataObject

Subclasses override for customized behaviour.



102
103
104
105
# File 'lib/json_api_server/resources_serializer.rb', line 102

def data
  data = @objects.try(:map) { |o| o.try(:data) }
  data.try(:compact!) || data
end

#includedObject

Subclasses override for customized behaviour.



114
115
116
117
118
# File 'lib/json_api_server/resources_serializer.rb', line 114

def included
  included = @objects.try(:map) { |o| o.try(:included) }
  included.try(:flatten!)
  included.try(:compact!) || included
end


97
98
99
# File 'lib/json_api_server/resources_serializer.rb', line 97

def links
  @paginator.try(:as_json) || {}
end

#relationship_dataObject

Subclasses override for customized behaviour.



108
109
110
111
# File 'lib/json_api_server/resources_serializer.rb', line 108

def relationship_data
  data = @objects.try(:map) { |o| o.try(:relationship_data) }
  data.try(:compact!) || data
end