Module: Spree::Purchase::CheckoutSteps

Included in:
Cart
Defined in:
app/models/concerns/spree/purchase/checkout_steps.rb

Overview

Data-driven checkout-step introspection for Spree::Cart — computed from what the cart actually requires (Spree::Checkout::Requirements), never from a state column. Cart-only: an order is past checkout by definition. The payment predicates the step list reads (payment_required?/confirmation_required?) live in PaymentProcessing.

Instance Method Summary collapse

Instance Method Details

#checkout_step_index(step) ⇒ Integer

Returns position of the step, 0 when unknown.

Parameters:

  • step (String)

Returns:

  • (Integer) —

    position of the step, 0 when unknown



27
28
29
# File 'app/models/concerns/spree/purchase/checkout_steps.rb', line 27

def checkout_step_index(step)
  checkout_steps.index(step).to_i
end

#checkout_steps ⇒ Array<String>

The Registry owns the step vocabulary and applicability — built-in and registered custom steps alike (see Spree::Checkout::Registry.base_steps / .register_step).

Returns:

  • (Array<String>)


15
16
17
# File 'app/models/concerns/spree/purchase/checkout_steps.rb', line 15

def checkout_steps
  Spree::Checkout::Registry.step_names_for(self)
end

#completed_checkout_steps ⇒ Array<String>

Checkout steps before #current_checkout_step; never includes 'complete'.

Returns:

  • (Array<String>)


59
60
61
62
63
64
65
# File 'app/models/concerns/spree/purchase/checkout_steps.rb', line 59

def completed_checkout_steps
  steps = checkout_steps.reject { |step| step == 'complete' }
  return steps if current_checkout_step == 'complete'

  index = steps.index(current_checkout_step) || 0
  steps.first(index)
end

#current_checkout_step ⇒ String

Customer-facing checkout step. Completed carts are past checkout; open ones report the first unmet requirement's step ('cart' — missing line items — is not customer-facing and reports as 'address').

Only a completed cart reports 'complete'. An open cart with nothing outstanding is not finished, it is ready to be placed, so it waits on #final_checkout_step

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
# File 'app/models/concerns/spree/purchase/checkout_steps.rb', line 40

def current_checkout_step
  return 'complete' if completed?

  first_unmet = Spree::Checkout::Requirements.new(self).call.first
  return final_checkout_step if first_unmet.nil?

  step = first_unmet[:step].to_s
  step == 'cart' ? 'address' : step
end

#final_checkout_step ⇒ String

Returns:

  • (String)


51
52
53
# File 'app/models/concerns/spree/purchase/checkout_steps.rb', line 51

def final_checkout_step
  checkout_steps.reject { |step| step == 'complete' }.last || 'address'
end

#has_checkout_step?(step) ⇒ Boolean

Parameters:

  • step (String, Symbol)

Returns:

  • (Boolean)


21
22
23
# File 'app/models/concerns/spree/purchase/checkout_steps.rb', line 21

def has_checkout_step?(step)
  step.present? && checkout_steps.include?(step.to_s)
end