public
Fork of rubyist/aasm
Description: AASM - State machines for Ruby classes
Homepage: http://rubyi.st/aasm/
Clone URL: git://github.com/zilkey/aasm.git
Add two event callbacks:
aasm_event_fired(from, to) - called when an event is fired successfully
aasm_event_failed(event)   - called when an event fails

These could be used to implement transition logging [Artem Vasiliev] or
setting a timestamp column [Mike Ferrier] as well as other pieces of love 
and magic.
rubyist (author)
Sun Mar 02 04:52:46 -0800 2008
commit  61b6b8ec063ec793ed2170494d87c45c9375868e
tree    accb27ca0793595b38c9f990bafe48315af152c4
parent  29304c70ae8a3d571b85f827935a63332cc2cb13
...
 
 
 
1
2
3
...
1
2
3
4
5
6
0
@@ -1,3 +1,6 @@
0
+* Add some event callbacks, #aasm_event_fired(from, to), and #aasm_event_failed(event)
0
+ Based on transition logging suggestion [Artem Vasiliev] and timestamp column suggestion [Mike Ferrier]
0
+
0
 * Add #aasm_events_for_state and #aasm_events_for_current_state [Joao Paulo Lins]
0
 
0
 * Ensure that a state is written for a new record even if aasm_current_state or
0
...
9
10
11
12
13
...
9
10
11
 
 
0
@@ -9,5 +9,3 @@ Cool ideas from users:
0
 
0
 * Support multiple state machines on one object (Chris Nelson)
0
 * http://justbarebones.blogspot.com/2007/11/actsasstatemachine-enhancements.html (Chetan Patil)
0
-* transition logging (Artem Vasiliev)
0
-* timestamp column for state change on AR (Mike Ferrier)
...
41
42
43
 
 
 
 
44
45
46
 
 
 
 
47
48
49
...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
0
@@ -41,9 +41,17 @@ module AASM
0
       define_method("#{name.to_s}!") do
0
         new_state = self.class.aasm_events[name].fire(self)
0
         unless new_state.nil?
0
+ if self.respond_to?(:aasm_event_fired)
0
+ self.aasm_event_fired(self.aasm_current_state, new_state)
0
+ end
0
+
0
           self.aasm_current_state = new_state
0
           true
0
         else
0
+ if self.respond_to?(:aasm_event_failed)
0
+ self.aasm_event_failed(name)
0
+ end
0
+
0
           false
0
         end
0
       end
...
9
10
11
 
 
 
 
 
 
 
 
12
13
14
...
122
123
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
0
@@ -9,6 +9,14 @@ class Foo
0
   aasm_event :close do
0
     transitions :to => :closed, :from => [:open]
0
   end
0
+
0
+ aasm_event :null do
0
+ transitions :to => :closed, :from => [:open], :guard => :always_false
0
+ end
0
+
0
+ def always_false
0
+ false
0
+ end
0
 end
0
 
0
 class Bar
0
@@ -122,3 +130,27 @@ describe AASM, '- getting events for a state' do
0
     foo.aasm_events_for_current_state
0
   end
0
 end
0
+
0
+describe AASM, '- event callbacks' do
0
+ it 'should call aasm_event_fired if defined and successful' do
0
+ foo = Foo.new
0
+ def foo.aasm_event_fired(from, to)
0
+ end
0
+
0
+ foo.should_receive(:aasm_event_fired)
0
+
0
+ foo.close!
0
+ end
0
+
0
+ it 'should call aasm_event_failed if defined and transition failed' do
0
+ foo = Foo.new
0
+ def foo.aasm_event_failed(event)
0
+ end
0
+
0
+ foo.should_receive(:aasm_event_failed)
0
+
0
+ foo.null!
0
+ end
0
+end
0
+
0
+

Comments

    No one has commented yet.