Sha256: 43a50322011da8b02e57113d34573ec894dfedde54e0bf67240da4f1ac481df7

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

require "active_model_serializers"

# 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" }
#
class FieldSerializer < ActiveModel::Serializer
  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

  private

  def adding_id(&block)
    block.call.merge(id: object.id)
  end

  def create_attribute_structure(attribute_stack, model)
    parent = attribute_stack.shift
    if attribute_stack.count > 0
      nested_model = model.send(parent)
      { parent => create_attribute_structure(attribute_stack, nested_model) }
    else
      { parent => model.send(parent) }
    end
  end

  def merging_attributes(&block)
    block.call.inject(:deep_merge!)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fields-serializer-0.2.3 lib/fields/serializer/field_serializer.rb
fields-serializer-0.2.2 lib/fields/serializer/field_serializer.rb
fields-serializer-0.2.1 lib/fields/serializer/field_serializer.rb
fields-serializer-0.2.0 lib/fields/serializer/field_serializer.rb