Class: Spree::Variants::Create

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

Overview

Adds one variant to a product that already exists. A product being created carries its variants in the same write instead, which is one request and reconciles the list rather than adding to it.

The single gate for variant creation — the standalone variants endpoint and the nested payload on a product write both run through it, so a :validate handler sees every variant a catalog gains.

The product is a parameter rather than something read off the payload because everything nested needs it: an option value attaches an option type to the product, and a stock location is resolved through the product's store. The model defers those writes when it is built without an owner; a caller of this workflow has already named one, so they apply directly.

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 ServiceModule::Base

#call, prepended

Methods included from Workflow::ControlFlow

#failure

Instance Attribute Details

#variant ⇒ Object (readonly)

The unsaved record a :validate handler reads.



23
24
25
# File 'app/workflows/spree/variants/create.rb', line 23

def variant
  @variant
end

Instance Method Details

#perform(product:, attributes: {}) ⇒ Spree::ServiceModule::Result

Returns value is the variant.

Parameters:

  • product (Spree::Product)
  • attributes (Hash) (defaults to: {}) —

    may carry options, prices, stock_levels

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/workflows/spree/variants/create.rb', line 28

def perform(product:, attributes: {})
  super

  step :build_variant
  run_hooks :validate

  ApplicationRecord.transaction do
    step :save_variant
    step :apply_nested_attributes
    run_hooks :after_create
  end

  success(variant)
end