Class: Caprese::Adapter::JsonApi::ResourceIdentifier

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, options) ⇒ ResourceIdentifier



28
29
30
31
# 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

Class Method Details

.for_type_with_id(type, id, options) ⇒ Object



19
20
21
22
23
24
25
# 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

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



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Method Details

#as_jsonObject



33
34
35
# File 'lib/caprese/adapter/json_api/resource_identifier.rb', line 33

def as_json
  { id: id, type: type }
end