-
Notifications
You must be signed in to change notification settings - Fork 23
/
active_record.rb
194 lines (151 loc) · 4.71 KB
/
active_record.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require 'active_record'
### Setup test database
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Base.connection.create_table(:active_record_test_models) do |t|
t.string :state
t.string :type
t.timestamps
end
ActiveRecord::Base.connection.create_table(:active_record_test_model_with_contexts) do |t|
t.string :state
t.string :type
t.timestamps
end
ActiveRecord::Base.connection.create_table(:active_record_test_model_with_multiple_contexts) do |t|
t.string :state
t.string :type
t.timestamps
end
ActiveRecord::Base.connection.create_table(:active_record_test_model_with_multiple_state_machines) do |t|
t.string :first
t.string :second
t.timestamps
end
# We probably want to provide a generator for this model and the accompanying migration.
class ActiveRecordTestModelStateTransition < ActiveRecord::Base
belongs_to :test_model
end
class ActiveRecordTestModelWithContextStateTransition < ActiveRecord::Base
belongs_to :test_model
end
class ActiveRecordTestModelWithMultipleContextStateTransition < ActiveRecord::Base
belongs_to :test_model
end
class ActiveRecordTestModelWithMultipleStateMachinesFirstTransition < ActiveRecord::Base
belongs_to :test_model
end
class ActiveRecordTestModelWithMultipleStateMachinesSecondTransition < ActiveRecord::Base
belongs_to :test_model
end
class ActiveRecordTestModelWithMultipleStateMachinesThirdTransition < ActiveRecord::Base
belongs_to :test_model
end
class ActiveRecordTestModel < ActiveRecord::Base
state_machine :state, :initial => :waiting do # log initial state?
store_audit_trail
event :start do
transition [:waiting, :stopped] => :started
end
event :stop do
transition :started => :stopped
end
end
end
class ActiveRecordTestModelWithContext < ActiveRecord::Base
state_machine :state, :initial => :waiting do # log initial state?
store_audit_trail :context_to_log => :context
event :start do
transition [:waiting, :stopped] => :started
end
event :stop do
transition :started => :stopped
end
end
def context
"Some context"
end
end
class ActiveRecordTestModelWithMultipleContext < ActiveRecord::Base
state_machine :state, :initial => :waiting do # log initial state?
store_audit_trail :context_to_log => [:context, :second_context]
event :start do
transition [:waiting, :stopped] => :started
end
event :stop do
transition :started => :stopped
end
end
def context
"Some context"
end
def second_context
"Extra context"
end
end
class ActiveRecordTestModelDescendant < ActiveRecordTestModel
end
class ActiveRecordTestModelDescendantWithOwnStateMachine < ActiveRecordTestModel
state_machine :state, :initial => :new do
store_audit_trail
event :complete do
transition [:new] => :completed
end
end
end
class ActiveRecordTestModelWithMultipleStateMachines < ActiveRecord::Base
state_machine :first, :initial => :beginning do
store_audit_trail
event :begin_first do
transition :beginning => :end
end
end
state_machine :second do
store_audit_trail
event :begin_second do
transition nil => :beginning_second
end
end
state_machine :third, :action => nil do
store_audit_trail
event :begin_third do
transition nil => :beginning_third
end
event :end_third do
transition :beginning_third => :done_third
end
end
end
module SomeNamespace
class ActiveRecordTestModelStateTransition < ActiveRecord::Base
belongs_to :test_model
end
class ActiveRecordTestModel < ActiveRecord::Base
state_machine :state, :initial => :waiting do # log initial state?
store_audit_trail
event :start do
transition [:waiting, :stopped] => :started
end
event :stop do
transition :started => :stopped
end
end
end
end
def create_transition_table(owner_class, state, add_context = false)
class_name = "#{owner_class.name}#{state.to_s.camelize}Transition"
ActiveRecord::Base.connection.create_table(class_name.tableize) do |t|
t.integer owner_class.name.foreign_key
t.string :event
t.string :from
t.string :to
t.string :context if add_context
t.string :second_context if add_context
t.datetime :created_at
end
end
create_transition_table(ActiveRecordTestModel, :state)
create_transition_table(ActiveRecordTestModelWithContext, :state, true)
create_transition_table(ActiveRecordTestModelWithMultipleContext, :state, true)
create_transition_table(ActiveRecordTestModelWithMultipleStateMachines, :first)
create_transition_table(ActiveRecordTestModelWithMultipleStateMachines, :second)
create_transition_table(ActiveRecordTestModelWithMultipleStateMachines, :third)