Class: Spree::Checkout::Requirement

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

Overview

Value object representing a single additional requirement registered via Registry.

Unlike Step, a Requirement attaches extra validation to an existing checkout step rather than introducing a new step.

Examples:

Require a PO number for B2B orders at the payment step

Spree::Checkout::Registry.add_requirement(
  step: :payment,
  field: :po_number,
  message: 'PO number is required for business accounts',
  satisfied: ->(order) { order.po_number.present? },
  applicable: ->(order) { order.&.business? }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step:, field:, message:, satisfied:, applicable: ->(_) { true }) ⇒ Requirement

Returns a new instance of Requirement.

Parameters:

  • step (String, Symbol)

    checkout step this requirement belongs to

  • field (String, Symbol)

    field identifier

  • message (String)

    human-readable validation message

  • satisfied (Proc)

    lambda accepting an order, returns true when met

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

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



32
33
34
35
36
37
38
# File 'app/models/spree/checkout/requirement.rb', line 32

def initialize(step:, field:, message:, satisfied:, applicable: ->(_) { true })
  @step = step.to_s
  @field = field.to_s
  @message = message
  @satisfied_proc = satisfied
  @applicable_proc = applicable
end

Instance Attribute Details

#fieldString (readonly)

Returns field identifier (e.g. “po_number”, “tax_id”).

Returns:

  • (String)

    field identifier (e.g. “po_number”, “tax_id”)



21
22
23
# File 'app/models/spree/checkout/requirement.rb', line 21

def field
  @field
end

#messageString (readonly)

Returns human-readable message shown when the requirement is not met.

Returns:

  • (String)

    human-readable message shown when the requirement is not met



24
25
26
# File 'app/models/spree/checkout/requirement.rb', line 24

def message
  @message
end

#stepString (readonly)

Returns checkout step this requirement belongs to.

Returns:

  • (String)

    checkout step this requirement belongs to



18
19
20
# File 'app/models/spree/checkout/requirement.rb', line 18

def step
  @step
end

Instance Method Details

#applicable?(order) ⇒ Boolean

Returns whether this requirement applies to the given order.

Parameters:

Returns:

  • (Boolean)

    whether this requirement applies to the given order



46
# File 'app/models/spree/checkout/requirement.rb', line 46

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

#satisfied?(order) ⇒ Boolean

Returns whether the requirement has been met.

Parameters:

Returns:

  • (Boolean)

    whether the requirement has been met



42
# File 'app/models/spree/checkout/requirement.rb', line 42

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