Module: Unit::ResourceOperations::Create::ClassMethods

Defined in:
lib/unit-ruby/util/resource_operations.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/unit-ruby/util/resource_operations.rb', line 23

def create(attributes)
  id = attributes.fetch(:id, nil)
  resource = new(attributes.except(:id))

  data = {
    type: resource.resource_type,
    attributes: resource.as_json_api.slice(*resource.dirty_attributes)
  }
  unless resource.relationships.empty?
    data[:relationships] =
      resource.relationships
  end

  created_resource = connection.post(resources_path(id), { data: data })

  build_resource_from_json_api(created_resource)
end