Class: OrderWalkthrough

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/testing_support/order_walkthrough.rb

Class Method Summary collapse

Class Method Details

.up_to(state, store = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spree/testing_support/order_walkthrough.rb', line 2

def self.up_to(state, store = nil)
  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 = store.orders.create!(email: '[email protected]')
  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