public
Description: Fork of acts_as_state_machine plugin for Rails
Clone URL: git://github.com/omghax/acts_as_state_machine.git
Added a :value argument to the state macro, allowing you to specify a 
custom value which will be used to refer to the state in the database.  
This is especially useful for integrating acts_as_state_machine with a 
legacy database.
omghax (author)
Sat Apr 12 23:45:28 -0700 2008
commit  ee5b41fffde5ae329c0fd4ef28d037d2715dd5ea
tree    19487b8fef1481e7ecc7e068556ec3bcb6798e97
parent  45a5c4a6d7915392bc20dfe614f66c1943331ba3
...
15
16
17
18
 
19
20
21
 
22
23
24
...
40
41
42
43
44
45
 
 
 
 
 
46
47
48
...
51
52
53
54
 
55
56
57
 
58
59
60
61
 
62
63
64
...
86
87
88
89
 
90
91
92
...
138
139
140
141
 
142
143
144
...
151
152
153
154
 
155
156
157
158
159
 
160
161
162
...
169
170
171
172
 
173
174
175
...
217
218
219
220
 
221
222
 
223
224
225
...
258
259
260
261
 
262
263
264
...
15
16
17
 
18
19
20
21
22
23
24
25
...
41
42
43
 
 
 
44
45
46
47
48
49
50
51
...
54
55
56
 
57
58
59
 
60
61
62
63
 
64
65
66
67
...
89
90
91
 
92
93
94
95
...
141
142
143
 
144
145
146
147
...
154
155
156
 
157
158
159
160
161
 
162
163
164
165
...
172
173
174
 
175
176
177
178
...
220
221
222
 
223
224
 
225
226
227
228
...
261
262
263
 
264
265
266
267
0
@@ -15,10 +15,11 @@ module ScottBarron #:nodoc:
0
         NOOP = lambda { |o| true }
0
 
0
         class State
0
- attr_reader :name
0
+ attr_reader :name, :value
0
 
0
           def initialize(name, options)
0
             @name = name.to_sym
0
+ @value = (options[:value] || @name).to_s
0
             @after = Array(options[:after])
0
             @enter = options[:enter] || NOOP
0
             @exit = options[:exit] || NOOP
0
@@ -40,9 +41,11 @@ module ScottBarron #:nodoc:
0
         class StateTransition
0
           attr_reader :from, :to, :opts
0
 
0
- def initialize(opts)
0
- @from, @to, @guard = opts[:from], opts[:to], opts[:guard]
0
- @opts = opts
0
+ def initialize(options)
0
+ @from = options[:from].to_s
0
+ @to = options[:to].to_s
0
+ @guard = options[:guard] || NOOP
0
+ @opts = options
0
           end
0
 
0
           def guard(obj)
0
@@ -51,14 +54,14 @@ module ScottBarron #:nodoc:
0
 
0
           def perform(record)
0
             return false unless guard(record)
0
- loopback = record.current_state == to
0
+ loopback = record.current_state.to_s == to
0
             states = record.class.read_inheritable_attribute(:states)
0
             next_state = states[to]
0
- old_state = states[record.current_state]
0
+ old_state = states[record.current_state.to_s]
0
 
0
             next_state.entering(record) unless loopback
0
 
0
- record.update_attribute(record.class.state_column, to.to_s)
0
+ record.update_attribute(record.class.state_column, next_state.value)
0
 
0
             next_state.entered(record) unless loopback
0
             old_state.exited(record) unless loopback
0
@@ -86,7 +89,7 @@ module ScottBarron #:nodoc:
0
           end
0
 
0
           def next_states(record)
0
- @transitions.select { |t| t.from == record.current_state }
0
+ @transitions.select { |t| t.from == record.current_state.to_s }
0
           end
0
 
0
           def fire(record)
0
@@ -138,7 +141,7 @@ module ScottBarron #:nodoc:
0
         end
0
 
0
         def run_initial_state_actions
0
- initial = self.class.read_inheritable_attribute(:states)[self.class.initial_state.to_sym]
0
+ initial = self.class.read_inheritable_attribute(:states)[self.class.initial_state.to_s]
0
           initial.entering(self)
0
           initial.entered(self)
0
         end
0
@@ -151,12 +154,12 @@ module ScottBarron #:nodoc:
0
         # Returns what the next state for a given event would be, as a Ruby symbol.
0
         def next_state_for_event(event)
0
           ns = next_states_for_event(event)
0
- ns.empty? ? nil : ns.first.to
0
+ ns.empty? ? nil : ns.first.to.to_sym
0
         end
0
 
0
         def next_states_for_event(event)
0
           self.class.read_inheritable_attribute(:transition_table)[event.to_sym].select do |s|
0
- s.from == current_state
0
+ s.from == current_state.to_s
0
           end
0
         end
0
 
0
@@ -169,7 +172,7 @@ module ScottBarron #:nodoc:
0
       module ClassMethods
0
         # Returns an array of all known states.
0
         def states
0
- read_inheritable_attribute(:states).keys
0
+ read_inheritable_attribute(:states).keys.collect { |state| state.to_sym }
0
         end
0
 
0
         # Define an event. This takes a block which describes all valid transitions
0
@@ -217,9 +220,9 @@ module ScottBarron #:nodoc:
0
         # end
0
         def state(name, opts={})
0
           state = SupportingClasses::State.new(name, opts)
0
- write_inheritable_hash(:states, name.to_sym => state)
0
+ write_inheritable_hash(:states, state.value => state)
0
 
0
- define_method("#{state.name}?") { current_state == state.name }
0
+ define_method("#{state.name}?") { current_state.to_s == state.value }
0
         end
0
 
0
         # Wraps ActiveRecord::Base.find to conveniently find all records in
0
@@ -258,7 +261,7 @@ module ScottBarron #:nodoc:
0
 
0
         protected
0
         def with_state_scope(state)
0
- raise InvalidState unless states.include?(state)
0
+ raise InvalidState unless states.include?(state.to_sym)
0
 
0
           with_scope :find => {:conditions => ["#{table_name}.#{state_column} = ?", state.to_s]} do
0
             yield if block_given?
...
369
370
371
372
373
 
 
374
375
376
...
480
481
482
483
 
484
485
486
...
609
610
611
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612
...
369
370
371
 
 
372
373
374
375
376
...
480
481
482
 
483
484
485
486
...
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
0
@@ -369,8 +369,8 @@ class ActsAsStateMachineTest < Test::Unit::TestCase
0
       state :needs_attention
0
       state :closed, :after => :closed_after_action
0
       state :read, :enter => :read_enter_action,
0
- :exit => Proc.new { |o| o.read_exit = true },
0
- :after => [:read_after_first_action, :read_after_second_action]
0
+ :exit => Proc.new { |o| o.read_exit = true },
0
+ :after => [:read_after_first_action, :read_after_second_action]
0
 
0
       event :view do
0
         transitions :to => :read, :from => [:needs_attention, :read]
0
@@ -480,7 +480,7 @@ class ActsAsStateMachineTest < Test::Unit::TestCase
0
     Conversation.class_eval do
0
       acts_as_state_machine :initial => :needs_attention
0
       state :needs_attention, :enter => lambda { |o| o.needs_attention_enter = true },
0
- :after => lambda { |o| o.needs_attention_after = true }
0
+ :after => lambda { |o| o.needs_attention_after = true }
0
     end
0
 
0
     c = Conversation.create!
0
@@ -609,4 +609,23 @@ class ActsAsStateMachineTest < Test::Unit::TestCase
0
     assert_equal :junk, event.name
0
     assert_equal "finished", event.opts[:note]
0
   end
0
+
0
+ def test_custom_state_values
0
+ Conversation.class_eval do
0
+ acts_as_state_machine :initial => "NEEDS_ATTENTION", :column => "state_machine"
0
+ state :needs_attention, :value => "NEEDS_ATTENTION"
0
+ state :read, :value => "READ"
0
+
0
+ event :view do
0
+ transitions :to => "READ", :from => ["NEEDS_ATTENTION", "READ"]
0
+ end
0
+ end
0
+
0
+ c = Conversation.create!
0
+ assert_equal "NEEDS_ATTENTION", c.state_machine
0
+ assert c.needs_attention?
0
+ c.view!
0
+ assert_equal "READ", c.state_machine
0
+ assert c.read?
0
+ end
0
 end

Comments

    No one has commented yet.