class JsonApiServer::ResourcesSerializer

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)

Attributes

objects_serializer[R]
filter[R]

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.

paginator[R]

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.

Public Class Methods

new(objects, **options) click to toggle source
Calls superclass method JsonApiServer::ResourceSerializer::new
# 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
serializer(klass) click to toggle source

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

# File lib/json_api_server/resources_serializer.rb, line 78
def serializer(klass)
  @objects_serializer = klass
end

Public Instance Methods

data() click to toggle source

Subclasses override for customized behaviour.

# 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
included() click to toggle source

Subclasses override for customized behaviour.

# 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
relationship_data() click to toggle source

Subclasses override for customized behaviour.

# 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

Protected Instance Methods

initalize_objects(objects) click to toggle source
# File lib/json_api_server/resources_serializer.rb, line 122
def initalize_objects(objects)
  klass = self.class.objects_serializer
  if klass && objects.respond_to?(:map)
    objects.map { |object| klass.new(object, includes: includes, fields: fields) }
  else
    objects
  end
end