aasm 3.0.24 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/.travis.yml +29 -4
- data/CHANGELOG.md +56 -0
- data/Gemfile +10 -1
- data/LICENSE +1 -1
- data/README.md +151 -20
- data/aasm.gemspec +5 -6
- data/gemfiles/rails_3.2.gemfile +13 -0
- data/gemfiles/rails_4.0.gemfile +16 -0
- data/gemfiles/rails_4.1.gemfile +16 -0
- data/lib/aasm/aasm.rb +36 -32
- data/lib/aasm/base.rb +49 -31
- data/lib/aasm/event.rb +28 -17
- data/lib/aasm/instance_base.rb +9 -4
- data/lib/aasm/localizer.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +65 -16
- data/lib/aasm/persistence/base.rb +10 -14
- data/lib/aasm/persistence/mongoid_persistence.rb +10 -8
- data/lib/aasm/persistence/sequel_persistence.rb +108 -0
- data/lib/aasm/persistence.rb +3 -0
- data/lib/aasm/state.rb +4 -3
- data/lib/aasm/state_machine.rb +18 -10
- data/lib/aasm/transition.rb +13 -6
- data/lib/aasm/version.rb +1 -1
- data/lib/aasm.rb +0 -3
- data/spec/database.rb +33 -0
- data/spec/models/double_definer.rb +21 -0
- data/spec/models/foo.rb +2 -1
- data/spec/models/guardian.rb +48 -0
- data/spec/models/mongoid/no_scope_mongoid.rb +1 -1
- data/spec/models/mongoid/simple_mongoid.rb +5 -4
- data/spec/models/mongoid/simple_new_dsl_mongoid.rb +1 -1
- data/spec/models/not_auto_loaded/process.rb +10 -8
- data/spec/models/persistence.rb +5 -13
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/api_spec.rb +12 -12
- data/spec/unit/callbacks_spec.rb +29 -45
- data/spec/unit/complex_example_spec.rb +24 -15
- data/spec/unit/event_naming_spec.rb +24 -0
- data/spec/unit/event_spec.rb +124 -76
- data/spec/unit/guard_spec.rb +60 -0
- data/spec/unit/initial_state_spec.rb +4 -5
- data/spec/unit/inspection_spec.rb +42 -53
- data/spec/unit/localizer_spec.rb +22 -18
- data/spec/unit/memory_leak_spec.rb +2 -2
- data/spec/unit/new_dsl_spec.rb +2 -2
- data/spec/unit/persistence/active_record_persistence_spec.rb +357 -89
- data/spec/unit/persistence/mongoid_persistance_spec.rb +102 -81
- data/spec/unit/persistence/sequel_persistence_spec.rb +103 -0
- data/spec/unit/reloading_spec.rb +15 -0
- data/spec/unit/simple_example_spec.rb +20 -21
- data/spec/unit/state_spec.rb +16 -16
- data/spec/unit/subclassing_spec.rb +8 -8
- data/spec/unit/transition_spec.rb +59 -44
- metadata +38 -96
- data/lib/aasm/deprecated/aasm.rb +0 -15
- data/spec/models/callback_old_dsl.rb +0 -41
- data/spec/schema.rb +0 -35
data/spec/unit/callbacks_spec.rb
CHANGED
@@ -1,36 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe 'callbacks for the old DSL' do
|
4
|
-
let(:callback) {CallbackOldDsl.new}
|
5
|
-
|
6
|
-
it "should get close callbacks" do
|
7
|
-
callback.should_receive(:exit_open).once.ordered
|
8
|
-
callback.should_receive(:before).once.ordered
|
9
|
-
callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
|
10
|
-
callback.should_receive(:before_enter_closed).once.ordered
|
11
|
-
callback.should_receive(:enter_closed).once.ordered
|
12
|
-
callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
13
|
-
callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
|
14
|
-
callback.should_receive(:after_enter_closed).once.ordered
|
15
|
-
callback.should_receive(:after).once.ordered
|
16
|
-
|
17
|
-
callback.close!
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
3
|
describe 'callbacks for the new DSL' do
|
22
4
|
let(:callback) {CallbackNewDsl.new}
|
23
5
|
|
24
6
|
it "be called in order" do
|
25
|
-
callback.
|
26
|
-
callback.
|
27
|
-
callback.
|
28
|
-
callback.
|
29
|
-
callback.
|
30
|
-
callback.
|
31
|
-
callback.
|
32
|
-
callback.
|
33
|
-
callback.
|
7
|
+
expect(callback).to receive(:exit_open).once.ordered
|
8
|
+
expect(callback).to receive(:before).once.ordered
|
9
|
+
expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes
|
10
|
+
expect(callback).to receive(:before_enter_closed).once.ordered
|
11
|
+
expect(callback).to receive(:enter_closed).once.ordered
|
12
|
+
expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
13
|
+
expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes
|
14
|
+
expect(callback).to receive(:after_enter_closed).once.ordered
|
15
|
+
expect(callback).to receive(:after).once.ordered
|
34
16
|
|
35
17
|
callback.close!
|
36
18
|
end
|
@@ -40,8 +22,10 @@ describe 'event callbacks' do
|
|
40
22
|
describe "with an error callback defined" do
|
41
23
|
before do
|
42
24
|
class Foo
|
43
|
-
|
44
|
-
|
25
|
+
aasm do
|
26
|
+
event :safe_close, :success => :success_callback, :error => :error_callback do
|
27
|
+
transitions :to => :closed, :from => [:open]
|
28
|
+
end
|
45
29
|
end
|
46
30
|
end
|
47
31
|
|
@@ -51,20 +35,20 @@ describe 'event callbacks' do
|
|
51
35
|
it "should run error_callback if an exception is raised and error_callback defined" do
|
52
36
|
def @foo.error_callback(e); end
|
53
37
|
|
54
|
-
@foo.
|
55
|
-
@foo.
|
38
|
+
allow(@foo).to receive(:enter).and_raise(e=StandardError.new)
|
39
|
+
expect(@foo).to receive(:error_callback).with(e)
|
56
40
|
|
57
41
|
@foo.safe_close!
|
58
42
|
end
|
59
43
|
|
60
44
|
it "should raise NoMethodError if exceptionis raised and error_callback is declared but not defined" do
|
61
|
-
@foo.
|
62
|
-
|
45
|
+
allow(@foo).to receive(:enter).and_raise(StandardError)
|
46
|
+
expect{@foo.safe_close!}.to raise_error(NoMethodError)
|
63
47
|
end
|
64
48
|
|
65
49
|
it "should propagate an error if no error callback is declared" do
|
66
|
-
@foo.
|
67
|
-
|
50
|
+
allow(@foo).to receive(:enter).and_raise("Cannot enter safe")
|
51
|
+
expect{@foo.close!}.to raise_error(StandardError, "Cannot enter safe")
|
68
52
|
end
|
69
53
|
end
|
70
54
|
|
@@ -75,18 +59,18 @@ describe 'event callbacks' do
|
|
75
59
|
end
|
76
60
|
|
77
61
|
it 'should call it for successful bang fire' do
|
78
|
-
@foo.
|
62
|
+
expect(@foo).to receive(:aasm_event_fired).with(:close, :open, :closed)
|
79
63
|
@foo.close!
|
80
64
|
end
|
81
65
|
|
82
66
|
it 'should call it for successful non-bang fire' do
|
83
|
-
@foo.
|
67
|
+
expect(@foo).to receive(:aasm_event_fired)
|
84
68
|
@foo.close
|
85
69
|
end
|
86
70
|
|
87
71
|
it 'should not call it for failing bang fire' do
|
88
|
-
@foo.aasm.
|
89
|
-
@foo.
|
72
|
+
allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
|
73
|
+
expect(@foo).not_to receive(:aasm_event_fired)
|
90
74
|
@foo.close!
|
91
75
|
end
|
92
76
|
end
|
@@ -98,18 +82,18 @@ describe 'event callbacks' do
|
|
98
82
|
end
|
99
83
|
|
100
84
|
it 'should call it when transition failed for bang fire' do
|
101
|
-
@foo.
|
102
|
-
|
85
|
+
expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
|
86
|
+
expect {@foo.null!}.to raise_error(AASM::InvalidTransition)
|
103
87
|
end
|
104
88
|
|
105
89
|
it 'should call it when transition failed for non-bang fire' do
|
106
|
-
@foo.
|
107
|
-
|
90
|
+
expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
|
91
|
+
expect {@foo.null}.to raise_error(AASM::InvalidTransition)
|
108
92
|
end
|
109
93
|
|
110
94
|
it 'should not call it if persist fails for bang fire' do
|
111
|
-
@foo.aasm.
|
112
|
-
@foo.
|
95
|
+
allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
|
96
|
+
expect(@foo).to receive(:aasm_event_failed)
|
113
97
|
@foo.close!
|
114
98
|
end
|
115
99
|
end
|
@@ -4,12 +4,12 @@ describe 'on initialization' do
|
|
4
4
|
let(:auth) {AuthMachine.new}
|
5
5
|
|
6
6
|
it 'should be in the pending state' do
|
7
|
-
auth.
|
7
|
+
expect(auth.aasm.current_state).to eq(:pending)
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'should have an activation code' do
|
11
|
-
auth.has_activation_code
|
12
|
-
auth.activation_code.
|
11
|
+
expect(auth.has_activation_code?).to be_true
|
12
|
+
expect(auth.activation_code).not_to be_nil
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -19,26 +19,26 @@ describe 'when being unsuspended' do
|
|
19
19
|
it 'should be able to be unsuspended' do
|
20
20
|
auth.activate!
|
21
21
|
auth.suspend!
|
22
|
-
auth.may_unsuspend
|
22
|
+
expect(auth.may_unsuspend?).to be_true
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'should not be able to be unsuspended into active' do
|
26
26
|
auth.suspend!
|
27
|
-
auth.may_unsuspend?(:active).
|
27
|
+
expect(auth.may_unsuspend?(:active)).not_to be_true
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should be able to be unsuspended into active if polite' do
|
31
31
|
auth.suspend!
|
32
|
-
auth.may_wait?(:waiting, :please).
|
32
|
+
expect(auth.may_wait?(:waiting, :please)).to be_true
|
33
33
|
auth.wait!(nil, :please)
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'should not be able to be unsuspended into active if not polite' do
|
37
37
|
auth.suspend!
|
38
|
-
auth.may_wait?(:waiting).
|
39
|
-
auth.may_wait?(:waiting, :rude).
|
40
|
-
|
41
|
-
|
38
|
+
expect(auth.may_wait?(:waiting)).not_to be_true
|
39
|
+
expect(auth.may_wait?(:waiting, :rude)).not_to be_true
|
40
|
+
expect {auth.wait!(nil, :rude)}.to raise_error(AASM::InvalidTransition)
|
41
|
+
expect {auth.wait!}.to raise_error(AASM::InvalidTransition)
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'should not be able to be unpassified' do
|
@@ -46,8 +46,8 @@ describe 'when being unsuspended' do
|
|
46
46
|
auth.suspend!
|
47
47
|
auth.unsuspend!
|
48
48
|
|
49
|
-
auth.may_unpassify
|
50
|
-
|
49
|
+
expect(auth.may_unpassify?).not_to be_true
|
50
|
+
expect {auth.unpassify!}.to raise_error(AASM::InvalidTransition)
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'should be active if previously activated' do
|
@@ -55,14 +55,14 @@ describe 'when being unsuspended' do
|
|
55
55
|
auth.suspend!
|
56
56
|
auth.unsuspend!
|
57
57
|
|
58
|
-
auth.
|
58
|
+
expect(auth.aasm.current_state).to eq(:active)
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'should be pending if not previously activated, but an activation code is present' do
|
62
62
|
auth.suspend!
|
63
63
|
auth.unsuspend!
|
64
64
|
|
65
|
-
auth.
|
65
|
+
expect(auth.aasm.current_state).to eq(:pending)
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'should be passive if not previously activated and there is no activation code' do
|
@@ -70,6 +70,15 @@ describe 'when being unsuspended' do
|
|
70
70
|
auth.suspend!
|
71
71
|
auth.unsuspend!
|
72
72
|
|
73
|
-
auth.
|
73
|
+
expect(auth.aasm.current_state).to eq(:passive)
|
74
74
|
end
|
75
|
+
|
76
|
+
it "should be able to fire known events" do
|
77
|
+
expect(auth.aasm.may_fire_event?(:activate)).to be_true
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should not be able to fire unknown events" do
|
81
|
+
expect(auth.aasm.may_fire_event?(:unknown)).to be_false
|
82
|
+
end
|
83
|
+
|
75
84
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class SimpleStateMachine
|
4
|
+
include AASM
|
5
|
+
|
6
|
+
aasm do
|
7
|
+
state :init, :initial => true
|
8
|
+
state :failed
|
9
|
+
|
10
|
+
event :failed do
|
11
|
+
transitions :from => :init, :to => :failed
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "event naming" do
|
17
|
+
let(:state_machine) { SimpleStateMachine.new }
|
18
|
+
|
19
|
+
it "allows an event of failed without blowing the stack" do
|
20
|
+
state_machine.failed
|
21
|
+
|
22
|
+
expect { state_machine.failed }.to raise_error(AASM::InvalidTransition)
|
23
|
+
end
|
24
|
+
end
|
data/spec/unit/event_spec.rb
CHANGED
@@ -10,27 +10,27 @@ describe 'adding an event' do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should set the name' do
|
13
|
-
event.name.
|
13
|
+
expect(event.name).to eq(:close_order)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should set the success callback' do
|
17
|
-
event.options[:success].
|
17
|
+
expect(event.options[:success]).to eq(:success_callback)
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'should set the after callback' do
|
21
|
-
event.options[:after].
|
21
|
+
expect(event.options[:after]).to eq([:after_callback])
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'should set the before callback' do
|
25
|
-
event.options[:before].
|
25
|
+
expect(event.options[:before]).to eq([:before_callback])
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'should create transitions' do
|
29
|
-
transitions = event.
|
30
|
-
transitions[0].from.
|
31
|
-
transitions[0].to.
|
32
|
-
transitions[1].from.
|
33
|
-
transitions[1].to.
|
29
|
+
transitions = event.transitions
|
30
|
+
expect(transitions[0].from).to eq(:open)
|
31
|
+
expect(transitions[0].to).to eq(:closed)
|
32
|
+
expect(transitions[1].from).to eq(:received)
|
33
|
+
expect(transitions[1].to).to eq(:closed)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -42,29 +42,45 @@ describe 'transition inspection' do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'should support inspecting transitions from other states' do
|
45
|
-
event.transitions_from_state(:sleeping).map(&:to).
|
46
|
-
event.transitions_from_state?(:sleeping).
|
45
|
+
expect(event.transitions_from_state(:sleeping).map(&:to)).to eq([:running])
|
46
|
+
expect(event.transitions_from_state?(:sleeping)).to be_true
|
47
47
|
|
48
|
-
event.transitions_from_state(:cleaning).map(&:to).
|
49
|
-
event.transitions_from_state?(:cleaning).
|
48
|
+
expect(event.transitions_from_state(:cleaning).map(&:to)).to eq([])
|
49
|
+
expect(event.transitions_from_state?(:cleaning)).to be_false
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'should support inspecting transitions to other states' do
|
53
|
-
event.transitions_to_state(:running).map(&:from).
|
54
|
-
event.transitions_to_state?(:running).
|
53
|
+
expect(event.transitions_to_state(:running).map(&:from)).to eq([:sleeping])
|
54
|
+
expect(event.transitions_to_state?(:running)).to be_true
|
55
55
|
|
56
|
-
event.transitions_to_state(:cleaning).map(&:to).
|
57
|
-
event.transitions_to_state?(:cleaning).
|
56
|
+
expect(event.transitions_to_state(:cleaning).map(&:to)).to eq([])
|
57
|
+
expect(event.transitions_to_state?(:cleaning)).to be_false
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
+
describe 'transition inspection without from' do
|
62
|
+
let(:event) do
|
63
|
+
AASM::Event.new(:run) do
|
64
|
+
transitions :to => :running
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should support inspecting transitions from other states' do
|
69
|
+
expect(event.transitions_from_state(:sleeping).map(&:to)).to eq([:running])
|
70
|
+
expect(event.transitions_from_state?(:sleeping)).to be_true
|
71
|
+
|
72
|
+
expect(event.transitions_from_state(:cleaning).map(&:to)).to eq([:running])
|
73
|
+
expect(event.transitions_from_state?(:cleaning)).to be_true
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
61
78
|
describe 'firing an event' do
|
62
79
|
it 'should return nil if the transitions are empty' do
|
63
|
-
obj = double('object')
|
64
|
-
obj.stub(:aasm_current_state)
|
80
|
+
obj = double('object', :aasm => double('aasm', :current_state => 'open'))
|
65
81
|
|
66
82
|
event = AASM::Event.new(:event)
|
67
|
-
event.fire(obj).
|
83
|
+
expect(event.fire(obj)).to be_nil
|
68
84
|
end
|
69
85
|
|
70
86
|
it 'should return the state of the first matching transition it finds' do
|
@@ -72,10 +88,9 @@ describe 'firing an event' do
|
|
72
88
|
transitions :to => :closed, :from => [:open, :received]
|
73
89
|
end
|
74
90
|
|
75
|
-
obj = double('object')
|
76
|
-
obj.stub(:aasm_current_state).and_return(:open)
|
91
|
+
obj = double('object', :aasm => double('aasm', :current_state => :open))
|
77
92
|
|
78
|
-
event.fire(obj).
|
93
|
+
expect(event.fire(obj)).to eq(:closed)
|
79
94
|
end
|
80
95
|
|
81
96
|
it 'should call the guard with the params passed in' do
|
@@ -83,11 +98,10 @@ describe 'firing an event' do
|
|
83
98
|
transitions :to => :closed, :from => [:open, :received], :guard => :guard_fn
|
84
99
|
end
|
85
100
|
|
86
|
-
obj = double('object')
|
87
|
-
obj.
|
88
|
-
obj.should_receive(:guard_fn).with('arg1', 'arg2').and_return(true)
|
101
|
+
obj = double('object', :aasm => double('aasm', :current_state => :open))
|
102
|
+
expect(obj).to receive(:guard_fn).with('arg1', 'arg2').and_return(true)
|
89
103
|
|
90
|
-
event.fire(obj, nil, 'arg1', 'arg2').
|
104
|
+
expect(event.fire(obj, nil, 'arg1', 'arg2')).to eq(:closed)
|
91
105
|
end
|
92
106
|
|
93
107
|
end
|
@@ -96,64 +110,74 @@ describe 'should fire callbacks' do
|
|
96
110
|
describe 'success' do
|
97
111
|
it "if it's a symbol" do
|
98
112
|
ThisNameBetterNotBeInUse.instance_eval {
|
99
|
-
|
100
|
-
|
113
|
+
aasm do
|
114
|
+
event :with_symbol, :success => :symbol_success_callback do
|
115
|
+
transitions :to => :symbol, :from => [:initial]
|
116
|
+
end
|
101
117
|
end
|
102
118
|
}
|
103
119
|
|
104
120
|
model = ThisNameBetterNotBeInUse.new
|
105
|
-
model.
|
121
|
+
expect(model).to receive(:symbol_success_callback)
|
106
122
|
model.with_symbol!
|
107
123
|
end
|
108
124
|
|
109
125
|
it "if it's a string" do
|
110
126
|
ThisNameBetterNotBeInUse.instance_eval {
|
111
|
-
|
112
|
-
|
127
|
+
aasm do
|
128
|
+
event :with_string, :success => 'string_success_callback' do
|
129
|
+
transitions :to => :string, :from => [:initial]
|
130
|
+
end
|
113
131
|
end
|
114
132
|
}
|
115
133
|
|
116
134
|
model = ThisNameBetterNotBeInUse.new
|
117
|
-
model.
|
135
|
+
expect(model).to receive(:string_success_callback)
|
118
136
|
model.with_string!
|
119
137
|
end
|
120
138
|
|
121
139
|
it "if passed an array of strings and/or symbols" do
|
122
140
|
ThisNameBetterNotBeInUse.instance_eval {
|
123
|
-
|
124
|
-
|
141
|
+
aasm do
|
142
|
+
event :with_array, :success => [:success_callback1, 'success_callback2'] do
|
143
|
+
transitions :to => :array, :from => [:initial]
|
144
|
+
end
|
125
145
|
end
|
126
146
|
}
|
127
147
|
|
128
148
|
model = ThisNameBetterNotBeInUse.new
|
129
|
-
model.
|
130
|
-
model.
|
149
|
+
expect(model).to receive(:success_callback1)
|
150
|
+
expect(model).to receive(:success_callback2)
|
131
151
|
model.with_array!
|
132
152
|
end
|
133
153
|
|
134
154
|
it "if passed an array of strings and/or symbols and/or procs" do
|
135
155
|
ThisNameBetterNotBeInUse.instance_eval {
|
136
|
-
|
137
|
-
|
156
|
+
aasm do
|
157
|
+
event :with_array_including_procs, :success => [:success_callback1, 'success_callback2', lambda { proc_success_callback }] do
|
158
|
+
transitions :to => :array, :from => [:initial]
|
159
|
+
end
|
138
160
|
end
|
139
161
|
}
|
140
162
|
|
141
163
|
model = ThisNameBetterNotBeInUse.new
|
142
|
-
model.
|
143
|
-
model.
|
144
|
-
model.
|
164
|
+
expect(model).to receive(:success_callback1)
|
165
|
+
expect(model).to receive(:success_callback2)
|
166
|
+
expect(model).to receive(:proc_success_callback)
|
145
167
|
model.with_array_including_procs!
|
146
168
|
end
|
147
169
|
|
148
170
|
it "if it's a proc" do
|
149
171
|
ThisNameBetterNotBeInUse.instance_eval {
|
150
|
-
|
151
|
-
|
172
|
+
aasm do
|
173
|
+
event :with_proc, :success => lambda { proc_success_callback } do
|
174
|
+
transitions :to => :proc, :from => [:initial]
|
175
|
+
end
|
152
176
|
end
|
153
177
|
}
|
154
178
|
|
155
179
|
model = ThisNameBetterNotBeInUse.new
|
156
|
-
model.
|
180
|
+
expect(model).to receive(:proc_success_callback)
|
157
181
|
model.with_proc!
|
158
182
|
end
|
159
183
|
end
|
@@ -161,21 +185,23 @@ describe 'should fire callbacks' do
|
|
161
185
|
describe 'after' do
|
162
186
|
it "if they set different ways" do
|
163
187
|
ThisNameBetterNotBeInUse.instance_eval do
|
164
|
-
|
165
|
-
after do
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
188
|
+
aasm do
|
189
|
+
event :with_afters, :after => :do_one_thing_after do
|
190
|
+
after do
|
191
|
+
do_another_thing_after_too
|
192
|
+
end
|
193
|
+
after do
|
194
|
+
do_third_thing_at_last
|
195
|
+
end
|
196
|
+
transitions :to => :proc, :from => [:initial]
|
170
197
|
end
|
171
|
-
transitions :to => :proc, :from => [:initial]
|
172
198
|
end
|
173
199
|
end
|
174
200
|
|
175
201
|
model = ThisNameBetterNotBeInUse.new
|
176
|
-
model.
|
177
|
-
model.
|
178
|
-
model.
|
202
|
+
expect(model).to receive(:do_one_thing_after).once.ordered
|
203
|
+
expect(model).to receive(:do_another_thing_after_too).once.ordered
|
204
|
+
expect(model).to receive(:do_third_thing_at_last).once.ordered
|
179
205
|
model.with_afters!
|
180
206
|
end
|
181
207
|
end
|
@@ -183,76 +209,98 @@ describe 'should fire callbacks' do
|
|
183
209
|
describe 'before' do
|
184
210
|
it "if it's a proc" do
|
185
211
|
ThisNameBetterNotBeInUse.instance_eval do
|
186
|
-
|
187
|
-
|
188
|
-
|
212
|
+
aasm do
|
213
|
+
event :before_as_proc do
|
214
|
+
before do
|
215
|
+
do_something_before
|
216
|
+
end
|
217
|
+
transitions :to => :proc, :from => [:initial]
|
189
218
|
end
|
190
|
-
transitions :to => :proc, :from => [:initial]
|
191
219
|
end
|
192
220
|
end
|
193
221
|
|
194
222
|
model = ThisNameBetterNotBeInUse.new
|
195
|
-
model.
|
223
|
+
expect(model).to receive(:do_something_before).once
|
196
224
|
model.before_as_proc!
|
197
225
|
end
|
198
226
|
end
|
199
227
|
|
200
228
|
it 'in right order' do
|
201
229
|
ThisNameBetterNotBeInUse.instance_eval do
|
202
|
-
|
203
|
-
|
204
|
-
|
230
|
+
aasm do
|
231
|
+
event :in_right_order, :after => :do_something_after do
|
232
|
+
before do
|
233
|
+
do_something_before
|
234
|
+
end
|
235
|
+
transitions :to => :proc, :from => [:initial]
|
205
236
|
end
|
206
|
-
transitions :to => :proc, :from => [:initial]
|
207
237
|
end
|
208
238
|
end
|
209
239
|
|
210
240
|
model = ThisNameBetterNotBeInUse.new
|
211
|
-
model.
|
212
|
-
model.
|
241
|
+
expect(model).to receive(:do_something_before).once.ordered
|
242
|
+
expect(model).to receive(:do_something_after).once.ordered
|
213
243
|
model.in_right_order!
|
214
244
|
end
|
215
245
|
end
|
216
246
|
|
247
|
+
describe 'current event' do
|
248
|
+
let(:pe) {ParametrisedEvent.new}
|
249
|
+
|
250
|
+
it 'if no event has been triggered' do
|
251
|
+
expect(pe.aasm.current_event).to be_nil
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'if a event has been triggered' do
|
255
|
+
pe.wakeup
|
256
|
+
expect(pe.aasm.current_event).to eql :wakeup
|
257
|
+
end
|
258
|
+
|
259
|
+
it 'if no event has been triggered' do
|
260
|
+
pe.wakeup!
|
261
|
+
expect(pe.aasm.current_event).to eql :wakeup!
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
217
265
|
describe 'parametrised events' do
|
218
266
|
let(:pe) {ParametrisedEvent.new}
|
219
267
|
|
220
268
|
it 'should transition to specified next state (sleeping to showering)' do
|
221
269
|
pe.wakeup!(:showering)
|
222
|
-
pe.
|
270
|
+
expect(pe.aasm.current_state).to eq(:showering)
|
223
271
|
end
|
224
272
|
|
225
273
|
it 'should transition to specified next state (sleeping to working)' do
|
226
274
|
pe.wakeup!(:working)
|
227
|
-
pe.
|
275
|
+
expect(pe.aasm.current_state).to eq(:working)
|
228
276
|
end
|
229
277
|
|
230
278
|
it 'should transition to default (first or showering) state' do
|
231
279
|
pe.wakeup!
|
232
|
-
pe.
|
280
|
+
expect(pe.aasm.current_state).to eq(:showering)
|
233
281
|
end
|
234
282
|
|
235
283
|
it 'should transition to default state when on_transition invoked' do
|
236
284
|
pe.dress!(nil, 'purple', 'dressy')
|
237
|
-
pe.
|
285
|
+
expect(pe.aasm.current_state).to eq(:working)
|
238
286
|
end
|
239
287
|
|
240
288
|
it 'should call on_transition method with args' do
|
241
289
|
pe.wakeup!(:showering)
|
242
|
-
pe.
|
290
|
+
expect(pe).to receive(:wear_clothes).with('blue', 'jeans')
|
243
291
|
pe.dress!(:working, 'blue', 'jeans')
|
244
292
|
end
|
245
293
|
|
246
294
|
it 'should call on_transition proc' do
|
247
295
|
pe.wakeup!(:showering)
|
248
|
-
pe.
|
296
|
+
expect(pe).to receive(:wear_clothes).with('purple', 'slacks')
|
249
297
|
pe.dress!(:dating, 'purple', 'slacks')
|
250
298
|
end
|
251
299
|
|
252
300
|
it 'should call on_transition with an array of methods' do
|
253
301
|
pe.wakeup!(:showering)
|
254
|
-
pe.
|
255
|
-
pe.
|
302
|
+
expect(pe).to receive(:condition_hair)
|
303
|
+
expect(pe).to receive(:fix_hair)
|
256
304
|
pe.dress!(:prettying_up)
|
257
305
|
end
|
258
306
|
end
|
@@ -261,9 +309,9 @@ describe 'event firing without persistence' do
|
|
261
309
|
it 'should attempt to persist if aasm_write_state is defined' do
|
262
310
|
foo = Foo.new
|
263
311
|
def foo.aasm_write_state; end
|
264
|
-
foo.
|
312
|
+
expect(foo).to be_open
|
265
313
|
|
266
|
-
foo.
|
314
|
+
expect(foo).to receive(:aasm_write_state_without_persistence)
|
267
315
|
foo.close
|
268
316
|
end
|
269
317
|
end
|