class Caprese::Adapter::JsonApi::ResourceIdentifier

Attributes

id[R]
type[R]

Public Class Methods

for_type_with_id(type, id, options) click to toggle source
# File lib/caprese/adapter/json_api/resource_identifier.rb, line 19
def self.for_type_with_id(type, id, options)
  return nil if id.blank?
  {
    id: id.to_s,
    type: type_for(:no_class_needed, type, options)
  }
end
new(serializer, options) click to toggle source

{jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}

# File lib/caprese/adapter/json_api/resource_identifier.rb, line 28
def initialize(serializer, options)
  @id   = id_for(serializer)
  @type = type_for(serializer, options)
end
type_for(class_name, serializer_type = nil, transform_options = {}) click to toggle source
# File lib/caprese/adapter/json_api/resource_identifier.rb, line 5
def self.type_for(class_name, serializer_type = nil, transform_options = {})
  inflection =
    if ActiveModelSerializers.config.jsonapi_resource_type == :singular
      :singularize
    else
      :pluralize
    end

  raw_type = serializer_type || class_name.underscore
  raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)

  JsonApi.send(:transform_key_casing!, raw_type, transform_options)
end

Public Instance Methods

as_json() click to toggle source
# File lib/caprese/adapter/json_api/resource_identifier.rb, line 33
def as_json
  { id: id, type: type }
end

Private Instance Methods

id_for(serializer) click to toggle source
# File lib/caprese/adapter/json_api/resource_identifier.rb, line 47
def id_for(serializer)
  serializer.read_attribute_for_serialization(Caprese.config.resource_primary_key).to_s
end
type_for(serializer, transform_options) click to toggle source
# File lib/caprese/adapter/json_api/resource_identifier.rb, line 43
def type_for(serializer, transform_options)
  self.class.type_for(serializer.object.class.name, serializer.json_key, transform_options)
end