Class: Spree::AgentTools::ProposeImportMapping
- Inherits:
-
ImportTool
- Object
- Spree::AgentTool
- ImportTool
- Spree::AgentTools::ProposeImportMapping
- Defined in:
- app/tools/spree/agent_tools/propose_import_mapping.rb
Overview
Applies a column mapping to an import, and starts it if the mapping is complete.
Mutating, and deliberately so. Mapping decides what every row in the
file becomes: get "Cost" onto price instead of cost_price and a
merchant has repriced their whole catalogue in one click. The merchant
sees the proposed pairing and approves it before anything is written.
Constant Summary
Constants inherited from Spree::AgentTool
Spree::AgentTool::PARENT_ID_DESCRIPTION
Instance Attribute Summary
Attributes inherited from Spree::AgentTool
Instance Method Summary collapse
- #call(id:, mapping:) ⇒ Object
-
#summary(arguments) ⇒ Object
The merchant judges the pairing itself, so the card spells it out rather than saying "apply a mapping".
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(id:, mapping:) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/tools/spree/agent_tools/propose_import_mapping.rb', line 30 def call(id:, mapping:) import = find_import(id) return { error: "No import found for #{id.inspect}" } if import.nil? # Per-import, because a customer import needs `write_customers` # while a product import needs `write_products`. required = "write_#{import.class.required_scope}" unless context.holds?(required) return { error: "You do not have permission to run a #{import_kind(import)} import." } end refusal = (:update, import) return refusal if refusal return { error: 'This import is not waiting to be mapped.' } unless import.mapping? pairs = sanitize(import, mapping) return unknown_fields(import, mapping) if pairs.empty? # A pair naming a field or column that does not exist is dropped here # rather than refusing the whole proposal — the valid half is usually # most of it, and a model that misspelled one heading should not have # to resend forty. It is named back, so the model can correct that one # and never believes it was applied. rejected = mapping.to_h.keys.map(&:to_s) - pairs.keys applied = apply!(import, pairs) start_if_ready(import) { ok: true, id: import.prefixed_id, # Only what was actually written. Reporting a field as mapped when # its mapping row did not exist would have the model tell the # merchant a column was handled when it was not. mapped: applied, # Named back so the model knows exactly what did not land, and why: # `rejected` did not exist on this import, `unmapped` exists but has # no mapping row to write to. rejected: rejected.presence, unmapped: (pairs.keys - applied.keys).presence, status: import.reload.status, # `mapped_fields` returns mapping records, not names. still_missing: import.required_fields - import.mappings.mapped.pluck(:schema_field) } end |
#summary(arguments) ⇒ Object
The merchant judges the pairing itself, so the card spells it out rather than saying "apply a mapping".
79 80 81 82 83 84 |
# File 'app/tools/spree/agent_tools/propose_import_mapping.rb', line 79 def summary(arguments) pairs = (arguments[:mapping] || {}).map { |field, column| "#{column} → #{field}" } return 'Apply an import mapping' if pairs.empty? "Map #{pairs.join(', ')}" end |