Skip to content

Commit

Permalink
Minor cleanup of Transition
Browse files Browse the repository at this point in the history
  • Loading branch information
obrie committed Aug 10, 2009
1 parent 9735a34 commit 39609dd
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/state_machine/transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class << self
# 1. Before callbacks
# 2. Persist state
# 3. Invoke action
# 4. After callbacks if configured
# 5. Rollback if action is unsuccessful
# 4. After callbacks (if configured)
# 5. Rollback (if action is unsuccessful)
#
# Configuration options:
# * <tt>:action</tt> - Whether to run the action configured for each transition
Expand Down Expand Up @@ -64,8 +64,9 @@ def perform(transitions, options = {})
raise
end

# Always run after callbacks regardless of whether the actions failed
transitions.each {|transition| transition.after(results[transition.action])} if options[:after] != false || !success
# Run after callbacks even when the actions failed. The :after option
# is ignored if the transitions were unsuccessful.
transitions.each {|transition| transition.after(results[transition.action])} unless options[:after] == false && success

# Rollback the transitions if the transaction was unsuccessful
transitions.each {|transition| transition.rollback} unless success
Expand Down Expand Up @@ -236,8 +237,11 @@ def before
result = false

catch(:halt) do
callback(:before) unless @before_run
@before_run = true
unless @before_run
callback(:before)
@before_run = true
end

result = true
end

Expand All @@ -264,8 +268,10 @@ def before
#
# vehicle.state # => 'idling'
def persist
machine.write(object, :state, to) unless @persisted
@persisted = true
unless @persisted
machine.write(object, :state, to)
@persisted = true
end
end

# Runs the machine's +after+ callbacks for this transition. Only
Expand Down Expand Up @@ -304,8 +310,10 @@ def after(result = nil)
@result = result

catch(:halt) do
callback(:after, :result => result) unless @after_run
@after_run = true
unless @after_run
callback(:after, :result => result)
@after_run = true
end
end

true
Expand Down

0 comments on commit 39609dd

Please sign in to comment.