Skip to content

Commit

Permalink
introduce before_transition and after_transition, so we can go after_…
Browse files Browse the repository at this point in the history
…transition { save! }, when using it with active record
  • Loading branch information
ryan-allen committed Apr 30, 2009
1 parent 0fc381d commit a0cf0f3
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/workflow.rb
Expand Up @@ -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 = []
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a0cf0f3

Please sign in to comment.