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



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

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

Class Method Details

.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



31
32
33
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 31

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