2012-01-16 11:27:15 -05:00
|
|
|
require 'active_record'
|
|
|
|
|
|
|
|
class Validator < ActiveRecord::Base
|
2014-03-17 16:58:44 -04:00
|
|
|
|
2012-01-16 11:27:15 -05:00
|
|
|
include AASM
|
|
|
|
aasm :column => :status do
|
|
|
|
state :sleeping, :initial => true
|
2014-03-17 16:58:44 -04:00
|
|
|
state :running
|
|
|
|
state :failed, :after_enter => :fail
|
|
|
|
|
|
|
|
event :run, :after_commit => :change_name! do
|
2012-01-16 11:27:15 -05:00
|
|
|
transitions :to => :running, :from => :sleeping
|
|
|
|
end
|
|
|
|
event :sleep do
|
2015-06-15 16:53:44 -04:00
|
|
|
after_commit do |name|
|
|
|
|
change_name_on_sleep name
|
2015-05-13 02:52:13 -04:00
|
|
|
end
|
2012-01-16 11:27:15 -05:00
|
|
|
transitions :to => :sleeping, :from => :running
|
|
|
|
end
|
2013-10-22 06:30:04 -04:00
|
|
|
event :fail do
|
|
|
|
transitions :to => :failed, :from => [:sleeping, :running]
|
|
|
|
end
|
2012-01-16 11:27:15 -05:00
|
|
|
end
|
2014-03-17 16:58:44 -04:00
|
|
|
|
2012-01-16 11:27:15 -05:00
|
|
|
validates_presence_of :name
|
2013-10-22 06:30:04 -04:00
|
|
|
|
|
|
|
def change_name!
|
|
|
|
self.name = "name changed"
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2015-06-15 16:53:44 -04:00
|
|
|
def change_name_on_sleep name
|
|
|
|
self.name = name
|
2015-05-13 02:52:13 -04:00
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2013-10-22 06:30:04 -04:00
|
|
|
def fail
|
|
|
|
raise StandardError.new('failed on purpose')
|
|
|
|
end
|
2012-01-16 11:27:15 -05:00
|
|
|
end
|