Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Created July 16, 2012 23:54
Show Gist options
  • Save mgreenly/3125936 to your computer and use it in GitHub Desktop.
Save mgreenly/3125936 to your computer and use it in GitHub Desktop.
WorkflowEventsController < ApplicationController
# . . .
def create
workflow_event = WorkflowEvent.new(params[:workflow_event])
@some_model = SomeModel.find(params[:some_model_id]
SomeWorkflow.new(current_user, @some_model).handle(workflow_event) do |result|
result.on_success { render :template => 'success' }
result.on_failure { render :template => 'failure' }
end
end
# . . .
end
#
# example workflow returning multiple results
#
class SomeWorkflow < Workflow
include SimpleStates
states :created, :started, :finished
def startable?
return true if @subject.has_claim?
@subject.errors[:base] << 'There must be an attached labor or part claim.'
false
end
event :start, :to => :started, :if => :startable?, :from => [:created]
event :finish, :to => :finished, :if => nil, :from => [:started]
# you can call return_#{any_result_class} This works a bit like
# rendering in controllers. If you don't call at least one there
# will be an exception
def start
if unspecifified_condition
return_success # causes the result in the controller's handle block to yield to on_failure
else
return_failure('foo') # causes the result in the controller's handle block to yield to on_failure and pass in 'foo'
end
end
# Except if you don't define the method at all then it will automatically return what
# ever is defined using as the default result. So we never defined a 'finish' method.
# So if we define the default like so
default_result :return_success
# The 'finish' method that doesn't exist will behave like this
#
# def finish
# return_success
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment