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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -0
  3. data/.travis.yml +29 -4
  4. data/CHANGELOG.md +56 -0
  5. data/Gemfile +10 -1
  6. data/LICENSE +1 -1
  7. data/README.md +151 -20
  8. data/aasm.gemspec +5 -6
  9. data/gemfiles/rails_3.2.gemfile +13 -0
  10. data/gemfiles/rails_4.0.gemfile +16 -0
  11. data/gemfiles/rails_4.1.gemfile +16 -0
  12. data/lib/aasm/aasm.rb +36 -32
  13. data/lib/aasm/base.rb +49 -31
  14. data/lib/aasm/event.rb +28 -17
  15. data/lib/aasm/instance_base.rb +9 -4
  16. data/lib/aasm/localizer.rb +1 -1
  17. data/lib/aasm/persistence/active_record_persistence.rb +65 -16
  18. data/lib/aasm/persistence/base.rb +10 -14
  19. data/lib/aasm/persistence/mongoid_persistence.rb +10 -8
  20. data/lib/aasm/persistence/sequel_persistence.rb +108 -0
  21. data/lib/aasm/persistence.rb +3 -0
  22. data/lib/aasm/state.rb +4 -3
  23. data/lib/aasm/state_machine.rb +18 -10
  24. data/lib/aasm/transition.rb +13 -6
  25. data/lib/aasm/version.rb +1 -1
  26. data/lib/aasm.rb +0 -3
  27. data/spec/database.rb +33 -0
  28. data/spec/models/double_definer.rb +21 -0
  29. data/spec/models/foo.rb +2 -1
  30. data/spec/models/guardian.rb +48 -0
  31. data/spec/models/mongoid/no_scope_mongoid.rb +1 -1
  32. data/spec/models/mongoid/simple_mongoid.rb +5 -4
  33. data/spec/models/mongoid/simple_new_dsl_mongoid.rb +1 -1
  34. data/spec/models/not_auto_loaded/process.rb +10 -8
  35. data/spec/models/persistence.rb +5 -13
  36. data/spec/spec_helper.rb +1 -1
  37. data/spec/unit/api_spec.rb +12 -12
  38. data/spec/unit/callbacks_spec.rb +29 -45
  39. data/spec/unit/complex_example_spec.rb +24 -15
  40. data/spec/unit/event_naming_spec.rb +24 -0
  41. data/spec/unit/event_spec.rb +124 -76
  42. data/spec/unit/guard_spec.rb +60 -0
  43. data/spec/unit/initial_state_spec.rb +4 -5
  44. data/spec/unit/inspection_spec.rb +42 -53
  45. data/spec/unit/localizer_spec.rb +22 -18
  46. data/spec/unit/memory_leak_spec.rb +2 -2
  47. data/spec/unit/new_dsl_spec.rb +2 -2
  48. data/spec/unit/persistence/active_record_persistence_spec.rb +357 -89
  49. data/spec/unit/persistence/mongoid_persistance_spec.rb +102 -81
  50. data/spec/unit/persistence/sequel_persistence_spec.rb +103 -0
  51. data/spec/unit/reloading_spec.rb +15 -0
  52. data/spec/unit/simple_example_spec.rb +20 -21
  53. data/spec/unit/state_spec.rb +16 -16
  54. data/spec/unit/subclassing_spec.rb +8 -8
  55. data/spec/unit/transition_spec.rb +59 -44
  56. metadata +38 -96
  57. data/lib/aasm/deprecated/aasm.rb +0 -15
  58. data/spec/models/callback_old_dsl.rb +0 -41
  59. data/spec/schema.rb +0 -35
@@ -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.should_receive(:exit_open).once.ordered
26
- callback.should_receive(:before).once.ordered
27
- callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
28
- callback.should_receive(:before_enter_closed).once.ordered
29
- callback.should_receive(:enter_closed).once.ordered
30
- callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
31
- callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
32
- callback.should_receive(:after_enter_closed).once.ordered
33
- callback.should_receive(:after).once.ordered
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
- aasm_event :safe_close, :success => :success_callback, :error => :error_callback do
44
- transitions :to => :closed, :from => [:open]
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.stub(:enter).and_raise(e=StandardError.new)
55
- @foo.should_receive(:error_callback).with(e)
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.stub(:enter).and_raise(StandardError)
62
- lambda{@foo.safe_close!}.should raise_error(NoMethodError)
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.stub(:enter).and_raise("Cannot enter safe")
67
- lambda{@foo.close!}.should raise_error(StandardError, "Cannot enter safe")
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.should_receive(:aasm_event_fired).with(:close, :open, :closed)
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.should_receive(:aasm_event_fired)
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.stub(:set_current_state_with_persistence).and_return(false)
89
- @foo.should_not_receive(:aasm_event_fired)
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.should_receive(:aasm_event_failed).with(:null, :open)
102
- lambda {@foo.null!}.should raise_error(AASM::InvalidTransition)
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.should_receive(:aasm_event_failed).with(:null, :open)
107
- lambda {@foo.null}.should raise_error(AASM::InvalidTransition)
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.stub(:set_current_state_with_persistence).and_return(false)
112
- @foo.should_receive(:aasm_event_failed)
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.aasm_current_state.should == :pending
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?.should be_true
12
- auth.activation_code.should_not be_nil
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?.should be_true
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).should_not be_true
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).should be_true
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).should_not be_true
39
- auth.may_wait?(:waiting, :rude).should_not be_true
40
- lambda {auth.wait!(nil, :rude)}.should raise_error(AASM::InvalidTransition)
41
- lambda {auth.wait!}.should raise_error(AASM::InvalidTransition)
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?.should_not be_true
50
- lambda {auth.unpassify!}.should raise_error(AASM::InvalidTransition)
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.aasm_current_state.should == :active
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.aasm_current_state.should == :pending
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.aasm_current_state.should == :passive
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
@@ -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.should == :close_order
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].should == :success_callback
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].should == [:after_callback]
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].should == [:before_callback]
25
+ expect(event.options[:before]).to eq([:before_callback])
26
26
  end
27
27
 
28
28
  it 'should create transitions' do
29
- transitions = event.all_transitions
30
- transitions[0].from.should == :open
31
- transitions[0].to.should == :closed
32
- transitions[1].from.should == :received
33
- transitions[1].to.should == :closed
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).should == [:running]
46
- event.transitions_from_state?(:sleeping).should be_true
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).should == []
49
- event.transitions_from_state?(:cleaning).should be_false
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).should == [:sleeping]
54
- event.transitions_to_state?(:running).should be_true
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).should == []
57
- event.transitions_to_state?(:cleaning).should be_false
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).should be_nil
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).should == :closed
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.stub(:aasm_current_state).and_return(:open)
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').should == :closed
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
- aasm_event :with_symbol, :success => :symbol_success_callback do
100
- transitions :to => :symbol, :from => [:initial]
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.should_receive(:symbol_success_callback)
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
- aasm_event :with_string, :success => 'string_success_callback' do
112
- transitions :to => :string, :from => [:initial]
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.should_receive(:string_success_callback)
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
- aasm_event :with_array, :success => [:success_callback1, 'success_callback2'] do
124
- transitions :to => :array, :from => [:initial]
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.should_receive(:success_callback1)
130
- model.should_receive(:success_callback2)
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
- aasm_event :with_array_including_procs, :success => [:success_callback1, 'success_callback2', lambda { proc_success_callback }] do
137
- transitions :to => :array, :from => [:initial]
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.should_receive(:success_callback1)
143
- model.should_receive(:success_callback2)
144
- model.should_receive(:proc_success_callback)
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
- aasm_event :with_proc, :success => lambda { proc_success_callback } do
151
- transitions :to => :proc, :from => [:initial]
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.should_receive(:proc_success_callback)
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
- aasm_event :with_afters, :after => :do_one_thing_after do
165
- after do
166
- do_another_thing_after_too
167
- end
168
- after do
169
- do_third_thing_at_last
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.should_receive(:do_one_thing_after).once.ordered
177
- model.should_receive(:do_another_thing_after_too).once.ordered
178
- model.should_receive(:do_third_thing_at_last).once.ordered
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
- aasm_event :before_as_proc do
187
- before do
188
- do_something_before
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.should_receive(:do_something_before).once
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
- aasm_event :in_right_order, :after => :do_something_after do
203
- before do
204
- do_something_before
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.should_receive(:do_something_before).once.ordered
212
- model.should_receive(:do_something_after).once.ordered
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.aasm_current_state.should == :showering
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.aasm_current_state.should == :working
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.aasm_current_state.should == :showering
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.aasm_current_state.should == :working
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.should_receive(:wear_clothes).with('blue', 'jeans')
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.should_receive(:wear_clothes).with('purple', 'slacks')
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.should_receive(:condition_hair)
255
- pe.should_receive(:fix_hair)
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.should be_open
312
+ expect(foo).to be_open
265
313
 
266
- foo.should_receive(:aasm_write_state_without_persistence)
314
+ expect(foo).to receive(:aasm_write_state_without_persistence)
267
315
  foo.close
268
316
  end
269
317
  end