Class: JsonApi::Parameters::Handlers::DefaultHandlers::ToOneRelationHandler

Inherits:
BaseHandler
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
lib/jsonapi_parameters/default_handlers/to_one_relation_handler.rb

Instance Attribute Summary

Attributes inherited from BaseHandler

#included, #relationship_key, #relationship_value

Instance Method Summary collapse

Methods inherited from BaseHandler

call, #find_included_object, #initialize

Constructor Details

This class inherits a constructor from JsonApi::Parameters::Handlers::DefaultHandlers::BaseHandler

Instance Method Details

#handleObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jsonapi_parameters/default_handlers/to_one_relation_handler.rb', line 8

def handle
  related_id = relationship_value.dig(:id)
  related_type = relationship_value.dig(:type)

  included_object = find_included_object(
    related_id: related_id, related_type: related_type
  ) || {}

  return ["#{singularize(relationship_key)}_id".to_sym, related_id] if included_object.empty?

  included_object.delete(:type)
  included_object = included_object[:attributes].merge(id: related_id)
  ["#{singularize(relationship_key)}_attributes".to_sym, included_object]
end