Class: Spree::Customers::Create

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

Overview

Creates a customer account — the single storefront registration flow (docs/plans/6.0-service-workflows.md decision 13).

Two callers today: store self-registration and the checkout "create an account" box, which passes order: — the customer is built from the order without a password (the account is claimed via password reset, like an admin-created one), the order's addresses become the account's defaults and the order is linked to the new account.

The validate hook is the veto seam for registration policy — bot/disposable-email screening, fraud checks, B2B approval. Handlers read the built (unsaved) customer plus order/created_by; staff bypass is a handler decision via created_by, the same pattern as return eligibility. Core sends no welcome email — signup email is host-app code on the user.created event or the after_create hook.

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

#customer ⇒ Object (readonly)

The customer record — built (unsaved) while :validate runs.



22
23
24
# File 'app/workflows/spree/customers/create.rb', line 22

def customer
  @customer
end

Instance Method Details

#perform(store:, email: nil, password: nil, password_confirmation: nil, first_name: nil, last_name: nil, phone: nil, accepts_email_marketing: nil, metadata: nil, order: nil, password_required: nil, created_by: nil, terms_of_service: nil, ip_address: nil, user_agent: nil) ⇒ Object

Parameters:

  • store (Spree::Store) —

    the store the registration happens on; exposed to hook handlers and scopes the newsletter-subscriber link

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

    defaults to the order email with order:

  • password (String, nil) (defaults to: nil)
  • password_confirmation (String, nil) (defaults to: nil)
  • first_name (String, nil) (defaults to: nil) —

    defaults from the order addresses with order:, as do last_name and phone

  • last_name (String, nil) (defaults to: nil)
  • phone (String, nil) (defaults to: nil)
  • accepts_email_marketing (Boolean, nil) (defaults to: nil) —

    defaults from the order's marketing opt-in with order:

  • metadata (Hash, nil) (defaults to: nil)
  • order (Spree::Order, nil) (defaults to: nil) —

    a placed order adopting the new account; when its email already belongs to a customer, the order is linked to that account and no new customer is created

  • password_required (Boolean, nil) (defaults to: nil) —

    whether a missing password fails the registration (the account is created password-less otherwise and claimed via password reset). Defaults to true except with order: — guest checkout collects no password, while self-registration must, because the caller issues a JWT on success

  • terms_of_service (Boolean, nil) (defaults to: nil) —

    whether the person ticked the storefront's terms box. Only a true value records a terms consent row — absent means "not asked", which is not agreement

  • created_by (Object, nil) (defaults to: nil) —

    the staff actor; nil for storefront self-registration



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
# File 'app/workflows/spree/customers/create.rb', line 49

def perform(store:, email: nil, password: nil, password_confirmation: nil,
            first_name: nil, last_name: nil, phone: nil,
            accepts_email_marketing: nil, metadata: nil,
            order: nil, password_required: nil, created_by: nil,
            terms_of_service: nil, ip_address: nil, user_agent: nil)
  super

  step :adopt_existing_customer
  step :build_customer
  step :ensure_password

  # Veto point — registration policy. The customer is built but not
  # yet persisted.
  run_hooks :validate

  ApplicationRecord.transaction do
    step :create_customer
    step :link_order
    step :record_consent
  end

  step :link_newsletter_subscriber
  run_hooks :after_create
  success(order ? customer.reload : customer)
end