Skip to content

Instantly share code, notes, and snippets.

@svs
Created August 28, 2012 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svs/3496546 to your computer and use it in GitHub Desktop.
Save svs/3496546 to your computer and use it in GitHub Desktop.
AWSM - After Workflow State Machine
class Document
# ....
# ..snip...
# ....
workflow do
state :created do
event :scan, :transitions_to => :scanned, :if => Proc.new{|t| !(t.scans.empty?)}
event :to_edit, :transitions_to => :editing
end
state :scanned do
event :to_edit, :transitions_to => :editing
end
state :editing do
# snip
end
state :completed do
# snip
end
state :incomplete do
# snip
end
state :illegible do
# snip
end
state :checker_rejected do
# snip
end
state :checker_accepted do
# snip
end
state :user_rejected do
# snip
end
state :user_accepted do
# snip
end
on_transition do |from, to, event, *event_args|
# put all access control validations here
# *args is basically current_user
halt! "not authorized" unless Ability.new(event_args[0]).can?(event, self) # sweet - all validations work through this one line.
AuditTrail.create(:from => from, :to => to, :event => event, :user => event_args[0], :model_name => self.class.to_s, :model_id => self.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment