Class: Spree::AgentTools::CreateResource

Inherits:
ResourceWrite show all
Defined in:
app/tools/spree/agent_tools/create_resource.rb

Overview

Creates a record, through the Admin API's own create operation.

Constant Summary

Constants inherited from Spree::AgentTool

Spree::AgentTool::PARENT_ID_DESCRIPTION

Instance Attribute Summary

Attributes inherited from Spree::AgentTool

#context

Instance Method Summary collapse

Methods inherited from ResourceWrite

#permitted?

Methods inherited from Spree::AgentTool

additive!, audience, description, destructive?, #initialize, input_schema, mutating!, mutating?, param, params, permission, #permitted?, tool_name, #unknown_resource

Constructor Details

This class inherits a constructor from Spree::AgentTool

Instance Method Details

#call(resource:, attributes: {}, parent_id: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/tools/spree/agent_tools/create_resource.rb', line 18

def call(resource:, attributes: {}, parent_id: nil)
  entry, refusal = writable_entry(resource, :create)
  return refusal if refusal

  body, rejection = body_for(entry, attributes)
  return rejection if rejection

  path, missing = write_path(entry, :create, parent_id: parent_id)
  return missing if missing

  response = dispatch.call(method: :post, path: path, body: body)

  result_for(response, entry, summary: summary(resource: resource, attributes: attributes, parent_id: parent_id))
end

#summary(arguments) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/tools/spree/agent_tools/create_resource.rb', line 33

def summary(arguments)
  named = arguments[:attributes].to_h.values_at('name', :name, 'number', :number).compact.first

  line = "Create #{arguments[:resource].to_s.singularize.tr('_', ' ')} #{named}".strip

  # A top-level resource ignores parent_id, so the approval card does too.
  nested = arguments[:parent_id].present? && ResourceMap.find(arguments[:resource])&.parent_param(:create)
  nested ? "#{line} under #{arguments[:parent_id]}" : line
end