Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…machine/trunk@45 a9f67fa3-8408-0410-ad84-b6cefbfd10ac
  • Loading branch information
sbarron committed Jan 20, 2006
1 parent 60c625f commit ff00b78
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
3 changes: 0 additions & 3 deletions TODO
Expand Up @@ -9,6 +9,3 @@
* Real transition actions

* Default states

* Entry/exit actions and guards can be a symbol representing a method name
instead of a proc.
17 changes: 14 additions & 3 deletions lib/acts_as_state_machine.rb
Expand Up @@ -18,7 +18,7 @@ def initialize(from, to, guard=nil)
end

def guard(obj)
@guard ? @guard.call(obj) : true
@guard ? obj.send(:run_transition_action, @guard) : true
end

def ==(obj)
Expand Down Expand Up @@ -79,6 +79,15 @@ def next_states_for_event(event)
s.from == current_state
end
end

def run_transition_action(action)
if action.kind_of?(Symbol)
self.method(action).call
else
action.call(self)
end
end
private :run_transition_action
end

module ClassMethods
Expand Down Expand Up @@ -120,9 +129,11 @@ def #{event.to_s}!
exitact = self.class.read_inheritable_attribute(:states)[current_state][:exit]
enteract = self.class.read_inheritable_attribute(:states)[ns.to][:enter]
enteract.call(self) if enteract && !loopback
run_transition_action(enteract) if enteract && !loopback
self.update_attribute(self.class.state_column, ns.to.to_s)
exitact.call(self) if exitact && !loopback
run_transition_action(exitact) if exitact && !loopback
break
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/acts_as_state_machine_test.rb
Expand Up @@ -117,6 +117,11 @@ def test_entry_and_exit_not_run_on_loopback_transition
assert !c.read_exit
end

def test_run_transition_action_is_private
c = Conversation.create
assert_raise(NoMethodError) { c.run_transition_action :foo }
end

def test_find_all_in_state
cs = Conversation.find_in_state(:all, :read)

Expand Down
14 changes: 11 additions & 3 deletions test/fixtures/conversation.rb
Expand Up @@ -5,7 +5,7 @@ class Conversation < ActiveRecord::Base
acts_as_state_machine :initial => :needs_attention, :column => 'state_machine'

state :needs_attention
state :read, :enter => Proc.new { |o| o.read_enter = true },
state :read, :enter => :read_enter_action,
:exit => Proc.new { |o| o.read_exit = true }
state :closed
state :awaiting_response
Expand All @@ -22,10 +22,10 @@ class Conversation < ActiveRecord::Base
event :reply do
transitions :to => :awaiting_response, :from => [:read, :closed]
end

event :close do
transitions :to => :closed, :from => [:read, :awaiting_response], :guard => Proc.new {|o| o.can_close?}
transitions :to => :read, :from => [:read, :awaiting_response]
transitions :to => :read, :from => [:read, :awaiting_response], :guard => :always_true
end

event :junk do
Expand All @@ -39,4 +39,12 @@ class Conversation < ActiveRecord::Base
def can_close?
@can_close
end

def read_enter_action
self.read_enter = true
end

def always_true
true
end
end

0 comments on commit ff00b78

Please sign in to comment.