Class: ActiveModelSerializers::Adapter::JsonApi::ResourceIdentifier

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers/adapter/json_api/resource_identifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, options) ⇒ ResourceIdentifier


34
35
36
37
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 34

def initialize(serializer, options)
  @id   = id_for(serializer)
  @type = type_for(serializer, options)
end

Class Method Details

.for_type_with_id(type, id, options) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 25

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

.type_for(class_name, serializer_type = nil, transform_options = {}) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 5

def self.type_for(class_name, serializer_type = nil, transform_options = {})
  if serializer_type
    raw_type = serializer_type
  else
    inflection =
      if ActiveModelSerializers.config.jsonapi_resource_type == :singular
        :singularize
      else
        :pluralize
      end

    raw_type = class_name.underscore
    raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)
    raw_type
      .gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
    raw_type
  end
  JsonApi.send(:transform_key_casing!, raw_type, transform_options)
end

Instance Method Details

#as_jsonObject


39
40
41
42
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 39

def as_json
  return nil if id.blank?
  { id: id, type: type }
end