aasm 3.0.25 → 3.1.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 +3 -0
- data/.travis.yml +7 -6
- data/CHANGELOG.md +14 -2
- data/README.md +56 -4
- data/aasm.gemspec +1 -1
- data/gemfiles/rails_3.2.gemfile +3 -2
- data/gemfiles/rails_4.0.gemfile +3 -2
- data/gemfiles/rails_4.1.gemfile +11 -0
- data/lib/aasm/base.rb +21 -15
- data/lib/aasm/event.rb +15 -4
- data/lib/aasm/instance_base.rb +2 -0
- data/lib/aasm/persistence/active_record_persistence.rb +17 -1
- data/lib/aasm/state.rb +1 -0
- data/lib/aasm/transition.rb +13 -6
- data/lib/aasm/version.rb +1 -1
- data/spec/database.rb +33 -0
- data/spec/models/guardian.rb +48 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/api_spec.rb +12 -12
- data/spec/unit/callbacks_spec.rb +25 -25
- data/spec/unit/complex_example_spec.rb +15 -15
- data/spec/unit/event_spec.rb +44 -44
- data/spec/unit/guard_spec.rb +60 -0
- data/spec/unit/initial_state_spec.rb +3 -3
- data/spec/unit/inspection_spec.rb +36 -36
- data/spec/unit/localizer_spec.rb +14 -10
- data/spec/unit/new_dsl_spec.rb +2 -2
- data/spec/unit/persistence/active_record_persistence_spec.rb +107 -67
- data/spec/unit/persistence/mongoid_persistance_spec.rb +24 -24
- data/spec/unit/simple_example_spec.rb +20 -20
- data/spec/unit/state_spec.rb +16 -16
- data/spec/unit/subclassing_spec.rb +6 -6
- data/spec/unit/transition_spec.rb +55 -40
- metadata +26 -21
- data/spec/schema.rb +0 -35
data/spec/unit/localizer_spec.rb
CHANGED
|
@@ -30,11 +30,15 @@ describe 'localized state names' do
|
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
it 'should localize' do
|
|
33
|
-
LocalizerTestModel.aasm.states.detect {|s| s == :opened}
|
|
33
|
+
state = LocalizerTestModel.aasm.states.detect {|s| s == :opened}
|
|
34
|
+
expect(state.localized_name).to eq("It's open now!")
|
|
35
|
+
expect(state.human_name).to eq("It's open now!")
|
|
34
36
|
end
|
|
35
37
|
|
|
36
38
|
it 'should use fallback' do
|
|
37
|
-
LocalizerTestModel.aasm.states.detect {|s| s == :closed}
|
|
39
|
+
state = LocalizerTestModel.aasm.states.detect {|s| s == :closed}
|
|
40
|
+
expect(state.localized_name).to eq('Closed')
|
|
41
|
+
expect(state.human_name).to eq('Closed')
|
|
38
42
|
end
|
|
39
43
|
end
|
|
40
44
|
|
|
@@ -54,21 +58,21 @@ describe AASM::Localizer, "new style" do
|
|
|
54
58
|
|
|
55
59
|
context 'aasm.human_state' do
|
|
56
60
|
it 'should return translated state value' do
|
|
57
|
-
foo_opened.aasm.human_state.
|
|
61
|
+
expect(foo_opened.aasm.human_state).to eq("It's open now!")
|
|
58
62
|
end
|
|
59
63
|
|
|
60
64
|
it 'should return humanized value if not localized' do
|
|
61
|
-
foo_closed.aasm.human_state.
|
|
65
|
+
expect(foo_closed.aasm.human_state).to eq("Closed")
|
|
62
66
|
end
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
context 'aasm_human_event_name' do
|
|
66
70
|
it 'should return translated event name' do
|
|
67
|
-
LocalizerTestModel.aasm_human_event_name(:close).
|
|
71
|
+
expect(LocalizerTestModel.aasm_human_event_name(:close)).to eq("Let's close it!")
|
|
68
72
|
end
|
|
69
73
|
|
|
70
74
|
it 'should return humanized event name' do
|
|
71
|
-
LocalizerTestModel.aasm_human_event_name(:open).
|
|
75
|
+
expect(LocalizerTestModel.aasm_human_event_name(:open)).to eq("Open")
|
|
72
76
|
end
|
|
73
77
|
end
|
|
74
78
|
end
|
|
@@ -89,21 +93,21 @@ describe AASM::Localizer, "deprecated style" do
|
|
|
89
93
|
|
|
90
94
|
context 'aasm.human_state' do
|
|
91
95
|
it 'should return translated state value' do
|
|
92
|
-
foo_opened.aasm.human_state.
|
|
96
|
+
expect(foo_opened.aasm.human_state).to eq("It's open now!")
|
|
93
97
|
end
|
|
94
98
|
|
|
95
99
|
it 'should return humanized value if not localized' do
|
|
96
|
-
foo_closed.aasm.human_state.
|
|
100
|
+
expect(foo_closed.aasm.human_state).to eq("Closed")
|
|
97
101
|
end
|
|
98
102
|
end
|
|
99
103
|
|
|
100
104
|
context 'aasm_human_event_name' do
|
|
101
105
|
it 'should return translated event name' do
|
|
102
|
-
LocalizerTestModel.aasm_human_event_name(:close).
|
|
106
|
+
expect(LocalizerTestModel.aasm_human_event_name(:close)).to eq("Let's close it!")
|
|
103
107
|
end
|
|
104
108
|
|
|
105
109
|
it 'should return humanized event name' do
|
|
106
|
-
LocalizerTestModel.aasm_human_event_name(:open).
|
|
110
|
+
expect(LocalizerTestModel.aasm_human_event_name(:open)).to eq("Open")
|
|
107
111
|
end
|
|
108
112
|
end
|
|
109
113
|
end
|
data/spec/unit/new_dsl_spec.rb
CHANGED
|
@@ -5,8 +5,8 @@ describe "the new dsl" do
|
|
|
5
5
|
let(:process) {ProcessWithNewDsl.new}
|
|
6
6
|
|
|
7
7
|
it 'should not conflict with other event or state methods' do
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
expect {ProcessWithNewDsl.state}.to raise_error(RuntimeError, "wrong state method")
|
|
9
|
+
expect {ProcessWithNewDsl.event}.to raise_error(RuntimeError, "wrong event method")
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
end
|
|
@@ -9,8 +9,8 @@ load_schema
|
|
|
9
9
|
|
|
10
10
|
shared_examples_for "aasm model" do
|
|
11
11
|
it "should include persistence mixins" do
|
|
12
|
-
klass.included_modules.
|
|
13
|
-
klass.included_modules.
|
|
12
|
+
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence)
|
|
13
|
+
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
|
|
@@ -18,45 +18,45 @@ describe "instance methods" do
|
|
|
18
18
|
let(:gate) {Gate.new}
|
|
19
19
|
|
|
20
20
|
it "should respond to aasm persistence methods" do
|
|
21
|
-
gate.
|
|
22
|
-
gate.
|
|
23
|
-
gate.
|
|
21
|
+
expect(gate).to respond_to(:aasm_read_state)
|
|
22
|
+
expect(gate).to respond_to(:aasm_write_state)
|
|
23
|
+
expect(gate).to respond_to(:aasm_write_state_without_persistence)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
it "should return the initial state when new and the aasm field is nil" do
|
|
27
|
-
gate.aasm.current_state.
|
|
27
|
+
expect(gate.aasm.current_state).to eq(:opened)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it "should return the aasm column when new and the aasm field is not nil" do
|
|
31
31
|
gate.aasm_state = "closed"
|
|
32
|
-
gate.aasm.current_state.
|
|
32
|
+
expect(gate.aasm.current_state).to eq(:closed)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should return the aasm column when not new and the aasm_column is not nil" do
|
|
36
|
-
gate.
|
|
36
|
+
allow(gate).to receive(:new_record?).and_return(false)
|
|
37
37
|
gate.aasm_state = "state"
|
|
38
|
-
gate.aasm.current_state.
|
|
38
|
+
expect(gate.aasm.current_state).to eq(:state)
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "should allow a nil state" do
|
|
42
|
-
gate.
|
|
42
|
+
allow(gate).to receive(:new_record?).and_return(false)
|
|
43
43
|
gate.aasm_state = nil
|
|
44
|
-
gate.aasm.current_state.
|
|
44
|
+
expect(gate.aasm.current_state).to be_nil
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
it "should call aasm_ensure_initial_state on validation before create" do
|
|
48
|
-
gate.
|
|
48
|
+
expect(gate).to receive(:aasm_ensure_initial_state).and_return(true)
|
|
49
49
|
gate.valid?
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
|
53
|
-
gate.
|
|
53
|
+
expect(gate).to receive(:aasm_ensure_initial_state).and_return(true)
|
|
54
54
|
gate.save(:validate => false)
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "should not call aasm_ensure_initial_state on validation before update" do
|
|
58
|
-
gate.
|
|
59
|
-
gate.
|
|
58
|
+
allow(gate).to receive(:new_record?).and_return(false)
|
|
59
|
+
expect(gate).not_to receive(:aasm_ensure_initial_state)
|
|
60
60
|
gate.valid?
|
|
61
61
|
end
|
|
62
62
|
|
|
@@ -64,36 +64,36 @@ end
|
|
|
64
64
|
|
|
65
65
|
describe 'subclasses' do
|
|
66
66
|
it "should have the same states as its parent class" do
|
|
67
|
-
DerivateNewDsl.aasm.states.
|
|
67
|
+
expect(DerivateNewDsl.aasm.states).to eq(SimpleNewDsl.aasm.states)
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
it "should have the same events as its parent class" do
|
|
71
|
-
DerivateNewDsl.aasm.events.
|
|
71
|
+
expect(DerivateNewDsl.aasm.events).to eq(SimpleNewDsl.aasm.events)
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
it "should have the same column as its parent even for the new dsl" do
|
|
75
|
-
SimpleNewDsl.aasm_column.
|
|
76
|
-
DerivateNewDsl.aasm_column.
|
|
75
|
+
expect(SimpleNewDsl.aasm_column).to eq(:status)
|
|
76
|
+
expect(DerivateNewDsl.aasm_column).to eq(:status)
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
describe "named scopes with the new DSL" do
|
|
81
81
|
context "Does not already respond_to? the scope name" do
|
|
82
82
|
it "should add a scope" do
|
|
83
|
-
SimpleNewDsl.
|
|
84
|
-
SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation).
|
|
83
|
+
expect(SimpleNewDsl).to respond_to(:unknown_scope)
|
|
84
|
+
expect(SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_true
|
|
85
85
|
end
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
context "Already respond_to? the scope name" do
|
|
89
89
|
it "should not add a scope" do
|
|
90
|
-
SimpleNewDsl.
|
|
91
|
-
SimpleNewDsl.new.class.
|
|
90
|
+
expect(SimpleNewDsl).to respond_to(:new)
|
|
91
|
+
expect(SimpleNewDsl.new.class).to eq(SimpleNewDsl)
|
|
92
92
|
end
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
it "does not create scopes if requested" do
|
|
96
|
-
NoScope.
|
|
96
|
+
expect(NoScope).not_to respond_to(:ignored_scope)
|
|
97
97
|
end
|
|
98
98
|
|
|
99
99
|
end
|
|
@@ -101,8 +101,8 @@ end
|
|
|
101
101
|
describe 'initial states' do
|
|
102
102
|
|
|
103
103
|
it 'should support conditions' do
|
|
104
|
-
Thief.new(:skilled => true).aasm.current_state.
|
|
105
|
-
Thief.new(:skilled => false).aasm.current_state.
|
|
104
|
+
expect(Thief.new(:skilled => true).aasm.current_state).to eq(:rich)
|
|
105
|
+
expect(Thief.new(:skilled => false).aasm.current_state).to eq(:jailed)
|
|
106
106
|
end
|
|
107
107
|
end
|
|
108
108
|
|
|
@@ -110,54 +110,54 @@ describe 'transitions with persistence' do
|
|
|
110
110
|
|
|
111
111
|
it "should work for valid models" do
|
|
112
112
|
valid_object = Validator.create(:name => 'name')
|
|
113
|
-
valid_object.
|
|
113
|
+
expect(valid_object).to be_sleeping
|
|
114
114
|
valid_object.status = :running
|
|
115
|
-
valid_object.
|
|
115
|
+
expect(valid_object).to be_running
|
|
116
116
|
end
|
|
117
117
|
|
|
118
118
|
it 'should not store states for invalid models' do
|
|
119
119
|
validator = Validator.create(:name => 'name')
|
|
120
|
-
validator.
|
|
121
|
-
validator.
|
|
120
|
+
expect(validator).to be_valid
|
|
121
|
+
expect(validator).to be_sleeping
|
|
122
122
|
|
|
123
123
|
validator.name = nil
|
|
124
|
-
validator.
|
|
125
|
-
validator.run
|
|
126
|
-
validator.
|
|
124
|
+
expect(validator).not_to be_valid
|
|
125
|
+
expect(validator.run!).to be_false
|
|
126
|
+
expect(validator).to be_sleeping
|
|
127
127
|
|
|
128
128
|
validator.reload
|
|
129
|
-
validator.
|
|
130
|
-
validator.
|
|
129
|
+
expect(validator).not_to be_running
|
|
130
|
+
expect(validator).to be_sleeping
|
|
131
131
|
|
|
132
132
|
validator.name = 'another name'
|
|
133
|
-
validator.
|
|
134
|
-
validator.run
|
|
135
|
-
validator.
|
|
133
|
+
expect(validator).to be_valid
|
|
134
|
+
expect(validator.run!).to be_true
|
|
135
|
+
expect(validator).to be_running
|
|
136
136
|
|
|
137
137
|
validator.reload
|
|
138
|
-
validator.
|
|
139
|
-
validator.
|
|
138
|
+
expect(validator).to be_running
|
|
139
|
+
expect(validator).not_to be_sleeping
|
|
140
140
|
end
|
|
141
141
|
|
|
142
142
|
it 'should store states for invalid models if configured' do
|
|
143
143
|
persistor = InvalidPersistor.create(:name => 'name')
|
|
144
|
-
persistor.
|
|
145
|
-
persistor.
|
|
144
|
+
expect(persistor).to be_valid
|
|
145
|
+
expect(persistor).to be_sleeping
|
|
146
146
|
|
|
147
147
|
persistor.name = nil
|
|
148
|
-
persistor.
|
|
149
|
-
persistor.run
|
|
150
|
-
persistor.
|
|
148
|
+
expect(persistor).not_to be_valid
|
|
149
|
+
expect(persistor.run!).to be_true
|
|
150
|
+
expect(persistor).to be_running
|
|
151
151
|
|
|
152
152
|
persistor = InvalidPersistor.find(persistor.id)
|
|
153
153
|
persistor.valid?
|
|
154
|
-
persistor.
|
|
155
|
-
persistor.
|
|
156
|
-
persistor.
|
|
154
|
+
expect(persistor).to be_valid
|
|
155
|
+
expect(persistor).to be_running
|
|
156
|
+
expect(persistor).not_to be_sleeping
|
|
157
157
|
|
|
158
158
|
persistor.reload
|
|
159
|
-
persistor.
|
|
160
|
-
persistor.
|
|
159
|
+
expect(persistor).to be_running
|
|
160
|
+
expect(persistor).not_to be_sleeping
|
|
161
161
|
end
|
|
162
162
|
|
|
163
163
|
describe 'transactions' do
|
|
@@ -165,41 +165,81 @@ describe 'transitions with persistence' do
|
|
|
165
165
|
let(:transactor) { Transactor.create!(:name => 'transactor', :worker => worker) }
|
|
166
166
|
|
|
167
167
|
it 'should rollback all changes' do
|
|
168
|
-
transactor.
|
|
169
|
-
worker.status.
|
|
168
|
+
expect(transactor).to be_sleeping
|
|
169
|
+
expect(worker.status).to eq('sleeping')
|
|
170
170
|
|
|
171
|
-
|
|
172
|
-
transactor.
|
|
173
|
-
worker.reload.status.
|
|
171
|
+
expect {transactor.run!}.to raise_error(StandardError, 'failed on purpose')
|
|
172
|
+
expect(transactor).to be_running
|
|
173
|
+
expect(worker.reload.status).to eq('sleeping')
|
|
174
174
|
end
|
|
175
175
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
176
|
+
context "nested transactions" do
|
|
177
|
+
it "should rollback all changes in nested transaction" do
|
|
178
|
+
expect(transactor).to be_sleeping
|
|
179
|
+
expect(worker.status).to eq('sleeping')
|
|
179
180
|
|
|
180
|
-
|
|
181
|
-
|
|
181
|
+
Worker.transaction do
|
|
182
|
+
expect { transactor.run! }.to raise_error(StandardError, 'failed on purpose')
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
expect(transactor).to be_running
|
|
186
|
+
expect(worker.reload.status).to eq('sleeping')
|
|
182
187
|
end
|
|
183
188
|
|
|
184
|
-
|
|
185
|
-
|
|
189
|
+
it "should only rollback changes in the main transaction not the nested one" do
|
|
190
|
+
# change configuration to not require new transaction
|
|
191
|
+
AASM::StateMachine[Transactor].config.requires_new_transaction = false
|
|
192
|
+
|
|
193
|
+
expect(transactor).to be_sleeping
|
|
194
|
+
expect(worker.status).to eq('sleeping')
|
|
195
|
+
|
|
196
|
+
Worker.transaction do
|
|
197
|
+
expect { transactor.run! }.to raise_error(StandardError, 'failed on purpose')
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
expect(transactor).to be_running
|
|
201
|
+
expect(worker.reload.status).to eq('running')
|
|
202
|
+
end
|
|
186
203
|
end
|
|
187
204
|
|
|
188
205
|
describe "after_commit callback" do
|
|
189
206
|
it "should fire :after_commit if transaction was successful" do
|
|
190
207
|
validator = Validator.create(:name => 'name')
|
|
191
|
-
validator.
|
|
208
|
+
expect(validator).to be_sleeping
|
|
192
209
|
validator.run!
|
|
193
|
-
validator.
|
|
194
|
-
validator.name.
|
|
210
|
+
expect(validator).to be_running
|
|
211
|
+
expect(validator.name).not_to eq("name")
|
|
195
212
|
end
|
|
196
213
|
|
|
197
214
|
it "should not fire :after_commit if transaction failed" do
|
|
198
215
|
validator = Validator.create(:name => 'name')
|
|
199
|
-
|
|
200
|
-
validator.name.
|
|
216
|
+
expect { validator.fail! }.to raise_error(StandardError, 'failed on purpose')
|
|
217
|
+
expect(validator.name).to eq("name")
|
|
201
218
|
end
|
|
202
219
|
|
|
203
220
|
end
|
|
204
221
|
end
|
|
205
222
|
end
|
|
223
|
+
|
|
224
|
+
describe "invalid states with persistence" do
|
|
225
|
+
|
|
226
|
+
it "should not store states" do
|
|
227
|
+
validator = Validator.create(:name => 'name')
|
|
228
|
+
validator.status = 'invalid_state'
|
|
229
|
+
expect(validator.save).to be_false
|
|
230
|
+
expect {validator.save!}.to raise_error(ActiveRecord::RecordInvalid)
|
|
231
|
+
|
|
232
|
+
validator.reload
|
|
233
|
+
expect(validator).to be_sleeping
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
it "should store invalid states if configured" do
|
|
237
|
+
persistor = InvalidPersistor.create(:name => 'name')
|
|
238
|
+
persistor.status = 'invalid_state'
|
|
239
|
+
expect(persistor.save).to be_true
|
|
240
|
+
|
|
241
|
+
persistor.reload
|
|
242
|
+
expect(persistor.status).to eq('invalid_state')
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
end
|
|
@@ -27,15 +27,15 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
|
|
|
27
27
|
|
|
28
28
|
context "Does not already respond_to? the scope name" do
|
|
29
29
|
it "should add a scope" do
|
|
30
|
-
SimpleMongoid.
|
|
31
|
-
SimpleMongoid.unknown_scope.class.
|
|
30
|
+
expect(SimpleMongoid).to respond_to(:unknown_scope)
|
|
31
|
+
expect(SimpleMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
context "Already respond_to? the scope name" do
|
|
36
36
|
it "should not add a scope" do
|
|
37
|
-
SimpleMongoid.
|
|
38
|
-
SimpleMongoid.new.class.
|
|
37
|
+
expect(SimpleMongoid).to respond_to(:new)
|
|
38
|
+
expect(SimpleMongoid.new.class).to eq(SimpleMongoid)
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
@@ -45,20 +45,20 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
|
|
|
45
45
|
|
|
46
46
|
context "Does not already respond_to? the scope name" do
|
|
47
47
|
it "should add a scope" do
|
|
48
|
-
SimpleNewDslMongoid.
|
|
49
|
-
SimpleNewDslMongoid.unknown_scope.class.
|
|
48
|
+
expect(SimpleNewDslMongoid).to respond_to(:unknown_scope)
|
|
49
|
+
expect(SimpleNewDslMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
context "Already respond_to? the scope name" do
|
|
54
54
|
it "should not add a scope" do
|
|
55
|
-
SimpleNewDslMongoid.
|
|
56
|
-
SimpleNewDslMongoid.new.class.
|
|
55
|
+
expect(SimpleNewDslMongoid).to respond_to(:new)
|
|
56
|
+
expect(SimpleNewDslMongoid.new.class).to eq(SimpleNewDslMongoid)
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
it "does not create scopes if requested" do
|
|
61
|
-
NoScopeMongoid.
|
|
61
|
+
expect(NoScopeMongoid).not_to respond_to(:ignored_scope)
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
end
|
|
@@ -69,12 +69,12 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
|
|
|
69
69
|
let!(:model_id) { model._id }
|
|
70
70
|
|
|
71
71
|
it "should respond to method" do
|
|
72
|
-
SimpleNewDslMongoid.
|
|
72
|
+
expect(SimpleNewDslMongoid).to respond_to(:find_in_state)
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
it "should find the model when given the correct scope and model id" do
|
|
76
|
-
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class.
|
|
77
|
-
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').
|
|
76
|
+
expect(SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class).to eq(SimpleNewDslMongoid)
|
|
77
|
+
expect(SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope')).to eq(model)
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
it "should raise DocumentNotFound error when given incorrect scope" do
|
|
@@ -94,17 +94,17 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
|
|
|
94
94
|
end
|
|
95
95
|
|
|
96
96
|
it "should respond to method" do
|
|
97
|
-
SimpleNewDslMongoid.
|
|
97
|
+
expect(SimpleNewDslMongoid).to respond_to(:count_in_state)
|
|
98
98
|
end
|
|
99
99
|
|
|
100
100
|
it "should return n for a scope with n records persisted" do
|
|
101
|
-
SimpleNewDslMongoid.count_in_state('unknown_scope').class.
|
|
102
|
-
SimpleNewDslMongoid.count_in_state('unknown_scope').
|
|
101
|
+
expect(SimpleNewDslMongoid.count_in_state('unknown_scope').class).to eq(Fixnum)
|
|
102
|
+
expect(SimpleNewDslMongoid.count_in_state('unknown_scope')).to eq(3)
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
it "should return zero for a scope without records persisted" do
|
|
106
|
-
SimpleNewDslMongoid.count_in_state('new').class.
|
|
107
|
-
SimpleNewDslMongoid.count_in_state('new').
|
|
106
|
+
expect(SimpleNewDslMongoid.count_in_state('new').class).to eq(Fixnum)
|
|
107
|
+
expect(SimpleNewDslMongoid.count_in_state('new')).to eq(0)
|
|
108
108
|
end
|
|
109
109
|
|
|
110
110
|
end
|
|
@@ -117,16 +117,16 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
|
|
|
117
117
|
end
|
|
118
118
|
|
|
119
119
|
it "should respond to method" do
|
|
120
|
-
SimpleNewDslMongoid.
|
|
120
|
+
expect(SimpleNewDslMongoid).to respond_to(:with_state_scope)
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
it "should correctly process block" do
|
|
124
|
-
SimpleNewDslMongoid.with_state_scope('unknown_scope') do
|
|
124
|
+
expect(SimpleNewDslMongoid.with_state_scope('unknown_scope') do
|
|
125
125
|
SimpleNewDslMongoid.count
|
|
126
|
-
end.
|
|
127
|
-
SimpleNewDslMongoid.with_state_scope('new') do
|
|
126
|
+
end).to eq(3)
|
|
127
|
+
expect(SimpleNewDslMongoid.with_state_scope('new') do
|
|
128
128
|
SimpleNewDslMongoid.count
|
|
129
|
-
end.
|
|
129
|
+
end).to eq(2)
|
|
130
130
|
end
|
|
131
131
|
|
|
132
132
|
end
|
|
@@ -136,12 +136,12 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
|
|
|
136
136
|
let(:simple) {SimpleNewDslMongoid.new}
|
|
137
137
|
|
|
138
138
|
it "should call aasm_ensure_initial_state on validation before create" do
|
|
139
|
-
simple.
|
|
139
|
+
expect(simple).to receive(:aasm_ensure_initial_state).and_return(true)
|
|
140
140
|
simple.valid?
|
|
141
141
|
end
|
|
142
142
|
|
|
143
143
|
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
|
144
|
-
simple.
|
|
144
|
+
expect(simple).to receive(:aasm_ensure_initial_state).and_return(true)
|
|
145
145
|
simple.save(:validate => false)
|
|
146
146
|
end
|
|
147
147
|
end
|
|
@@ -20,39 +20,39 @@ describe 'state machine' do
|
|
|
20
20
|
let(:payment) {Payment.new}
|
|
21
21
|
|
|
22
22
|
it 'starts with an initial state' do
|
|
23
|
-
payment.aasm.current_state.
|
|
24
|
-
payment.
|
|
25
|
-
payment.
|
|
23
|
+
expect(payment.aasm.current_state).to eq(:initialised)
|
|
24
|
+
expect(payment).to respond_to(:initialised?)
|
|
25
|
+
expect(payment).to be_initialised
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it 'allows transitions to other states' do
|
|
29
|
-
payment.
|
|
30
|
-
payment.
|
|
29
|
+
expect(payment).to respond_to(:fill_out)
|
|
30
|
+
expect(payment).to respond_to(:fill_out!)
|
|
31
31
|
payment.fill_out!
|
|
32
|
-
payment.
|
|
33
|
-
payment.
|
|
32
|
+
expect(payment).to respond_to(:filled_out?)
|
|
33
|
+
expect(payment).to be_filled_out
|
|
34
34
|
|
|
35
|
-
payment.
|
|
36
|
-
payment.
|
|
35
|
+
expect(payment).to respond_to(:authorise)
|
|
36
|
+
expect(payment).to respond_to(:authorise!)
|
|
37
37
|
payment.authorise
|
|
38
|
-
payment.
|
|
39
|
-
payment.
|
|
38
|
+
expect(payment).to respond_to(:authorised?)
|
|
39
|
+
expect(payment).to be_authorised
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
it 'denies transitions to other states' do
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
expect {payment.authorise}.to raise_error(AASM::InvalidTransition)
|
|
44
|
+
expect {payment.authorise!}.to raise_error(AASM::InvalidTransition)
|
|
45
45
|
payment.fill_out
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
expect {payment.fill_out}.to raise_error(AASM::InvalidTransition)
|
|
47
|
+
expect {payment.fill_out!}.to raise_error(AASM::InvalidTransition)
|
|
48
48
|
payment.authorise
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
expect {payment.fill_out}.to raise_error(AASM::InvalidTransition)
|
|
50
|
+
expect {payment.fill_out!}.to raise_error(AASM::InvalidTransition)
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
it 'defines constants for each state name' do
|
|
54
|
-
Payment::STATE_INITIALISED.
|
|
55
|
-
Payment::STATE_FILLED_OUT.
|
|
56
|
-
Payment::STATE_AUTHORISED.
|
|
54
|
+
expect(Payment::STATE_INITIALISED).to eq(:initialised)
|
|
55
|
+
expect(Payment::STATE_FILLED_OUT).to eq(:filled_out)
|
|
56
|
+
expect(Payment::STATE_AUTHORISED).to eq(:authorised)
|
|
57
57
|
end
|
|
58
58
|
end
|
data/spec/unit/state_spec.rb
CHANGED
|
@@ -12,34 +12,34 @@ describe AASM::State do
|
|
|
12
12
|
|
|
13
13
|
it 'should set the name' do
|
|
14
14
|
state = new_state
|
|
15
|
-
state.name.
|
|
15
|
+
expect(state.name).to eq(:astate)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'should set the display_name from name' do
|
|
19
|
-
new_state.display_name.
|
|
19
|
+
expect(new_state.display_name).to eq('Astate')
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it 'should set the display_name from options' do
|
|
23
|
-
new_state(:display => "A State").display_name.
|
|
23
|
+
expect(new_state(:display => "A State").display_name).to eq('A State')
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
it 'should set the options and expose them as options' do
|
|
27
|
-
new_state.options.
|
|
27
|
+
expect(new_state.options).to eq(@options)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it 'should be equal to a symbol of the same name' do
|
|
31
|
-
new_state.
|
|
31
|
+
expect(new_state).to eq(:astate)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it 'should be equal to a State of the same name' do
|
|
35
|
-
new_state.
|
|
35
|
+
expect(new_state).to eq(new_state)
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it 'should send a message to the record for an action if the action is present as a symbol' do
|
|
39
39
|
state = new_state(:entering => :foo)
|
|
40
40
|
|
|
41
41
|
record = double('record')
|
|
42
|
-
record.
|
|
42
|
+
expect(record).to receive(:foo)
|
|
43
43
|
|
|
44
44
|
state.fire_callbacks(:entering, record)
|
|
45
45
|
end
|
|
@@ -48,7 +48,7 @@ describe AASM::State do
|
|
|
48
48
|
state = new_state(:entering => 'foo')
|
|
49
49
|
|
|
50
50
|
record = double('record')
|
|
51
|
-
record.
|
|
51
|
+
expect(record).to receive(:foo)
|
|
52
52
|
|
|
53
53
|
state.fire_callbacks(:entering, record)
|
|
54
54
|
end
|
|
@@ -57,10 +57,10 @@ describe AASM::State do
|
|
|
57
57
|
state = new_state(:entering => [:a, :b, "c", lambda {|r| r.foobar }])
|
|
58
58
|
|
|
59
59
|
record = double('record')
|
|
60
|
-
record.
|
|
61
|
-
record.
|
|
62
|
-
record.
|
|
63
|
-
record.
|
|
60
|
+
expect(record).to receive(:a)
|
|
61
|
+
expect(record).to receive(:b)
|
|
62
|
+
expect(record).to receive(:c)
|
|
63
|
+
expect(record).to receive(:foobar)
|
|
64
64
|
|
|
65
65
|
state.fire_callbacks(:entering, record)
|
|
66
66
|
end
|
|
@@ -69,9 +69,9 @@ describe AASM::State do
|
|
|
69
69
|
state = new_state(:entering => [:a, :b, :c])
|
|
70
70
|
|
|
71
71
|
record = double('record')
|
|
72
|
-
record.
|
|
73
|
-
record.
|
|
74
|
-
record.
|
|
72
|
+
expect(record).to receive(:a)
|
|
73
|
+
expect(record).to receive(:b).and_throw(:halt_aasm_chain)
|
|
74
|
+
expect(record).not_to receive(:c)
|
|
75
75
|
|
|
76
76
|
state.fire_callbacks(:entering, record)
|
|
77
77
|
end
|
|
@@ -80,7 +80,7 @@ describe AASM::State do
|
|
|
80
80
|
state = new_state(:entering => Proc.new {|r| r.foobar})
|
|
81
81
|
|
|
82
82
|
record = double('record')
|
|
83
|
-
record.
|
|
83
|
+
expect(record).to receive(:foobar)
|
|
84
84
|
|
|
85
85
|
state.fire_callbacks(:entering, record)
|
|
86
86
|
end
|