transitions 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +3 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile +7 -2
- data/README.md +1 -1
- data/Rakefile +4 -4
- data/lib/active_model/transitions.rb +8 -8
- data/lib/transitions/event.rb +15 -16
- data/lib/transitions/machine.rb +4 -5
- data/lib/transitions/presenter.rb +1 -1
- data/lib/transitions/state.rb +2 -1
- data/lib/transitions/state_transition.rb +2 -2
- data/lib/transitions/version.rb +1 -1
- data/lib/transitions.rb +10 -8
- data/test/active_record/test_active_record.rb +64 -66
- data/test/active_record/test_active_record_scopes.rb +7 -7
- data/test/active_record/test_active_record_timestamps.rb +27 -28
- data/test/active_record/test_custom_select.rb +3 -3
- data/test/event/test_event.rb +20 -20
- data/test/event/test_event_arguments.rb +3 -4
- data/test/event/test_event_being_fired.rb +4 -4
- data/test/event/test_event_checks.rb +5 -6
- data/test/helper.rb +1 -1
- data/test/machine/machine_template.rb +4 -4
- data/test/machine/test_available_states_listing.rb +1 -1
- data/test/machine/test_fire_event_machine.rb +5 -5
- data/test/machine/test_machine.rb +10 -10
- data/test/state/test_state.rb +16 -16
- data/test/state/test_state_predicate_method.rb +2 -2
- data/test/state_transition/test_state_transition.rb +11 -11
- data/test/state_transition/test_state_transition_event_failed_callback.rb +3 -3
- data/test/state_transition/test_state_transition_event_fired_callback.rb +3 -3
- data/test/state_transition/test_state_transition_guard_check.rb +14 -15
- data/test/state_transition/test_state_transition_on_transition_callback.rb +4 -4
- data/test/state_transition/test_state_transition_success_callback.rb +8 -8
- data/transitions.gemspec +16 -16
- metadata +4 -3
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
class CreateTrafficLights < ActiveRecord::Migration
|
4
4
|
def self.up
|
5
|
-
create_table(:traffic_lights, :
|
5
|
+
create_table(:traffic_lights, force: true) do |t|
|
6
6
|
t.string :state
|
7
7
|
t.string :name
|
8
8
|
t.string :power
|
@@ -22,7 +22,7 @@ end
|
|
22
22
|
class TrafficLight < ActiveRecord::Base
|
23
23
|
include ActiveModel::Transitions
|
24
24
|
|
25
|
-
state_machine :
|
25
|
+
state_machine auto_scopes: true do
|
26
26
|
state :off, enter: :turn_power_on
|
27
27
|
|
28
28
|
state :red
|
@@ -30,34 +30,34 @@ class TrafficLight < ActiveRecord::Base
|
|
30
30
|
state :yellow
|
31
31
|
|
32
32
|
event :red_on do
|
33
|
-
transitions :
|
33
|
+
transitions to: :red, from: [:yellow]
|
34
34
|
end
|
35
35
|
|
36
36
|
event :green_on do
|
37
|
-
transitions :
|
37
|
+
transitions to: :green, from: [:red]
|
38
38
|
end
|
39
39
|
|
40
40
|
event :yellow_on do
|
41
|
-
transitions :
|
41
|
+
transitions to: :yellow, from: [:green]
|
42
42
|
end
|
43
43
|
|
44
44
|
event :reset do
|
45
|
-
transitions :
|
45
|
+
transitions to: :red, from: [:off]
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
def turn_power_on
|
50
|
-
|
51
|
-
self.power =
|
50
|
+
fail 'the power should not have been on already' if power == 'on'
|
51
|
+
self.power = 'on'
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
class ValidatingTrafficLight < TrafficLight
|
56
|
-
validate {|t| errors.add(:base, 'This TrafficLight will never validate after creation') unless t.new_record? }
|
56
|
+
validate { |t| errors.add(:base, 'This TrafficLight will never validate after creation') unless t.new_record? }
|
57
57
|
end
|
58
58
|
|
59
59
|
class ConditionalValidatingTrafficLight < TrafficLight
|
60
|
-
validates(:name, :
|
60
|
+
validates(:name, presence: true, if: :red?)
|
61
61
|
end
|
62
62
|
|
63
63
|
class TestActiveRecord < Test::Unit::TestCase
|
@@ -66,31 +66,31 @@ class TestActiveRecord < Test::Unit::TestCase
|
|
66
66
|
@light = TrafficLight.create!
|
67
67
|
end
|
68
68
|
|
69
|
-
test
|
69
|
+
test 'new record has the initial state set' do
|
70
70
|
@light = TrafficLight.new
|
71
|
-
assert_equal
|
71
|
+
assert_equal 'off', @light.state
|
72
72
|
end
|
73
73
|
|
74
|
-
test
|
74
|
+
test 'new active records defaults current state to the initial state' do
|
75
75
|
assert_equal :off, @light.current_state
|
76
76
|
end
|
77
77
|
|
78
|
-
test
|
78
|
+
test 'states initial state' do
|
79
79
|
assert @light.off?
|
80
80
|
assert_equal :off, @light.current_state
|
81
81
|
end
|
82
82
|
|
83
|
-
test
|
83
|
+
test 'calls enter when setting the initial state' do
|
84
84
|
@new_light = TrafficLight.new
|
85
|
-
assert_equal
|
85
|
+
assert_equal 'on', @new_light.power
|
86
86
|
end
|
87
87
|
|
88
|
-
test
|
89
|
-
assert_equal
|
88
|
+
test 'does not call enter when loading a persisted record' do
|
89
|
+
assert_equal 'on', @light.power
|
90
90
|
assert_nothing_raised { TrafficLight.find(@light.id) }
|
91
91
|
end
|
92
92
|
|
93
|
-
test
|
93
|
+
test 'transition to a valid state' do
|
94
94
|
@light.reset
|
95
95
|
assert @light.red?
|
96
96
|
assert_equal :red, @light.current_state
|
@@ -100,42 +100,42 @@ class TestActiveRecord < Test::Unit::TestCase
|
|
100
100
|
assert_equal :green, @light.current_state
|
101
101
|
end
|
102
102
|
|
103
|
-
test
|
103
|
+
test 'transition does not persist state' do
|
104
104
|
@light.reset
|
105
105
|
assert_equal :red, @light.current_state
|
106
106
|
@light.reload
|
107
|
-
assert_equal
|
107
|
+
assert_equal 'off', @light.state
|
108
108
|
end
|
109
109
|
|
110
|
-
test
|
110
|
+
test 'transition does persists state' do
|
111
111
|
@light.reset!
|
112
112
|
assert_equal :red, @light.current_state
|
113
113
|
@light.reload
|
114
|
-
assert_equal
|
114
|
+
assert_equal 'red', @light.state
|
115
115
|
end
|
116
116
|
|
117
|
-
test
|
117
|
+
test 'transition to an invalid state' do
|
118
118
|
assert_raise(Transitions::InvalidTransition) { @light.yellow_on }
|
119
119
|
assert_equal :off, @light.current_state
|
120
120
|
end
|
121
121
|
|
122
|
-
test
|
122
|
+
test 'transition with wrong state will not validate' do
|
123
123
|
for s in @light.class.get_state_machine.states
|
124
124
|
@light.state = s.name
|
125
125
|
assert @light.valid?
|
126
126
|
end
|
127
|
-
@light.state =
|
127
|
+
@light.state = 'invalid_one'
|
128
128
|
assert_false @light.valid?
|
129
129
|
end
|
130
130
|
|
131
|
-
test
|
132
|
-
validating_light = ValidatingTrafficLight.create!(:
|
131
|
+
test 'transition raises exception when model validation fails' do
|
132
|
+
validating_light = ValidatingTrafficLight.create!(name: 'Foobar')
|
133
133
|
assert_raise(ActiveRecord::RecordInvalid) do
|
134
134
|
validating_light.reset!
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
138
|
-
test
|
138
|
+
test 'state query method used in a validation condition' do
|
139
139
|
validating_light = ConditionalValidatingTrafficLight.create!
|
140
140
|
assert_raise(ActiveRecord::RecordInvalid) do
|
141
141
|
validating_light.reset!
|
@@ -143,19 +143,19 @@ class TestActiveRecord < Test::Unit::TestCase
|
|
143
143
|
assert(validating_light.off?)
|
144
144
|
end
|
145
145
|
|
146
|
-
test
|
146
|
+
test 'reloading model resets current state' do
|
147
147
|
@light.reset
|
148
148
|
assert @light.red?
|
149
149
|
@light.update_attribute(:state, 'green')
|
150
|
-
assert @light.reload.green?,
|
150
|
+
assert @light.reload.green?, 'reloaded state should come from database, not instance variable'
|
151
151
|
end
|
152
152
|
|
153
|
-
test
|
153
|
+
test 'calling non-bang event updates state attribute' do
|
154
154
|
@light.reset!
|
155
155
|
assert @light.red?
|
156
156
|
@light.green_on
|
157
|
-
assert_equal
|
158
|
-
assert_equal
|
157
|
+
assert_equal 'green', @light.state
|
158
|
+
assert_equal 'red', @light.reload.state
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
@@ -172,38 +172,36 @@ if ActiveRecord::VERSION::MAJOR == 3
|
|
172
172
|
end
|
173
173
|
end
|
174
174
|
|
175
|
-
test
|
175
|
+
test 'transition does persists state when state is protected' do
|
176
176
|
protected_light = @light_with_protected_state.create!
|
177
177
|
protected_light.reset!
|
178
178
|
assert_equal :red, protected_light.current_state
|
179
179
|
protected_light.reload
|
180
|
-
assert_equal
|
180
|
+
assert_equal 'red', protected_light.state
|
181
181
|
end
|
182
182
|
end
|
183
183
|
end
|
184
184
|
|
185
185
|
class TestNewActiveRecord < TestActiveRecord
|
186
|
-
|
187
186
|
def setup
|
188
187
|
set_up_db CreateTrafficLights
|
189
188
|
@light = TrafficLight.new
|
190
189
|
end
|
191
190
|
|
192
|
-
test
|
191
|
+
test 'new active records defaults current state to the initial state' do
|
193
192
|
assert_equal :off, @light.current_state
|
194
193
|
end
|
195
|
-
|
196
194
|
end
|
197
195
|
|
198
196
|
class TestScopes < Test::Unit::TestCase
|
199
|
-
test
|
197
|
+
test 'scope returns correct object' do
|
200
198
|
@light = TrafficLight.create!
|
201
199
|
assert_respond_to TrafficLight, :off
|
202
200
|
assert_equal TrafficLight.off.first, @light
|
203
201
|
assert TrafficLight.red.empty?
|
204
202
|
end
|
205
203
|
|
206
|
-
test
|
204
|
+
test 'scopes exist' do
|
207
205
|
assert_respond_to TrafficLight, :off
|
208
206
|
assert_respond_to TrafficLight, :red
|
209
207
|
assert_respond_to TrafficLight, :green
|
@@ -216,23 +214,23 @@ class TestScopes < Test::Unit::TestCase
|
|
216
214
|
end
|
217
215
|
|
218
216
|
test 'scope generation raises an exception if we try to overwrite an existing method' do
|
219
|
-
assert_raise(Transitions::InvalidMethodOverride)
|
217
|
+
assert_raise(Transitions::InvalidMethodOverride) do
|
220
218
|
class Light < ActiveRecord::Base
|
221
219
|
include ActiveModel::Transitions
|
222
220
|
|
223
|
-
state_machine :
|
221
|
+
state_machine auto_scopes: true do
|
224
222
|
state :new
|
225
223
|
state :broken
|
226
224
|
end
|
227
225
|
end
|
228
|
-
|
226
|
+
end
|
229
227
|
end
|
230
228
|
end
|
231
229
|
|
232
230
|
class DifferentTrafficLight < ActiveRecord::Base
|
233
231
|
include ActiveModel::Transitions
|
234
232
|
|
235
|
-
state_machine :
|
233
|
+
state_machine attribute_name: :different_state, auto_scopes: true do
|
236
234
|
state :off
|
237
235
|
|
238
236
|
state :red
|
@@ -240,19 +238,19 @@ class DifferentTrafficLight < ActiveRecord::Base
|
|
240
238
|
state :yellow
|
241
239
|
|
242
240
|
event :red_on do
|
243
|
-
transitions :
|
241
|
+
transitions to: :red, from: [:yellow]
|
244
242
|
end
|
245
243
|
|
246
244
|
event :green_on do
|
247
|
-
transitions :
|
245
|
+
transitions to: :green, from: [:red]
|
248
246
|
end
|
249
247
|
|
250
248
|
event :yellow_on do
|
251
|
-
transitions :
|
249
|
+
transitions to: :yellow, from: [:green]
|
252
250
|
end
|
253
251
|
|
254
252
|
event :reset do
|
255
|
-
transitions :
|
253
|
+
transitions to: :red, from: [:off]
|
256
254
|
end
|
257
255
|
end
|
258
256
|
end
|
@@ -263,17 +261,17 @@ class TestActiveRecordWithDifferentColumnName < Test::Unit::TestCase
|
|
263
261
|
@light = DifferentTrafficLight.create!
|
264
262
|
end
|
265
263
|
|
266
|
-
test
|
264
|
+
test 'new record has the initial state set' do
|
267
265
|
@light = DifferentTrafficLight.new
|
268
|
-
assert_equal
|
266
|
+
assert_equal 'off', @light.different_state
|
269
267
|
end
|
270
268
|
|
271
|
-
test
|
269
|
+
test 'states initial state' do
|
272
270
|
assert @light.off?
|
273
271
|
assert_equal :off, @light.current_state
|
274
272
|
end
|
275
273
|
|
276
|
-
test
|
274
|
+
test 'transition to a valid state' do
|
277
275
|
@light.reset
|
278
276
|
assert @light.red?
|
279
277
|
assert_equal :red, @light.current_state
|
@@ -283,46 +281,46 @@ class TestActiveRecordWithDifferentColumnName < Test::Unit::TestCase
|
|
283
281
|
assert_equal :green, @light.current_state
|
284
282
|
end
|
285
283
|
|
286
|
-
test
|
284
|
+
test 'transition does not persist state' do
|
287
285
|
@light.reset
|
288
286
|
assert_equal :red, @light.current_state
|
289
287
|
@light.reload
|
290
|
-
assert_equal
|
288
|
+
assert_equal 'off', @light.different_state
|
291
289
|
end
|
292
290
|
|
293
|
-
test
|
291
|
+
test 'transition does persists state' do
|
294
292
|
@light.reset!
|
295
293
|
assert_equal :red, @light.current_state
|
296
294
|
@light.reload
|
297
|
-
assert_equal
|
295
|
+
assert_equal 'red', @light.different_state
|
298
296
|
end
|
299
297
|
|
300
|
-
test
|
298
|
+
test 'transition to an invalid state' do
|
301
299
|
assert_raise(Transitions::InvalidTransition) { @light.yellow_on }
|
302
300
|
assert_equal :off, @light.current_state
|
303
301
|
end
|
304
302
|
|
305
|
-
test
|
303
|
+
test 'transition with wrong state will not validate' do
|
306
304
|
for s in @light.class.state_machine.states
|
307
305
|
@light.different_state = s.name
|
308
306
|
assert @light.valid?
|
309
307
|
end
|
310
|
-
@light.different_state =
|
308
|
+
@light.different_state = 'invalid_one'
|
311
309
|
assert_false @light.valid?
|
312
310
|
end
|
313
311
|
|
314
|
-
test
|
312
|
+
test 'reloading model resets current state' do
|
315
313
|
@light.reset
|
316
314
|
assert @light.red?
|
317
315
|
@light.update_attribute(:different_state, 'green')
|
318
|
-
assert @light.reload.green?,
|
316
|
+
assert @light.reload.green?, 'reloaded state should come from database, not instance variable'
|
319
317
|
end
|
320
318
|
|
321
|
-
test
|
319
|
+
test 'calling non-bang event updates state attribute' do
|
322
320
|
@light.reset!
|
323
321
|
assert @light.red?
|
324
322
|
@light.green_on
|
325
|
-
assert_equal
|
326
|
-
assert_equal
|
323
|
+
assert_equal 'green', @light.different_state
|
324
|
+
assert_equal 'red', @light.reload.different_state
|
327
325
|
end
|
328
326
|
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
class CreateBunnies < ActiveRecord::Migration
|
4
4
|
def self.up
|
5
|
-
create_table(:bunnies, :
|
5
|
+
create_table(:bunnies, force: true) do |t|
|
6
6
|
t.string :status # Explicitly use another state column to ensure that this whole enchilada is working with other state column names than the default ones.
|
7
7
|
end
|
8
8
|
end
|
@@ -10,7 +10,7 @@ end
|
|
10
10
|
|
11
11
|
class CreatePuppies < ActiveRecord::Migration
|
12
12
|
def self.up
|
13
|
-
create_table(:puppies, :
|
13
|
+
create_table(:puppies, force: true) do |t|
|
14
14
|
t.string :state
|
15
15
|
end
|
16
16
|
end
|
@@ -19,7 +19,7 @@ end
|
|
19
19
|
class Bunny < ActiveRecord::Base
|
20
20
|
include ActiveModel::Transitions
|
21
21
|
|
22
|
-
state_machine :
|
22
|
+
state_machine attribute_name: :status, auto_scopes: true do
|
23
23
|
state :hobbling
|
24
24
|
end
|
25
25
|
end
|
@@ -38,11 +38,11 @@ class TestScopes < Test::Unit::TestCase
|
|
38
38
|
@bunny = Bunny.create!
|
39
39
|
end
|
40
40
|
|
41
|
-
test
|
41
|
+
test 'scopes exist' do
|
42
42
|
assert_respond_to Bunny, :hobbling
|
43
43
|
end
|
44
44
|
|
45
|
-
test
|
45
|
+
test 'scope returns correct object' do
|
46
46
|
assert_equal Bunny.hobbling.first, @bunny
|
47
47
|
end
|
48
48
|
|
@@ -55,7 +55,7 @@ class TestScopes < Test::Unit::TestCase
|
|
55
55
|
Class.new(ActiveRecord::Base) do
|
56
56
|
include ActiveModel::Transitions
|
57
57
|
|
58
|
-
state_machine :
|
58
|
+
state_machine auto_scopes: true do
|
59
59
|
state :new
|
60
60
|
end
|
61
61
|
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
class CreateOrders < ActiveRecord::Migration
|
4
4
|
def self.up
|
5
|
-
create_table(:orders, :
|
5
|
+
create_table(:orders, force: true) do |t|
|
6
6
|
t.string :state
|
7
7
|
t.string :order_number
|
8
8
|
t.datetime :paid_at
|
9
9
|
t.datetime :prepared_on
|
10
10
|
t.datetime :dispatched_at
|
11
11
|
t.date :cancellation_date
|
12
|
-
t.boolean :allow_transition, :
|
12
|
+
t.boolean :allow_transition, default: true
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
@@ -27,62 +27,62 @@ class Order < ActiveRecord::Base
|
|
27
27
|
|
28
28
|
# no timestamp col is being specified here - should be ignored
|
29
29
|
event :place do
|
30
|
-
transitions :
|
30
|
+
transitions from: :opened, to: :placed
|
31
31
|
end
|
32
32
|
|
33
33
|
# should set paid_at timestamp
|
34
|
-
event :pay, :
|
35
|
-
transitions :
|
34
|
+
event :pay, timestamp: true do
|
35
|
+
transitions from: :placed, to: :paid, guard: lambda { |obj| obj.allow_transition }
|
36
36
|
end
|
37
37
|
|
38
38
|
# should set prepared_on
|
39
|
-
event :prepare, :
|
40
|
-
transitions :
|
39
|
+
event :prepare, timestamp: true do
|
40
|
+
transitions from: :paid, to: :prepared
|
41
41
|
end
|
42
42
|
|
43
43
|
# should set dispatched_at
|
44
|
-
event :deliver, :
|
45
|
-
transitions :
|
44
|
+
event :deliver, timestamp: 'dispatched_at' do
|
45
|
+
transitions from: :prepared, to: :delivered
|
46
46
|
end
|
47
47
|
|
48
48
|
# should set cancellation_date
|
49
|
-
event :cancel, :
|
50
|
-
transitions :
|
49
|
+
event :cancel, timestamp: :cancellation_date do
|
50
|
+
transitions from: [:placed, :paid, :prepared], to: :cancelled
|
51
51
|
end
|
52
52
|
|
53
53
|
# should raise an exception as there is no timestamp col
|
54
|
-
event :reopen, :
|
55
|
-
transitions :
|
54
|
+
event :reopen, timestamp: true do
|
55
|
+
transitions from: :cancelled, to: :opened
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
class TestActiveRecordTimestamps < Test::Unit::TestCase
|
61
|
-
require
|
61
|
+
require 'securerandom'
|
62
62
|
|
63
63
|
def setup
|
64
64
|
set_up_db CreateOrders
|
65
65
|
end
|
66
66
|
|
67
67
|
def create_order(state = nil)
|
68
|
-
Order.create! :
|
68
|
+
Order.create! order_number: SecureRandom.hex(4), state: state
|
69
69
|
end
|
70
70
|
|
71
71
|
# control case, no timestamp has been set so we should expect default behaviour
|
72
|
-
test
|
72
|
+
test 'moving to placed does not raise any exceptions' do
|
73
73
|
@order = create_order
|
74
74
|
assert_nothing_raised { @order.place! }
|
75
|
-
assert_equal @order.state,
|
75
|
+
assert_equal @order.state, 'placed'
|
76
76
|
end
|
77
77
|
|
78
|
-
test
|
78
|
+
test 'moving to paid should set paid_at' do
|
79
79
|
@order = create_order(:placed)
|
80
80
|
@order.pay!
|
81
81
|
@order.reload
|
82
82
|
assert_not_nil @order.paid_at
|
83
83
|
end
|
84
84
|
|
85
|
-
test
|
85
|
+
test 'moving to paid should not set paid_at if our guard evaluates to false' do
|
86
86
|
@order = create_order(:placed)
|
87
87
|
@order.update_attribute :allow_transition, false
|
88
88
|
@order.pay!
|
@@ -90,43 +90,42 @@ class TestActiveRecordTimestamps < Test::Unit::TestCase
|
|
90
90
|
assert_nil @order.paid_at
|
91
91
|
end
|
92
92
|
|
93
|
-
test
|
93
|
+
test 'moving to prepared should set prepared_on' do
|
94
94
|
@order = create_order(:paid)
|
95
95
|
@order.prepare!
|
96
96
|
@order.reload
|
97
97
|
assert_not_nil @order.prepared_on
|
98
98
|
end
|
99
99
|
|
100
|
-
test
|
100
|
+
test 'moving to delivered should set dispatched_at' do
|
101
101
|
@order = create_order(:prepared)
|
102
102
|
@order.deliver!
|
103
103
|
@order.reload
|
104
104
|
assert_not_nil @order.dispatched_at
|
105
105
|
end
|
106
106
|
|
107
|
-
test
|
107
|
+
test 'moving to cancelled should set cancellation_date' do
|
108
108
|
@order = create_order(:placed)
|
109
109
|
@order.cancel!
|
110
110
|
@order.reload
|
111
111
|
assert_not_nil @order.cancellation_date
|
112
112
|
end
|
113
113
|
|
114
|
-
test
|
114
|
+
test 'moving to reopened should raise an exception as there is no attribute' do
|
115
115
|
@order = create_order(:cancelled)
|
116
116
|
assert_raise(NoMethodError) { @order.re_open! }
|
117
117
|
@order.reload
|
118
118
|
end
|
119
119
|
|
120
|
-
test
|
120
|
+
test 'passing an invalid value to timestamp options should raise an exception' do
|
121
121
|
assert_raise(ArgumentError) do
|
122
122
|
class Order < ActiveRecord::Base
|
123
123
|
include ActiveModel::Transitions
|
124
124
|
state_machine do
|
125
|
-
event :replace, :
|
126
|
-
transitions :
|
125
|
+
event :replace, timestamp: 1 do
|
126
|
+
transitions from: :prepared, to: :placed
|
127
127
|
end
|
128
128
|
end
|
129
|
-
|
130
129
|
end
|
131
130
|
end
|
132
131
|
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
# Regression test for https://github.com/troessner/transitions/issues/95
|
4
4
|
class CreateSwitches < ActiveRecord::Migration
|
5
5
|
def self.up
|
6
|
-
create_table(:switches, :
|
6
|
+
create_table(:switches, force: true) do |t|
|
7
7
|
t.string :state
|
8
8
|
end
|
9
9
|
end
|
@@ -24,7 +24,7 @@ class TestCustomSelect < Test::Unit::TestCase
|
|
24
24
|
Switch.create!
|
25
25
|
end
|
26
26
|
|
27
|
-
test
|
27
|
+
test 'should not trigger an exception when we use a custom select query which excludes the name of our state attribute' do
|
28
28
|
result = Switch.select(:id)
|
29
29
|
assert_nothing_raised NoMethodError do
|
30
30
|
result.inspect
|
data/test/event/test_event.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
class TestEvent < Test::Unit::TestCase
|
4
4
|
def setup
|
@@ -9,64 +9,64 @@ class TestEvent < Test::Unit::TestCase
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def event_with_symbol_success_callback
|
12
|
-
@event = Transitions::Event.new(nil, @state_name,
|
13
|
-
transitions :
|
12
|
+
@event = Transitions::Event.new(nil, @state_name, success: @success_as_symbol) do
|
13
|
+
transitions to: :closed, from: [:open, :received]
|
14
14
|
end
|
15
15
|
end
|
16
16
|
alias_method :new_event, :event_with_symbol_success_callback
|
17
17
|
|
18
18
|
def event_with_lambda_success_callback
|
19
|
-
@event = Transitions::Event.new(nil, @state_name,
|
20
|
-
transitions :
|
19
|
+
@event = Transitions::Event.new(nil, @state_name, success: @success_as_lambda) do
|
20
|
+
transitions to: :closed, from: [:open, :received]
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
def event_with_array_success_callback
|
25
|
-
@event = Transitions::Event.new(nil, @state_name,
|
26
|
-
transitions :
|
25
|
+
@event = Transitions::Event.new(nil, @state_name, success: @success_as_array) do
|
26
|
+
transitions to: :closed, from: [:open, :received]
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
test
|
30
|
+
test 'should set the name' do
|
31
31
|
assert_equal @state_name, new_event.name
|
32
32
|
end
|
33
33
|
|
34
|
-
test
|
34
|
+
test 'should set the success callback with a symbol and return a block' do
|
35
35
|
assert_respond_to event_with_symbol_success_callback.success, :call
|
36
36
|
end
|
37
37
|
|
38
|
-
test
|
39
|
-
record = mock(
|
38
|
+
test 'should build a block which calls the given success_callback symbol on the passed record instance' do
|
39
|
+
record = mock('SomeRecordToGetCalled')
|
40
40
|
record.expects(:success_callback)
|
41
41
|
|
42
42
|
event_with_symbol_success_callback.success.call(record)
|
43
43
|
end
|
44
44
|
|
45
|
-
test
|
45
|
+
test 'should set the success callback with a lambda' do
|
46
46
|
assert_respond_to event_with_lambda_success_callback.success, :call
|
47
47
|
end
|
48
48
|
|
49
|
-
test
|
50
|
-
record = mock(
|
49
|
+
test 'should build a block which calls the given success_callback lambda on the passed record instance' do
|
50
|
+
record = mock('SomeRecordToGetCalled')
|
51
51
|
record.expects(:success_callback)
|
52
52
|
|
53
53
|
event_with_lambda_success_callback.success.call(record)
|
54
54
|
end
|
55
55
|
|
56
|
-
test
|
56
|
+
test 'should set the success callback with an array' do
|
57
57
|
assert_respond_to event_with_array_success_callback.success, :call
|
58
58
|
end
|
59
59
|
|
60
|
-
test
|
61
|
-
record = mock(
|
60
|
+
test 'should build a block which calls the given success_callback array on the passed record instance for each callback' do
|
61
|
+
record = mock('SomeRecordToGetCalled')
|
62
62
|
record.expects(:success_callback).twice
|
63
63
|
|
64
64
|
event_with_array_success_callback.success.call(record)
|
65
65
|
end
|
66
66
|
|
67
|
-
test
|
68
|
-
Transitions::StateTransition.expects(:new).with(:
|
69
|
-
Transitions::StateTransition.expects(:new).with(:
|
67
|
+
test 'should create StateTransitions' do
|
68
|
+
Transitions::StateTransition.expects(:new).with(to: :closed, from: :open)
|
69
|
+
Transitions::StateTransition.expects(:new).with(to: :closed, from: :received)
|
70
70
|
new_event
|
71
71
|
end
|
72
72
|
end
|