-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.rb
60 lines (48 loc) · 977 Bytes
/
order.rb
1
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true
# require 'active_record'
class Order # < ActiveRecord::Base
include OrderState
attr_accessor :state
def initialize
@state = "received"
end
# Query the state
def active?
order_state.active?
end
# Predicates used by the states to determine if the state can transition
def can_confirm?
true
end
def can_complete?
true
end
def can_ship?
true
end
# predicates using the order state
def can_add_item?
order_state.received?
end
def can_edit?
order_state.can_edit?
end
def can_cancel?
order_state.can_cancel?
end
# Actions. These will fail and the block NOT executed if the order is in the wrong state
def cancel!
Order.transaction do
order_state.cancel! do
cancel_production_entries
end
end
end
def close!
Order.transaction do
order_state.close! do
production_entries.each(&:consume!)
end
end
end
end