-
Notifications
You must be signed in to change notification settings - Fork 629
/
complex_multiple_example.rb
133 lines (107 loc) · 3.39 KB
/
complex_multiple_example.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class ComplexMultipleExample
include AASM
attr_accessor :activation_code, :activated_at, :deleted_at
aasm(:left) do
state :passive
state :pending, :initial => true, :before_enter => :make_activation_code
state :active, :before_enter => :do_activate
state :suspended
state :deleted, :before_enter => :do_delete#, :exit => :do_undelete
state :waiting
event :left_register do
transitions :from => :passive, :to => :pending do
guard do
can_register?
end
end
end
event :left_activate do
transitions :from => :pending, :to => :active
end
event :left_suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended
end
event :left_delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end
# a dummy event that can never happen
event :left_unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end
event :left_unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new { has_activated? }
transitions :from => :suspended, :to => :pending, :guard => :has_activation_code?
transitions :from => :suspended, :to => :passive
end
event :left_wait do
transitions :from => :suspended, :to => :waiting, :guard => :if_polite?
end
end
aasm(:right) do
state :passive
state :pending, :initial => true, :before_enter => :make_activation_code
state :active, :before_enter => :do_activate
state :suspended
state :deleted, :before_enter => :do_delete#, :exit => :do_undelete
state :waiting
event :right_register do
transitions :from => :passive, :to => :pending do
guard do
can_register?
end
end
end
event :right_activate do
transitions :from => :pending, :to => :active
end
event :right_suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended
end
event :right_delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end
# a dummy event that can never happen
event :right_unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end
event :right_unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new { has_activated? }
transitions :from => :suspended, :to => :pending, :guard => :has_activation_code?
transitions :from => :suspended, :to => :passive
end
event :right_wait do
transitions :from => :suspended, :to => :waiting, :guard => :if_polite?
end
end # right
def initialize
# the AR backend uses a before_validate_on_create :aasm_ensure_initial_state
# lets do something similar here for testing purposes.
aasm(:left).enter_initial_state
aasm(:right).enter_initial_state
end
def make_activation_code
@activation_code = @activation_code ? @activation_code + '2' : '1'
end
def do_activate
@activated_at = Time.now
@activation_code = nil
end
def do_delete
@deleted_at = Time.now
end
def do_undelete
@deleted_at = false
end
def can_register?
true
end
def has_activated?
!!@activated_at
end
def has_activation_code?
!!@activation_code
end
def if_polite?(phrase = nil)
phrase == :please
end
end