aasm 3.0.24 → 3.0.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +21 -3
- data/CHANGELOG.md +8 -0
- data/Gemfile +9 -1
- data/LICENSE +1 -1
- data/README.md +9 -8
- data/aasm.gemspec +4 -5
- data/gemfiles/rails_3.2.gemfile +11 -0
- data/gemfiles/rails_4.0.gemfile +10 -0
- data/lib/aasm.rb +0 -3
- data/lib/aasm/aasm.rb +31 -27
- data/lib/aasm/base.rb +14 -5
- data/lib/aasm/event.rb +16 -16
- data/lib/aasm/instance_base.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +10 -8
- data/lib/aasm/persistence/base.rb +1 -1
- data/lib/aasm/persistence/mongoid_persistence.rb +10 -8
- data/lib/aasm/version.rb +1 -1
- 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/unit/callbacks_spec.rb +4 -20
- data/spec/unit/complex_example_spec.rb +4 -4
- data/spec/unit/event_spec.rb +49 -36
- data/spec/unit/initial_state_spec.rb +4 -5
- data/spec/unit/inspection_spec.rb +11 -24
- data/spec/unit/localizer_spec.rb +12 -12
- data/spec/unit/persistence/active_record_persistence_spec.rb +13 -31
- data/spec/unit/persistence/mongoid_persistance_spec.rb +102 -81
- data/spec/unit/simple_example_spec.rb +1 -2
- data/spec/unit/subclassing_spec.rb +7 -7
- data/spec/unit/transition_spec.rb +5 -5
- metadata +4 -75
- data/lib/aasm/deprecated/aasm.rb +0 -15
- data/spec/models/callback_old_dsl.rb +0 -41
data/spec/unit/localizer_spec.rb
CHANGED
@@ -10,12 +10,12 @@ class LocalizerTestModel < ActiveRecord::Base
|
|
10
10
|
|
11
11
|
attr_accessor :aasm_state
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
aasm do
|
14
|
+
state :opened, :initial => true
|
15
|
+
state :closed
|
16
|
+
event :close
|
17
|
+
event :open
|
18
|
+
end
|
19
19
|
end
|
20
20
|
|
21
21
|
describe 'localized state names' do
|
@@ -52,13 +52,13 @@ describe AASM::Localizer, "new style" do
|
|
52
52
|
let (:foo_opened) { LocalizerTestModel.new }
|
53
53
|
let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
|
54
54
|
|
55
|
-
context '
|
55
|
+
context 'aasm.human_state' do
|
56
56
|
it 'should return translated state value' do
|
57
|
-
foo_opened.
|
57
|
+
foo_opened.aasm.human_state.should == "It's open now!"
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'should return humanized value if not localized' do
|
61
|
-
foo_closed.
|
61
|
+
foo_closed.aasm.human_state.should == "Closed"
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -87,13 +87,13 @@ describe AASM::Localizer, "deprecated style" do
|
|
87
87
|
let (:foo_opened) { LocalizerTestModel.new }
|
88
88
|
let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
|
89
89
|
|
90
|
-
context '
|
90
|
+
context 'aasm.human_state' do
|
91
91
|
it 'should return translated state value' do
|
92
|
-
foo_opened.
|
92
|
+
foo_opened.aasm.human_state.should == "It's open now!"
|
93
93
|
end
|
94
94
|
|
95
95
|
it 'should return humanized value if not localized' do
|
96
|
-
foo_closed.
|
96
|
+
foo_closed.aasm.human_state.should == "Closed"
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -24,24 +24,24 @@ describe "instance methods" do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should return the initial state when new and the aasm field is nil" do
|
27
|
-
gate.
|
27
|
+
gate.aasm.current_state.should == :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.
|
32
|
+
gate.aasm.current_state.should == :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
36
|
gate.stub(:new_record?).and_return(false)
|
37
37
|
gate.aasm_state = "state"
|
38
|
-
gate.
|
38
|
+
gate.aasm.current_state.should == :state
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should allow a nil state" do
|
42
42
|
gate.stub(:new_record?).and_return(false)
|
43
43
|
gate.aasm_state = nil
|
44
|
-
gate.
|
44
|
+
gate.aasm.current_state.should be_nil
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should call aasm_ensure_initial_state on validation before create" do
|
@@ -49,6 +49,11 @@ describe "instance methods" do
|
|
49
49
|
gate.valid?
|
50
50
|
end
|
51
51
|
|
52
|
+
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
53
|
+
gate.should_receive(:aasm_ensure_initial_state).and_return(true)
|
54
|
+
gate.save(:validate => false)
|
55
|
+
end
|
56
|
+
|
52
57
|
it "should not call aasm_ensure_initial_state on validation before update" do
|
53
58
|
gate.stub(:new_record?).and_return(false)
|
54
59
|
gate.should_not_receive(:aasm_ensure_initial_state)
|
@@ -59,15 +64,11 @@ end
|
|
59
64
|
|
60
65
|
describe 'subclasses' do
|
61
66
|
it "should have the same states as its parent class" do
|
62
|
-
|
67
|
+
DerivateNewDsl.aasm.states.should == SimpleNewDsl.aasm.states
|
63
68
|
end
|
64
69
|
|
65
70
|
it "should have the same events as its parent class" do
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should have the same column as its parent class" do
|
70
|
-
Derivate.aasm_column.should == :status
|
71
|
+
DerivateNewDsl.aasm.events.should == SimpleNewDsl.aasm.events
|
71
72
|
end
|
72
73
|
|
73
74
|
it "should have the same column as its parent even for the new dsl" do
|
@@ -76,26 +77,7 @@ describe 'subclasses' do
|
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
79
|
-
describe "named scopes with the old DSL" do
|
80
|
-
|
81
|
-
context "Does not already respond_to? the scope name" do
|
82
|
-
it "should add a scope" do
|
83
|
-
Simple.should respond_to(:unknown_scope)
|
84
|
-
SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation).should be_true
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context "Already respond_to? the scope name" do
|
89
|
-
it "should not add a scope" do
|
90
|
-
Simple.should respond_to(:new)
|
91
|
-
Simple.new.class.should == Simple
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
80
|
describe "named scopes with the new DSL" do
|
98
|
-
|
99
81
|
context "Does not already respond_to? the scope name" do
|
100
82
|
it "should add a scope" do
|
101
83
|
SimpleNewDsl.should respond_to(:unknown_scope)
|
@@ -119,8 +101,8 @@ end
|
|
119
101
|
describe 'initial states' do
|
120
102
|
|
121
103
|
it 'should support conditions' do
|
122
|
-
Thief.new(:skilled => true).
|
123
|
-
Thief.new(:skilled => false).
|
104
|
+
Thief.new(:skilled => true).aasm.current_state.should == :rich
|
105
|
+
Thief.new(:skilled => false).aasm.current_state.should == :jailed
|
124
106
|
end
|
125
107
|
end
|
126
108
|
|
@@ -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
|
+
SimpleMongoid.should respond_to(:unknown_scope)
|
31
|
+
SimpleMongoid.unknown_scope.class.should == 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
|
+
SimpleMongoid.should respond_to(:new)
|
38
|
+
SimpleMongoid.new.class.should == 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
|
+
SimpleNewDslMongoid.should respond_to(:unknown_scope)
|
49
|
+
SimpleNewDslMongoid.unknown_scope.class.should == 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
|
+
SimpleNewDslMongoid.should respond_to(:new)
|
56
|
+
SimpleNewDslMongoid.new.class.should == 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
|
+
NoScopeMongoid.should_not 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
|
+
SimpleNewDslMongoid.should 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
|
+
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class.should == SimpleNewDslMongoid
|
77
|
+
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').should == 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
|
+
SimpleNewDslMongoid.should 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
|
+
SimpleNewDslMongoid.count_in_state('unknown_scope').class.should == Fixnum
|
102
|
+
SimpleNewDslMongoid.count_in_state('unknown_scope').should == 3
|
103
|
+
end
|
97
104
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
105
|
+
it "should return zero for a scope without records persisted" do
|
106
|
+
SimpleNewDslMongoid.count_in_state('new').class.should == Fixnum
|
107
|
+
SimpleNewDslMongoid.count_in_state('new').should == 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
|
+
SimpleNewDslMongoid.should respond_to(:with_state_scope)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should correctly process block" do
|
124
|
+
SimpleNewDslMongoid.with_state_scope('unknown_scope') do
|
125
|
+
SimpleNewDslMongoid.count
|
126
|
+
end.should == 3
|
127
|
+
SimpleNewDslMongoid.with_state_scope('new') do
|
128
|
+
SimpleNewDslMongoid.count
|
129
|
+
end.should == 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
|
+
simple.should_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
|
+
simple.should_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,8 +20,7 @@ describe 'state machine' do
|
|
20
20
|
let(:payment) {Payment.new}
|
21
21
|
|
22
22
|
it 'starts with an initial state' do
|
23
|
-
payment.
|
24
|
-
# payment.aasm.current_state.should == :initialised # not yet supported
|
23
|
+
payment.aasm.current_state.should == :initialised
|
25
24
|
payment.should respond_to(:initialised?)
|
26
25
|
payment.should be_initialised
|
27
26
|
end
|
@@ -4,18 +4,18 @@ 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
|
+
FooTwo.aasm.states.should include(state)
|
9
9
|
end
|
10
|
-
Baz.
|
10
|
+
Baz.aasm.states.should == 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
|
+
Foo.aasm.states.should_not include(:foo)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should have the same events as its parent" do
|
18
|
-
Baz.
|
18
|
+
Baz.aasm.events.should == Bar.aasm.events
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'should know how to respond to `may_add_details?`' do
|
@@ -24,8 +24,8 @@ describe 'subclassing' do
|
|
24
24
|
|
25
25
|
it 'should not break if I call Son#update_state' do
|
26
26
|
son.update_state
|
27
|
-
son.
|
27
|
+
son.aasm.current_state.should == :pending_details_confirmation
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
end
|
31
31
|
|
@@ -187,13 +187,13 @@ describe AASM::Transition, '- when executing the transition with an :on_transtio
|
|
187
187
|
args = {:arg1 => '1', :arg2 => '2'}
|
188
188
|
obj = double('object')
|
189
189
|
|
190
|
-
obj.
|
191
|
-
|
190
|
+
def obj.test(args)
|
191
|
+
"arg1: #{args[:arg1]} arg2: #{args[:arg2]}"
|
192
192
|
end
|
193
193
|
|
194
194
|
return_value = st.execute(obj, args)
|
195
195
|
|
196
|
-
return_value.should == '
|
196
|
+
return_value.should == 'arg1: 1 arg2: 2'
|
197
197
|
end
|
198
198
|
|
199
199
|
it 'should NOT pass args if the target method does NOT accept them' do
|
@@ -202,8 +202,8 @@ describe AASM::Transition, '- when executing the transition with an :on_transtio
|
|
202
202
|
args = {:arg1 => '1', :arg2 => '2'}
|
203
203
|
obj = double('object')
|
204
204
|
|
205
|
-
obj.
|
206
|
-
|
205
|
+
def obj.test
|
206
|
+
'success'
|
207
207
|
end
|
208
208
|
|
209
209
|
return_value = st.execute(obj, args)
|