Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

introduce before_transition and after_transition, so we can go after_…

…transition { save! }, when using it with active record
  • Loading branch information...
commit a0cf0f37c6206cbdb628893ffcd70d1a66445684 1 parent 0fc381d
@ryan-allen ryan-allen authored
Showing with 25 additions and 6 deletions.
  1. +25 −6 lib/workflow.rb
View
31 lib/workflow.rb
@@ -111,7 +111,7 @@ def initialize(*args, &block)
class Specification
- attr_accessor :states, :meta, :on_transition
+ attr_accessor :states, :meta, :on_transition, :before_transition, :after_transition
def initialize(meta = {}, &specification)
@states = []
@@ -120,7 +120,7 @@ def initialize(meta = {}, &specification)
end
def to_instance(reconstitute_at = nil)
- Instance.new(states, @on_transition, @meta, reconstitute_at)
+ Instance.new(states, @before_transition, @on_transition, @after_transition, @meta, reconstitute_at)
end
def blat(meta = {}, &specification)
@@ -140,6 +140,14 @@ def on_transition(&proc)
@on_transition = proc
end
+ def before_transition(&proc)
+ @before_transition = proc
+ end
+
+ def after_transition(&proc)
+ @after_transition = proc
+ end
+
def event(name, args = {}, &action)
scoped_state.add_event Event.new(name, args[:transitions_to], (args[:meta] or {}), &action)
end
@@ -169,11 +177,12 @@ def initialize(msg = nil)
end
end
- attr_accessor :states, :meta, :current_state, :on_transition, :context
+ attr_accessor :states, :meta, :current_state, :on_transition, :before_transition, :after_transition, :context
- def initialize(states, on_transition, meta = {}, reconstitute_at = nil)
+ def initialize(states, before_transition, on_transition, after_transition, meta = {}, reconstitute_at = nil)
Workflow.logger.debug "Creating workflow instance"
- @states, @on_transition, @meta = states, on_transition, meta
+ @states, @meta = states, on_transition, meta
+ @before_transition, @on_transition, @after_transition = before_transition, on_transition, after_transition
@context = self
if reconstitute_at.nil?
transition(nil, states.first, nil)
@@ -284,8 +293,10 @@ def process_event!(name, *args)
false
end
else
- run_on_transition(current_state, states(event.transitions_to), name, *args)
+ # run_on_transition(current_state, states(event.transitions_to), name, *args)
+ run_before_transition(current_state, states(event.transitions_to), name, *args)
transition(current_state, states(event.transitions_to), name, *args)
+ run_after_transition(current_state, states(event.transitions_to), name, *args)
return_value
end
end
@@ -318,10 +329,18 @@ def transition(from, to, name, *args)
run_on_entry(to, from, name, *args)
end
+ def run_before_transition(from, to, event, *args)
+ context.instance_exec(from.name, to.name, event, *args, &before_transition) if before_transition
+ end
+
def run_on_transition(from, to, event, *args)
context.instance_exec(from.name, to.name, event, *args, &on_transition) if on_transition
end
+ def run_after_transition(from, to, event, *args)
+ context.instance_exec(from.name, to.name, event, *args, &after_transition) if after_transition
+ end
+
def run_action(action, *args)
context.instance_exec(*args, &action) if action
end
Please sign in to comment.
Something went wrong with that request. Please try again.