aasm 3.0.24 → 3.1.1
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 +4 -0
- data/.travis.yml +23 -4
- data/CHANGELOG.md +24 -0
- data/Gemfile +9 -1
- data/LICENSE +1 -1
- data/README.md +65 -12
- data/aasm.gemspec +5 -6
- data/gemfiles/rails_3.2.gemfile +12 -0
- data/gemfiles/rails_4.0.gemfile +11 -0
- data/gemfiles/rails_4.1.gemfile +11 -0
- data/lib/aasm/aasm.rb +31 -27
- data/lib/aasm/base.rb +35 -20
- data/lib/aasm/event.rb +27 -16
- data/lib/aasm/instance_base.rb +3 -1
- data/lib/aasm/localizer.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +27 -9
- data/lib/aasm/persistence/base.rb +1 -1
- data/lib/aasm/persistence/mongoid_persistence.rb +10 -8
- data/lib/aasm/state.rb +1 -0
- 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/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 +15 -15
- data/spec/unit/event_spec.rb +89 -76
- data/spec/unit/guard_spec.rb +60 -0
- data/spec/unit/initial_state_spec.rb +4 -5
- data/spec/unit/inspection_spec.rb +40 -53
- data/spec/unit/localizer_spec.rb +22 -18
- data/spec/unit/new_dsl_spec.rb +2 -2
- data/spec/unit/persistence/active_record_persistence_spec.rb +111 -89
- data/spec/unit/persistence/mongoid_persistance_spec.rb +102 -81
- 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 +29 -95
- data/lib/aasm/deprecated/aasm.rb +0 -15
- data/spec/models/callback_old_dsl.rb +0 -41
- data/spec/schema.rb +0 -35
|
@@ -1,131 +1,152 @@
|
|
|
1
1
|
describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3') do
|
|
2
2
|
# describe 'mongoid' do
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
begin
|
|
5
5
|
require 'mongoid'
|
|
6
6
|
require 'logger'
|
|
7
7
|
require 'spec_helper'
|
|
8
|
-
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each { |f| require File.expand_path(f) }
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
before(:all) do
|
|
10
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each { |f| require File.expand_path(f) }
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
# if you want to see the statements while running the spec enable the following line
|
|
13
|
+
# Mongoid.logger = Logger.new(STDERR)
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
DATABASE_NAME = "mongoid_#{Process.pid}"
|
|
16
|
+
|
|
17
|
+
Mongoid.configure do |config|
|
|
18
|
+
config.connect_to DATABASE_NAME
|
|
19
|
+
end
|
|
17
20
|
end
|
|
18
|
-
end
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
after do
|
|
23
|
+
Mongoid.purge!
|
|
24
|
+
end
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
describe "named scopes with the old DSL" do
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
context "Does not already respond_to? the scope name" do
|
|
29
|
+
it "should add a scope" do
|
|
30
|
+
expect(SimpleMongoid).to respond_to(:unknown_scope)
|
|
31
|
+
expect(SimpleMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
32
|
+
end
|
|
30
33
|
end
|
|
31
|
-
end
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
context "Already respond_to? the scope name" do
|
|
36
|
+
it "should not add a scope" do
|
|
37
|
+
expect(SimpleMongoid).to respond_to(:new)
|
|
38
|
+
expect(SimpleMongoid.new.class).to eq(SimpleMongoid)
|
|
39
|
+
end
|
|
37
40
|
end
|
|
41
|
+
|
|
38
42
|
end
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
describe "named scopes with the new DSL" do
|
|
41
45
|
|
|
42
|
-
|
|
46
|
+
context "Does not already respond_to? the scope name" do
|
|
47
|
+
it "should add a scope" do
|
|
48
|
+
expect(SimpleNewDslMongoid).to respond_to(:unknown_scope)
|
|
49
|
+
expect(SimpleNewDslMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
43
52
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
context "Already respond_to? the scope name" do
|
|
54
|
+
it "should not add a scope" do
|
|
55
|
+
expect(SimpleNewDslMongoid).to respond_to(:new)
|
|
56
|
+
expect(SimpleNewDslMongoid.new.class).to eq(SimpleNewDslMongoid)
|
|
57
|
+
end
|
|
48
58
|
end
|
|
49
|
-
end
|
|
50
59
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
SimpleNewDslMongoid.should respond_to(:new)
|
|
54
|
-
SimpleNewDslMongoid.new.class.should == SimpleNewDslMongoid
|
|
60
|
+
it "does not create scopes if requested" do
|
|
61
|
+
expect(NoScopeMongoid).not_to respond_to(:ignored_scope)
|
|
55
62
|
end
|
|
56
|
-
end
|
|
57
63
|
|
|
58
|
-
it "does not create scopes if requested" do
|
|
59
|
-
NoScopeMongoid.should_not respond_to(:ignored_scope)
|
|
60
64
|
end
|
|
61
65
|
|
|
62
|
-
|
|
66
|
+
describe "#find_in_state" do
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
let!(:model) { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
|
|
69
|
+
let!(:model_id) { model._id }
|
|
65
70
|
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
it "should respond to method" do
|
|
72
|
+
expect(SimpleNewDslMongoid).to respond_to(:find_in_state)
|
|
73
|
+
end
|
|
68
74
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
it "should find the model when given the correct scope and model id" do
|
|
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
|
+
end
|
|
72
79
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
end
|
|
80
|
+
it "should raise DocumentNotFound error when given incorrect scope" do
|
|
81
|
+
expect {SimpleNewDslMongoid.find_in_state(model_id, 'new')}.to raise_error Mongoid::Errors::DocumentNotFound
|
|
82
|
+
end
|
|
77
83
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
it "should raise DocumentNotFound error when given incorrect model id" do
|
|
85
|
+
expect {SimpleNewDslMongoid.find_in_state('bad_id', 'unknown_scope')}.to raise_error Mongoid::Errors::DocumentNotFound
|
|
86
|
+
end
|
|
81
87
|
|
|
82
|
-
it "should raise DocumentNotFound error when given incorrect model id" do
|
|
83
|
-
expect {SimpleNewDslMongoid.find_in_state('bad_id', 'unknown_scope')}.to raise_error Mongoid::Errors::DocumentNotFound
|
|
84
88
|
end
|
|
85
89
|
|
|
86
|
-
|
|
90
|
+
describe "#count_in_state" do
|
|
87
91
|
|
|
88
|
-
|
|
92
|
+
before do
|
|
93
|
+
3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
|
|
94
|
+
end
|
|
89
95
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
96
|
+
it "should respond to method" do
|
|
97
|
+
expect(SimpleNewDslMongoid).to respond_to(:count_in_state)
|
|
98
|
+
end
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
100
|
+
it "should return n for a scope with n records persisted" do
|
|
101
|
+
expect(SimpleNewDslMongoid.count_in_state('unknown_scope').class).to eq(Fixnum)
|
|
102
|
+
expect(SimpleNewDslMongoid.count_in_state('unknown_scope')).to eq(3)
|
|
103
|
+
end
|
|
97
104
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
105
|
+
it "should return zero for a scope without records persisted" do
|
|
106
|
+
expect(SimpleNewDslMongoid.count_in_state('new').class).to eq(Fixnum)
|
|
107
|
+
expect(SimpleNewDslMongoid.count_in_state('new')).to eq(0)
|
|
108
|
+
end
|
|
102
109
|
|
|
103
|
-
it "should return zero for a scope without records persisted" do
|
|
104
|
-
SimpleNewDslMongoid.count_in_state('new').class.should == Fixnum
|
|
105
|
-
SimpleNewDslMongoid.count_in_state('new').should == 0
|
|
106
110
|
end
|
|
107
111
|
|
|
108
|
-
|
|
112
|
+
describe "#with_state_scope" do
|
|
109
113
|
|
|
110
|
-
|
|
114
|
+
before do
|
|
115
|
+
3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
|
|
116
|
+
2.times { SimpleNewDslMongoid.create!(:status => :new) }
|
|
117
|
+
end
|
|
111
118
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
it "should respond to method" do
|
|
120
|
+
expect(SimpleNewDslMongoid).to respond_to(:with_state_scope)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "should correctly process block" do
|
|
124
|
+
expect(SimpleNewDslMongoid.with_state_scope('unknown_scope') do
|
|
125
|
+
SimpleNewDslMongoid.count
|
|
126
|
+
end).to eq(3)
|
|
127
|
+
expect(SimpleNewDslMongoid.with_state_scope('new') do
|
|
128
|
+
SimpleNewDslMongoid.count
|
|
129
|
+
end).to eq(2)
|
|
130
|
+
end
|
|
116
131
|
|
|
117
|
-
it "should respond to method" do
|
|
118
|
-
SimpleNewDslMongoid.should respond_to(:with_state_scope)
|
|
119
132
|
end
|
|
120
133
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
134
|
+
|
|
135
|
+
describe "instance methods" do
|
|
136
|
+
let(:simple) {SimpleNewDslMongoid.new}
|
|
137
|
+
|
|
138
|
+
it "should call aasm_ensure_initial_state on validation before create" do
|
|
139
|
+
expect(simple).to receive(:aasm_ensure_initial_state).and_return(true)
|
|
140
|
+
simple.valid?
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
|
144
|
+
expect(simple).to receive(:aasm_ensure_initial_state).and_return(true)
|
|
145
|
+
simple.save(:validate => false)
|
|
146
|
+
end
|
|
128
147
|
end
|
|
129
148
|
|
|
149
|
+
rescue LoadError
|
|
150
|
+
puts "Not running Mongoid specs because mongoid gem if not installed!!!"
|
|
130
151
|
end
|
|
131
152
|
end
|
|
@@ -20,40 +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.
|
|
24
|
-
|
|
25
|
-
payment.
|
|
26
|
-
payment.should be_initialised
|
|
23
|
+
expect(payment.aasm.current_state).to eq(:initialised)
|
|
24
|
+
expect(payment).to respond_to(:initialised?)
|
|
25
|
+
expect(payment).to be_initialised
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
it 'allows transitions to other states' do
|
|
30
|
-
payment.
|
|
31
|
-
payment.
|
|
29
|
+
expect(payment).to respond_to(:fill_out)
|
|
30
|
+
expect(payment).to respond_to(:fill_out!)
|
|
32
31
|
payment.fill_out!
|
|
33
|
-
payment.
|
|
34
|
-
payment.
|
|
32
|
+
expect(payment).to respond_to(:filled_out?)
|
|
33
|
+
expect(payment).to be_filled_out
|
|
35
34
|
|
|
36
|
-
payment.
|
|
37
|
-
payment.
|
|
35
|
+
expect(payment).to respond_to(:authorise)
|
|
36
|
+
expect(payment).to respond_to(:authorise!)
|
|
38
37
|
payment.authorise
|
|
39
|
-
payment.
|
|
40
|
-
payment.
|
|
38
|
+
expect(payment).to respond_to(:authorised?)
|
|
39
|
+
expect(payment).to be_authorised
|
|
41
40
|
end
|
|
42
41
|
|
|
43
42
|
it 'denies transitions to other states' do
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
expect {payment.authorise}.to raise_error(AASM::InvalidTransition)
|
|
44
|
+
expect {payment.authorise!}.to raise_error(AASM::InvalidTransition)
|
|
46
45
|
payment.fill_out
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
expect {payment.fill_out}.to raise_error(AASM::InvalidTransition)
|
|
47
|
+
expect {payment.fill_out!}.to raise_error(AASM::InvalidTransition)
|
|
49
48
|
payment.authorise
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
expect {payment.fill_out}.to raise_error(AASM::InvalidTransition)
|
|
50
|
+
expect {payment.fill_out!}.to raise_error(AASM::InvalidTransition)
|
|
52
51
|
end
|
|
53
52
|
|
|
54
53
|
it 'defines constants for each state name' do
|
|
55
|
-
Payment::STATE_INITIALISED.
|
|
56
|
-
Payment::STATE_FILLED_OUT.
|
|
57
|
-
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)
|
|
58
57
|
end
|
|
59
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
|
|
@@ -4,28 +4,28 @@ describe 'subclassing' do
|
|
|
4
4
|
let(:son) {Son.new}
|
|
5
5
|
|
|
6
6
|
it 'should have the parent states' do
|
|
7
|
-
Foo.
|
|
8
|
-
FooTwo.
|
|
7
|
+
Foo.aasm.states.each do |state|
|
|
8
|
+
expect(FooTwo.aasm.states).to include(state)
|
|
9
9
|
end
|
|
10
|
-
Baz.
|
|
10
|
+
expect(Baz.aasm.states).to eq(Bar.aasm.states)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
it 'should not add the child states to the parent machine' do
|
|
14
|
-
Foo.
|
|
14
|
+
expect(Foo.aasm.states).not_to include(:foo)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it "should have the same events as its parent" do
|
|
18
|
-
Baz.
|
|
18
|
+
expect(Baz.aasm.events).to eq(Bar.aasm.events)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it 'should know how to respond to `may_add_details?`' do
|
|
22
|
-
son.may_add_details
|
|
22
|
+
expect(son.may_add_details?).to be_true
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
it 'should not break if I call Son#update_state' do
|
|
26
26
|
son.update_state
|
|
27
|
-
son.
|
|
27
|
+
expect(son.aasm.current_state).to eq(:pending_details_confirmation)
|
|
28
28
|
end
|
|
29
|
-
|
|
29
|
+
|
|
30
30
|
end
|
|
31
31
|
|