aasm 2.3.1 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +1,5 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
2
 
3
- class Foo
4
- include AASM
5
- aasm_initial_state :open
6
- aasm_state :open, :exit => :exit
7
- aasm_state :closed, :enter => :enter
8
-
9
- aasm_event :close, :success => :success_callback do
10
- transitions :to => :closed, :from => [:open]
11
- end
12
-
13
- aasm_event :null do
14
- transitions :to => :closed, :from => [:open], :guard => :always_false
15
- end
16
-
17
- def always_false
18
- false
19
- end
20
-
21
- def success_callback
22
- end
23
-
24
- def enter
25
- end
26
- def exit
27
- end
28
- end
29
-
30
- class FooTwo < Foo
31
- include AASM
32
- aasm_state :foo
33
- end
34
-
35
- class Bar
36
- include AASM
37
-
38
- aasm_state :read
39
- aasm_state :ended
40
-
41
- aasm_event :foo do
42
- transitions :to => :ended, :from => [:read]
43
- end
44
- end
45
-
46
- class Baz < Bar
47
- end
48
-
49
- class Banker
50
- include AASM
51
- aasm_initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
52
- aasm_state :retired
53
- aasm_state :selling_bad_mortgages
54
- RICH = 1_000_000
55
- attr_accessor :balance
56
- def initialize(balance = 0); self.balance = balance; end
57
- def rich?; self.balance >= RICH; end
58
- end
59
-
60
-
61
3
  describe AASM, '- class level definitions' do
62
4
  it 'should define a class level aasm_initial_state() method on its including class' do
63
5
  Foo.should respond_to(:aasm_initial_state)
@@ -85,6 +27,20 @@ describe AASM, '- class level definitions' do
85
27
 
86
28
  end
87
29
 
30
+ describe "naming" do
31
+ it "work for valid" do
32
+ Argument.aasm_states.should include(:invalid)
33
+ Argument.aasm_states.should include(:valid)
34
+
35
+ argument = Argument.new
36
+ argument.invalid?.should be_true
37
+ argument.aasm_current_state.should == :invalid
38
+
39
+ argument.valid!
40
+ argument.valid?.should be_true
41
+ argument.aasm_current_state.should == :valid
42
+ end
43
+ end
88
44
 
89
45
  describe AASM, '- subclassing' do
90
46
  it 'should have the parent states' do
@@ -293,10 +249,10 @@ describe AASM, '- event callbacks' do
293
249
  @foo.stub!(:enter).and_raise(StandardError)
294
250
  lambda{@foo.safe_close!}.should raise_error(NoMethodError)
295
251
  end
296
-
252
+
297
253
  it "should propagate an error if no error callback is declared" do
298
254
  @foo.stub!(:enter).and_raise("Cannot enter safe")
299
- lambda{@foo.close!}.should raise_error(StandardError, "Cannot enter safe")
255
+ lambda{@foo.close!}.should raise_error(StandardError, "Cannot enter safe")
300
256
  end
301
257
  end
302
258
 
@@ -377,35 +333,6 @@ describe Baz do
377
333
  end
378
334
 
379
335
 
380
- class ChetanPatil
381
- include AASM
382
- aasm_initial_state :sleeping
383
- aasm_state :sleeping
384
- aasm_state :showering
385
- aasm_state :working
386
- aasm_state :dating
387
- aasm_state :prettying_up
388
-
389
- aasm_event :wakeup do
390
- transitions :from => :sleeping, :to => [:showering, :working]
391
- end
392
-
393
- aasm_event :dress do
394
- transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes
395
- transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) }
396
- transitions :from => :showering, :to => :prettying_up, :on_transition => [:condition_hair, :fix_hair]
397
- end
398
-
399
- def wear_clothes(shirt_color, trouser_type)
400
- end
401
-
402
- def condition_hair
403
- end
404
-
405
- def fix_hair
406
- end
407
- end
408
-
409
336
 
410
337
  describe ChetanPatil do
411
338
  it 'should transition to specified next state (sleeping to showering)' do
@@ -1,246 +1,250 @@
1
- begin
2
- require 'rubygems'
3
- require 'active_record'
4
- require 'logger'
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'logger'
5
4
 
6
- load_schema
5
+ load_schema
7
6
 
8
- ActiveRecord::Base.logger = Logger.new(STDERR)
7
+ ActiveRecord::Base.logger = Logger.new(STDERR)
9
8
 
10
- class Gate < ActiveRecord::Base
11
- include AASM
9
+ class Gate < ActiveRecord::Base
10
+ include AASM
12
11
 
13
- # Fake this column for testing purposes
14
- attr_accessor :aasm_state
12
+ # Fake this column for testing purposes
13
+ attr_accessor :aasm_state
15
14
 
16
- aasm_state :opened
17
- aasm_state :closed
15
+ aasm_state :opened
16
+ aasm_state :closed
18
17
 
19
- aasm_event :view do
20
- transitions :to => :read, :from => [:needs_attention]
21
- end
18
+ aasm_event :view do
19
+ transitions :to => :read, :from => [:needs_attention]
22
20
  end
21
+ end
23
22
 
24
- class Reader < ActiveRecord::Base
25
- def aasm_read_state
26
- "fi"
27
- end
28
- include AASM
23
+ class Reader < ActiveRecord::Base
24
+ def aasm_read_state
25
+ "fi"
29
26
  end
27
+ include AASM
28
+ end
30
29
 
31
- class Writer < ActiveRecord::Base
32
- def aasm_write_state(state)
33
- "fo"
34
- end
35
- include AASM
30
+ class Writer < ActiveRecord::Base
31
+ def aasm_write_state(state)
32
+ "fo"
36
33
  end
34
+ include AASM
35
+ end
37
36
 
38
- class Transient < ActiveRecord::Base
39
- def aasm_write_state_without_persistence(state)
40
- "fum"
41
- end
42
- include AASM
37
+ class Transient < ActiveRecord::Base
38
+ def aasm_write_state_without_persistence(state)
39
+ "fum"
43
40
  end
41
+ include AASM
42
+ end
44
43
 
45
- class Simple < ActiveRecord::Base
46
- include AASM
47
- aasm_column :status
48
- end
44
+ class Simple < ActiveRecord::Base
45
+ include AASM
46
+ aasm_column :status
47
+ end
49
48
 
50
- class Derivate < Simple
51
- end
49
+ class SimpleNewDsl < ActiveRecord::Base
50
+ include AASM
51
+ aasm :column => :status
52
+ end
52
53
 
53
- class Thief < ActiveRecord::Base
54
- set_table_name "thieves"
55
- include AASM
56
- aasm_initial_state Proc.new { |thief| thief.skilled ? :rich : :jailed }
57
- aasm_state :rich
58
- aasm_state :jailed
59
- attr_accessor :skilled, :aasm_state
60
- end
54
+ class Derivate < Simple
55
+ end
61
56
 
62
- shared_examples_for "aasm model" do
63
- it "should include AASM::Persistence::ActiveRecordPersistence" do
64
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
65
- end
66
- it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
67
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
68
- end
69
- end
57
+ class DerivateNewDsl < SimpleNewDsl
58
+ end
70
59
 
71
- describe Gate, "class methods" do
72
- before(:each) do
73
- @klass = Gate
74
- end
75
- it_should_behave_like "aasm model"
76
- it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
77
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
78
- end
79
- it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
80
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
81
- end
82
- it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
83
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
84
- end
85
- end
60
+ class Thief < ActiveRecord::Base
61
+ set_table_name "thieves"
62
+ include AASM
63
+ aasm_initial_state Proc.new { |thief| thief.skilled ? :rich : :jailed }
64
+ aasm_state :rich
65
+ aasm_state :jailed
66
+ attr_accessor :skilled, :aasm_state
67
+ end
86
68
 
87
- describe Reader, "class methods" do
88
- before(:each) do
89
- @klass = Reader
90
- end
91
- it_should_behave_like "aasm model"
92
- it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
93
- @klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
94
- end
95
- it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
96
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
97
- end
98
- it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
99
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
100
- end
69
+ shared_examples_for "aasm model" do
70
+ it "should include AASM::Persistence::ActiveRecordPersistence" do
71
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
72
+ end
73
+ it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
74
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
101
75
  end
76
+ end
102
77
 
103
- describe Writer, "class methods" do
104
- before(:each) do
105
- @klass = Writer
106
- end
107
- it_should_behave_like "aasm model"
108
- it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
109
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
110
- end
111
- it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
112
- @klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
113
- end
114
- it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
115
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
116
- end
78
+ describe Gate, "class methods" do
79
+ before(:each) do
80
+ @klass = Gate
117
81
  end
82
+ it_should_behave_like "aasm model"
83
+ it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
84
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
85
+ end
86
+ it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
87
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
88
+ end
89
+ it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
90
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
91
+ end
92
+ end
118
93
 
119
- describe Transient, "class methods" do
120
- before(:each) do
121
- @klass = Transient
122
- end
123
- it_should_behave_like "aasm model"
124
- it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
125
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
126
- end
127
- it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
128
- @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
129
- end
130
- it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
131
- @klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
132
- end
94
+ describe Reader, "class methods" do
95
+ before(:each) do
96
+ @klass = Reader
97
+ end
98
+ it_should_behave_like "aasm model"
99
+ it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
100
+ @klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
101
+ end
102
+ it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
103
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
104
+ end
105
+ it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
106
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
133
107
  end
108
+ end
134
109
 
135
- describe Gate, "instance methods" do
110
+ describe Writer, "class methods" do
111
+ before(:each) do
112
+ @klass = Writer
113
+ end
114
+ it_should_behave_like "aasm model"
115
+ it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
116
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
117
+ end
118
+ it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
119
+ @klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
120
+ end
121
+ it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
122
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
123
+ end
124
+ end
136
125
 
137
- it "should respond to aasm read state when not previously defined" do
138
- Gate.new.should respond_to(:aasm_read_state)
139
- end
126
+ describe Transient, "class methods" do
127
+ before(:each) do
128
+ @klass = Transient
129
+ end
130
+ it_should_behave_like "aasm model"
131
+ it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
132
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
133
+ end
134
+ it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
135
+ @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
136
+ end
137
+ it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
138
+ @klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
139
+ end
140
+ end
140
141
 
141
- it "should respond to aasm write state when not previously defined" do
142
- Gate.new.should respond_to(:aasm_write_state)
143
- end
142
+ describe Gate, "instance methods" do
144
143
 
145
- it "should respond to aasm write state without persistence when not previously defined" do
146
- Gate.new.should respond_to(:aasm_write_state_without_persistence)
147
- end
144
+ it "should respond to aasm read state when not previously defined" do
145
+ Gate.new.should respond_to(:aasm_read_state)
146
+ end
148
147
 
149
- it "should return the initial state when new and the aasm field is nil" do
150
- Gate.new.aasm_current_state.should == :opened
151
- end
148
+ it "should respond to aasm write state when not previously defined" do
149
+ Gate.new.should respond_to(:aasm_write_state)
150
+ end
152
151
 
153
- it "should return the aasm column when new and the aasm field is not nil" do
154
- foo = Gate.new
155
- foo.aasm_state = "closed"
156
- foo.aasm_current_state.should == :closed
157
- end
152
+ it "should respond to aasm write state without persistence when not previously defined" do
153
+ Gate.new.should respond_to(:aasm_write_state_without_persistence)
154
+ end
158
155
 
159
- it "should return the aasm column when not new and the aasm_column is not nil" do
160
- foo = Gate.new
161
- foo.stub!(:new_record?).and_return(false)
162
- foo.aasm_state = "state"
163
- foo.aasm_current_state.should == :state
164
- end
156
+ it "should return the initial state when new and the aasm field is nil" do
157
+ Gate.new.aasm_current_state.should == :opened
158
+ end
165
159
 
166
- it "should allow a nil state" do
167
- foo = Gate.new
168
- foo.stub!(:new_record?).and_return(false)
169
- foo.aasm_state = nil
170
- foo.aasm_current_state.should be_nil
171
- end
160
+ it "should return the aasm column when new and the aasm field is not nil" do
161
+ foo = Gate.new
162
+ foo.aasm_state = "closed"
163
+ foo.aasm_current_state.should == :closed
164
+ end
172
165
 
173
- it "should have aasm_ensure_initial_state" do
174
- foo = Gate.new
175
- foo.send :aasm_ensure_initial_state
176
- end
166
+ it "should return the aasm column when not new and the aasm_column is not nil" do
167
+ foo = Gate.new
168
+ foo.stub!(:new_record?).and_return(false)
169
+ foo.aasm_state = "state"
170
+ foo.aasm_current_state.should == :state
171
+ end
177
172
 
178
- it "should call aasm_ensure_initial_state on validation before create" do
179
- foo = Gate.new
180
- foo.should_receive(:aasm_ensure_initial_state).and_return(true)
181
- foo.valid?
182
- end
173
+ it "should allow a nil state" do
174
+ foo = Gate.new
175
+ foo.stub!(:new_record?).and_return(false)
176
+ foo.aasm_state = nil
177
+ foo.aasm_current_state.should be_nil
178
+ end
183
179
 
184
- it "should call aasm_ensure_initial_state on validation before create" do
185
- foo = Gate.new
186
- foo.stub!(:new_record?).and_return(false)
187
- foo.should_not_receive(:aasm_ensure_initial_state)
188
- foo.valid?
189
- end
180
+ it "should have aasm_ensure_initial_state" do
181
+ foo = Gate.new
182
+ foo.send :aasm_ensure_initial_state
183
+ end
190
184
 
185
+ it "should call aasm_ensure_initial_state on validation before create" do
186
+ foo = Gate.new
187
+ foo.should_receive(:aasm_ensure_initial_state).and_return(true)
188
+ foo.valid?
191
189
  end
192
190
 
193
- describe 'Derivates' do
194
- it "should have the same states as it's parent" do
195
- Derivate.aasm_states.should == Simple.aasm_states
196
- end
191
+ it "should call aasm_ensure_initial_state on validation before create" do
192
+ foo = Gate.new
193
+ foo.stub!(:new_record?).and_return(false)
194
+ foo.should_not_receive(:aasm_ensure_initial_state)
195
+ foo.valid?
196
+ end
197
197
 
198
- it "should have the same events as it's parent" do
199
- Derivate.aasm_events.should == Simple.aasm_events
200
- end
198
+ end
201
199
 
202
- it "should have the same column as it's parent" do
203
- Derivate.aasm_column.should == :status
204
- end
200
+ describe 'Derivates' do
201
+ it "should have the same states as its parent" do
202
+ Derivate.aasm_states.should == Simple.aasm_states
205
203
  end
206
204
 
207
- describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do
205
+ it "should have the same events as its parent" do
206
+ Derivate.aasm_events.should == Simple.aasm_events
207
+ end
208
208
 
209
- context "Does not already respond_to? the scope name" do
210
- it "should add a scope" do
211
- Simple.should_not respond_to(:unknown_scope)
212
- Simple.aasm_state :unknown_scope
213
- Simple.should respond_to(:unknown_scope)
214
- Simple.unknown_scope.class.should == ActiveRecord::Relation
215
- end
216
- end
209
+ it "should have the same column as its parent" do
210
+ Derivate.aasm_column.should == :status
211
+ end
217
212
 
218
- context "Already respond_to? the scope name" do
219
- it "should not add a scope" do
220
- Simple.aasm_state :new
221
- Simple.should respond_to(:new)
222
- Simple.new.class.should == Simple
223
- end
224
- end
213
+ it "should have the same column as its parent even for the new dsl" do
214
+ SimpleNewDsl.aasm_column.should == :status
215
+ DerivateNewDsl.aasm_column.should == :status
225
216
  end
217
+ end
226
218
 
227
- describe 'Thieves' do
219
+ describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do
228
220
 
229
- it 'should be rich if they\'re skilled' do
230
- Thief.new(:skilled => true).aasm_current_state.should == :rich
221
+ context "Does not already respond_to? the scope name" do
222
+ it "should add a scope" do
223
+ Simple.should_not respond_to(:unknown_scope)
224
+ Simple.aasm_state :unknown_scope
225
+ Simple.should respond_to(:unknown_scope)
226
+ Simple.unknown_scope.class.should == ActiveRecord::Relation
231
227
  end
228
+ end
232
229
 
233
- it 'should be jailed if they\'re unskilled' do
234
- Thief.new(:skilled => false).aasm_current_state.should == :jailed
230
+ context "Already respond_to? the scope name" do
231
+ it "should not add a scope" do
232
+ Simple.aasm_state :new
233
+ Simple.should respond_to(:new)
234
+ Simple.new.class.should == Simple
235
235
  end
236
236
  end
237
+ end
237
238
 
238
- # TODO: figure out how to test ActiveRecord reload! without a database
239
+ describe 'Thieves' do
239
240
 
240
- rescue LoadError => e
241
- if e.message == "no such file to load -- active_record"
242
- puts "You must install active record to run this spec. Install with sudo gem install activerecord"
243
- else
244
- raise
241
+ it 'should be rich if they\'re skilled' do
242
+ Thief.new(:skilled => true).aasm_current_state.should == :rich
243
+ end
244
+
245
+ it 'should be jailed if they\'re unskilled' do
246
+ Thief.new(:skilled => false).aasm_current_state.should == :jailed
245
247
  end
246
248
  end
249
+
250
+ # TODO: figure out how to test ActiveRecord reload! without a database