Class: Spree::Checkout::Step

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/checkout/step.rb

Overview

Value object representing a custom checkout step registered via Registry.

Examples:

Register a loyalty step before payment

Spree::Checkout::Registry.register_step(
  name: :loyalty,
  before: :payment,
  satisfied: ->(order) { order.loyalty_verified? },
  requirements: ->(order) { [{ step: 'loyalty', field: 'loyalty_number', message: 'Enter loyalty number' }] }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, satisfied:, requirements:, applicable: ->(_) { true }, after: nil, before: nil) ⇒ Step

Returns a new instance of Step.

Parameters:

  • name (String, Symbol)

    unique step identifier

  • satisfied (Proc)

    lambda accepting an order, returns true when the step is complete

  • requirements (Proc)

    lambda accepting an order, returns Array of requirement hashes (+{ step:, field:, message: }+) describing what is still needed

  • applicable (Proc) (defaults to: ->(_) { true })

    lambda accepting an order, returns true when this step applies (defaults to always applicable)

  • after (String, Symbol, nil) (defaults to: nil)

    place this step after the named checkout step

  • before (String, Symbol, nil) (defaults to: nil)

    place this step before the named checkout step



30
31
32
33
34
35
36
37
# File 'app/models/spree/checkout/step.rb', line 30

def initialize(name:, satisfied:, requirements:, applicable: ->(_) { true }, after: nil, before: nil)
  @name = name.to_s
  @after = after&.to_s
  @before = before&.to_s
  @satisfied_proc = satisfied
  @requirements_proc = requirements
  @applicable_proc = applicable
end

Instance Attribute Details

#afterString? (readonly)

Returns name of the checkout step this should be placed after.

Returns:

  • (String, nil)

    name of the checkout step this should be placed after



17
18
19
# File 'app/models/spree/checkout/step.rb', line 17

def after
  @after
end

#beforeString? (readonly)

Returns name of the checkout step this should be placed before.

Returns:

  • (String, nil)

    name of the checkout step this should be placed before



20
21
22
# File 'app/models/spree/checkout/step.rb', line 20

def before
  @before
end

#nameString (readonly)

Returns step name.

Returns:

  • (String)

    step name



14
15
16
# File 'app/models/spree/checkout/step.rb', line 14

def name
  @name
end

Instance Method Details

#applicable?(order) ⇒ Boolean

Returns whether this step applies to the given order.

Parameters:

Returns:

  • (Boolean)

    whether this step applies to the given order



49
# File 'app/models/spree/checkout/step.rb', line 49

def applicable?(order) = @applicable_proc.call(order)

#requirements(order) ⇒ Array<Hash{Symbol => String}>

Returns outstanding requirement hashes (+{ step:, field:, message: }+).

Parameters:

Returns:

  • (Array<Hash{Symbol => String}>)

    outstanding requirement hashes (+{ step:, field:, message: }+)



45
# File 'app/models/spree/checkout/step.rb', line 45

def requirements(order) = @requirements_proc.call(order)

#satisfied?(order) ⇒ Boolean

Returns whether the step’s conditions have been met.

Parameters:

Returns:

  • (Boolean)

    whether the step’s conditions have been met



41
# File 'app/models/spree/checkout/step.rb', line 41

def satisfied?(order) = @satisfied_proc.call(order)