class OrderWalkthrough

Public Class Methods

up_to(state) click to toggle source
# File lib/spree/testing_support/order_walkthrough.rb, line 2
def self.up_to(state)
  store = if Spree::Store.exists?
            # Ensure the default store is used
            Spree::Store.default || FactoryBot.create(:store, default: true)
          else
            # Create a default store
            FactoryBot.create(:store, default: true)
          end

  # A payment method must exist for an order to proceed through the Address state
  unless Spree::PaymentMethod.exists?
    FactoryBot.create(:check_payment_method, stores: [store])
  end

  # Need to create a valid zone too...
  zone = FactoryBot.create(:zone)
  country = FactoryBot.create(:country)
  zone.members << Spree::ZoneMember.create(zoneable: country)
  country.states << FactoryBot.create(:state, country: country)

  # A shipping method must exist for rates to be displayed on checkout page
  unless Spree::ShippingMethod.exists?
    FactoryBot.create(:shipping_method).tap do |sm|
      sm.calculator.preferred_amount = 10
      sm.calculator.preferred_currency = store.default_currency
      sm.calculator.save
    end
  end

  order = Spree::Order.create!(email: 'spree@example.com')
  add_line_item!(order)
  order.next!

  end_state_position = states.index(state.to_sym)
  states[0...end_state_position].each do |state|
    send(state, order)
  end

  order
end

Private Class Methods

add_line_item!(order) click to toggle source
# File lib/spree/testing_support/order_walkthrough.rb, line 45
def self.add_line_item!(order)
  FactoryBot.create(:line_item, order: order)
  order.reload
end
address(order) click to toggle source
# File lib/spree/testing_support/order_walkthrough.rb, line 50
def self.address(order)
  order.bill_address = FactoryBot.create(:address, country_id: Spree::Zone.global.members.first.zoneable.id)
  order.ship_address = FactoryBot.create(:address, country_id: Spree::Zone.global.members.first.zoneable.id)
  order.next!
end
complete(_order) click to toggle source
# File lib/spree/testing_support/order_walkthrough.rb, line 71
def self.complete(_order)
  # noop?
end
delivery(order) click to toggle source
# File lib/spree/testing_support/order_walkthrough.rb, line 56
def self.delivery(order)
  order.next!
end
payment(order) click to toggle source
# File lib/spree/testing_support/order_walkthrough.rb, line 60
def self.payment(order)
  FactoryBot.create :payment,
    order: order,
    payment_method: Spree::PaymentMethod.first,
    amount: order.total

  # TODO: maybe look at some way of making this payment_state change automatic
  order.payment_state = 'paid'
  order.next!
end
states() click to toggle source
# File lib/spree/testing_support/order_walkthrough.rb, line 75
def self.states
  [:address, :delivery, :payment, :complete]
end