Class: Spree::StockTransfers::Create

Inherits:
Workflow
  • Object
show all
Defined in:
app/workflows/spree/stock_transfers/create.rb

Overview

Plans a transfer. Nothing moves: a draft is a packing list the merchant can still edit, and stock only leaves the source warehouse when MarkInTransit runs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Workflow

#call, define_argument_readers, #errors, #halt!, hooks, inherited, model_name, #read_attribute_for_validation, #reject!, workflow_key

Methods included from Spree::ServiceModule::Base

#call, prepended

Methods included from Workflow::ControlFlow

#failure

Instance Attribute Details

#stock_transfer ⇒ Object (readonly)

The created transfer — hook handlers read it (nil while :validate runs).



10
11
12
# File 'app/workflows/spree/stock_transfers/create.rb', line 10

def stock_transfer
  @stock_transfer
end

Instance Method Details

#perform(store:, source_location:, destination_location:, items: [], reference: nil, notes: nil, metadata: nil, created_by: nil) ⇒ Object

Parameters:

  • store (Spree::Store)
  • source_location (Spree::StockLocation) —

    where the goods leave from

  • destination_location (Spree::StockLocation) —

    where they are going

  • items (Array<Hash>) (defaults to: []) —

    [{ variant:, quantity_shipped: }]; a draft may legitimately open empty and gain lines as the merchant packs

  • reference (String, nil) (defaults to: nil) —

    the merchant's own label for the trip

  • notes (String, nil) (defaults to: nil)
  • metadata (Hash, nil) (defaults to: nil)
  • created_by (Object, nil) (defaults to: nil)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/workflows/spree/stock_transfers/create.rb', line 21

def perform(store:, source_location:, destination_location:, items: [],
            reference: nil, notes: nil, metadata: nil, created_by: nil)
  super

  step :build_transfer
  run_hooks :validate

  ApplicationRecord.transaction do
    step :save_transfer
  end

  run_hooks :after_create
  # No `publish_event` here: `publishes_lifecycle_events` on the model
  # already emits `stock_transfer.created` after commit, and publishing
  # it again fires every subscriber and webhook twice.
  success(stock_transfer.reload)
end