public
Fork of rails/rails
Description: Ruby on Rails - forked for implementing I18n patch
Homepage: http://rubyonrails.org
Clone URL: git://github.com/svenfuchs/rails.git
all aasm tests without activerecord moved over and passing
technoweenie (author)
Sat Jun 28 11:33:50 -0700 2008
commit  c9e366e997c6f3a383cfaa6351fa847e92de7fe4
tree    1fce341c1b769bd78494bfb6a99a314b57d6a3ed
parent  a9d9ca16c739ec39a192d29c62f760e51040fc6e
...
37
38
39
40
 
41
42
 
43
 
 
 
 
 
 
 
 
44
45
46
 
 
 
 
 
 
 
 
 
47
48
49
...
37
38
39
 
40
41
 
42
43
44
45
46
47
48
49
50
51
52
53
 
54
55
56
57
58
59
60
61
62
63
64
65
0
@@ -37,13 +37,29 @@ module ActiveModel
0
       end
0
     end
0
 
0
- def current_state(name = nil, new_state = nil)
0
+ def current_state(name = nil, new_state = nil, persist = false)
0
       sm = self.class.state_machine(name)
0
- ivar = "@#{sm.name}_current_state"
0
+ ivar = sm.current_state_variable
0
       if name && new_state
0
+ if persist && respond_to?(:write_state)
0
+ write_state(sm, new_state)
0
+ end
0
+
0
+ if respond_to?(:write_state_without_persistence)
0
+ write_state_without_persistence(sm, new_state)
0
+ end
0
+
0
         instance_variable_set(ivar, new_state)
0
       else
0
- instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state)
0
+ instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar)
0
+ value = instance_variable_get(ivar)
0
+ return value if value
0
+
0
+ if respond_to?(:read_state)
0
+ value = instance_variable_set(ivar, read_state(sm))
0
+ end
0
+
0
+ value || sm.initial_state
0
       end
0
     end
0
   end
...
37
38
39
40
41
42
43
44
45
46
...
37
38
39
 
 
 
 
40
41
42
0
@@ -37,10 +37,6 @@ module ActiveModel
0
         @transitions.any? { |t| t.from? state }
0
       end
0
 
0
- def success?
0
- !!@success
0
- end
0
-
0
       def ==(event)
0
         if event.is_a? Symbol
0
           name == event
...
28
29
30
31
 
 
 
32
33
34
...
47
48
49
 
 
 
 
50
51
52
...
28
29
30
 
31
32
33
34
35
36
...
49
50
51
52
53
54
55
56
57
58
0
@@ -28,7 +28,9 @@ module ActiveModel
0
             record.send(event_fired_callback, record.current_state, new_state)
0
           end
0
 
0
- record.current_state(@name, new_state)
0
+ record.current_state(@name, new_state, persist)
0
+ record.send(@events[event].success) if @events[event].success
0
+ true
0
         else
0
           if record.respond_to?(event_failed_callback)
0
             record.send(event_failed_callback, event)
0
@@ -47,6 +49,10 @@ module ActiveModel
0
         events.map! { |event| event.name }
0
       end
0
 
0
+ def current_state_variable
0
+ "@#{@name}_current_state"
0
+ end
0
+
0
     private
0
       def state(name, options = {})
0
         @states << (state_index[name] ||= State.new(name, :machine => self)).update(options)
...
17
18
19
20
 
21
22
23
...
17
18
19
 
20
21
22
23
0
@@ -17,7 +17,7 @@ class EventTest < ActiveModel::TestCase
0
   end
0
 
0
   test 'should set the success option' do
0
- assert new_event.success?
0
+ assert_equal @success, new_event.success
0
   end
0
 
0
   uses_mocha 'StateTransition creation' do
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
149
150
...
162
163
164
165
 
166
167
168
 
169
170
171
 
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
190
191
...
110
111
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
...
159
160
161
 
162
163
164
 
165
166
167
 
168
169
170
171
172
173
 
 
 
 
 
 
 
 
 
 
 
 
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
0
@@ -110,41 +110,38 @@ class StateMachineInitialStatesTest < ActiveModel::TestCase
0
     assert_equal :read, @foo.current_state(:bar)
0
   end
0
 end
0
-#
0
-#describe AASM, '- event firing with persistence' do
0
-# it 'should fire the Event' do
0
-# foo = Foo.new
0
-#
0
-# Foo.aasm_events[:close].should_receive(:fire).with(foo)
0
-# foo.close!
0
-# end
0
-#
0
-# it 'should update the current state' do
0
-# foo = Foo.new
0
-# foo.close!
0
-#
0
-# foo.aasm_current_state.should == :closed
0
-# end
0
-#
0
-# it 'should call the success callback if one was provided' do
0
-# foo = Foo.new
0
-#
0
-# foo.should_receive(:success_callback)
0
-#
0
-# foo.close!
0
-# end
0
-#
0
-# it 'should attempt to persist if aasm_write_state is defined' do
0
-# foo = Foo.new
0
-#
0
-# def foo.aasm_write_state
0
-# end
0
-#
0
-# foo.should_receive(:aasm_write_state)
0
-#
0
-# foo.close!
0
-# end
0
-#end
0
+
0
+class StateMachineEventFiringWithPersistenceTest < ActiveModel::TestCase
0
+ def setup
0
+ @subj = StateMachineSubject.new
0
+ end
0
+
0
+ test 'updates the current state' do
0
+ @subj.close!
0
+
0
+ assert_equal :closed, @subj.current_state
0
+ end
0
+
0
+ uses_mocha "StateMachineEventFiringWithPersistenceTest with callbacks" do
0
+ test 'fires the Event' do
0
+ @subj.class.state_machine.events[:close].expects(:fire).with(@subj)
0
+ @subj.close!
0
+ end
0
+
0
+ test 'calls the success callback if one was provided' do
0
+ @subj.expects(:success_callback)
0
+ @subj.close!
0
+ end
0
+
0
+ test 'attempts to persist if write_state is defined' do
0
+ def @subj.write_state
0
+ end
0
+
0
+ @subj.expects(:write_state)
0
+ @subj.close!
0
+ end
0
+ end
0
+end
0
 
0
 class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
0
   test 'updates the current state' do
0
@@ -162,30 +159,32 @@ class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
0
       subj.close
0
     end
0
 
0
- test 'should attempt to persist if aasm_write_state is defined' do
0
+ test 'attempts to persist if write_state is defined' do
0
       subj = StateMachineSubject.new
0
 
0
- def subj.aasm_write_state
0
+ def subj.write_state
0
       end
0
 
0
- subj.expects(:aasm_write_state_without_persistence)
0
+ subj.expects(:write_state_without_persistence)
0
 
0
       subj.close
0
     end
0
   end
0
 end
0
-
0
-#describe AASM, '- persistence' do
0
-# it 'should read the state if it has not been set and aasm_read_state is defined' do
0
-# foo = Foo.new
0
-# def foo.aasm_read_state
0
-# end
0
-#
0
-# foo.should_receive(:aasm_read_state)
0
-#
0
-# foo.aasm_current_state
0
-# end
0
-#end
0
+
0
+uses_mocha 'StateMachinePersistenceTest' do
0
+ class StateMachinePersistenceTest < ActiveModel::TestCase
0
+ test 'reads the state if it has not been set and read_state is defined' do
0
+ subj = StateMachineSubject.new
0
+ def subj.read_state
0
+ end
0
+
0
+ subj.expects(:read_state).with(StateMachineSubject.state_machine)
0
+
0
+ subj.current_state
0
+ end
0
+ end
0
+end
0
 
0
 uses_mocha 'StateMachineEventCallbacksTest' do
0
   class StateMachineEventCallbacksTest < ActiveModel::TestCase

Comments

    No one has commented yet.