Class: FieldSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
lib/fields/serializer/field_serializer.rb

Overview

This is a generic serializer intended to return a subset of a model’s attributes. It can be used with any model but does not currently support associations.

Example usage:

render json: @region, serializer: FieldSerializer, fields: [:id, :title]

> { "id": "5f19582d-ee28-4e89-9e3a-edc42a8b59e5", "title": "London" }

Instance Method Summary collapse

Instance Method Details

#attributes(*args) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/fields/serializer/field_serializer.rb', line 12

def attributes(*args)
  fields = Array(args.first).map { |str| str.to_s.split(",").map(&:strip) }.flatten
  adding_id do
    merging_attributes do
      fields.map { |field| create_attribute_structure(field.split("."), object) }
    end
  end
end