class ActiveModelSerializers::Adapter::JsonApi::ResourceIdentifier

Attributes

id[R]
type[R]

Public Class Methods

for_type_with_id(type, id, options) click to toggle source
# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 12
def self.for_type_with_id(type, id, options)
  type = inflect_type(type)
  type = type_for(:no_class_needed, type, options)
  if id.blank?
    { type: type }
  else
    { id: id.to_s, type: type }
  end
end
inflect_type(type) click to toggle source
# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 31
def self.inflect_type(type)
  singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
  inflection = singularize ? :singularize : :pluralize
  ActiveSupport::Inflector.public_send(inflection, type)
end
new(serializer, options) click to toggle source

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

# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 38
def initialize(serializer, options)
  @id   = id_for(serializer)
  @type = type_for(serializer, options)
end
raw_type_from_serializer_object(object) click to toggle source
# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 22
def self.raw_type_from_serializer_object(object)
  class_name = object.class.name # should use model_name
  raw_type = class_name.underscore
  raw_type = inflect_type(raw_type)
  raw_type
    .gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
  raw_type
end
type_for(serializer, serializer_type = nil, transform_options = {}) click to toggle source
# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 7
def self.type_for(serializer, serializer_type = nil, transform_options = {})
  raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object)
  JsonApi.send(:transform_key_casing!, raw_type, transform_options)
end

Public Instance Methods

as_json() click to toggle source
# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 43
def as_json
  if id.blank?
    { type: type }
  else
    { id: id.to_s, type: type }
  end
end

Private Instance Methods

id_for(serializer) click to toggle source
# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 62
def id_for(serializer)
  serializer.read_attribute_for_serialization(:id).to_s
end
type_for(serializer, transform_options) click to toggle source
# File lib/active_model_serializers/adapter/json_api/resource_identifier.rb, line 57
def type_for(serializer, transform_options)
  serializer_type = serializer._type
  self.class.type_for(serializer, serializer_type, transform_options)
end