Class: Spree::TestingSupport::OrderWalkthrough

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.up_to(state, user: nil) ⇒ Object



6
7
8
# File 'lib/spree/testing_support/order_walkthrough.rb', line 6

def self.up_to(state, user: nil)
  new.up_to(state, user: user)
end

Instance Method Details

#up_to(state, user: nil) ⇒ Object



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
42
43
44
45
# File 'lib/spree/testing_support/order_walkthrough.rb', line 10

def up_to(state, user: nil)
  # Need to create a valid zone too...
  @zone = ::FactoryBot.create(:zone)
  @country = ::FactoryBot.create(:country)
  @state = ::FactoryBot.create(:state, country: @country)

  @zone.members << Spree::ZoneMember.create(zoneable: @country)

  # A shipping method must exist for rates to be displayed on checkout page
  ::FactoryBot.create(:shipping_method, zones: [@zone]).tap do |sm|
    sm.calculator.preferred_amount = 10
    sm.calculator.preferred_currency = Spree::Config[:currency]
    sm.calculator.save
  end

  order = Spree::Order.create!(
    user: user,
    email: "[email protected]",
    store: Spree::Store.first || ::FactoryBot.create(:store)
  )
  add_line_item!(order)
  order.next!

  states_to_process = if state == :complete
                        states
                      else
                        end_state_position = states.index(state.to_sym)
                        states[..end_state_position]
                      end

  states_to_process.each do |state_to_process|
    send(state_to_process, order)
  end

  order
end