2012-01-16 11:27:15 -05:00
|
|
|
require 'active_record'
|
|
|
|
|
|
|
|
class Validator < ActiveRecord::Base
|
2015-11-05 13:32:58 -05:00
|
|
|
attr_accessor :after_all_transactions_performed,
|
|
|
|
:after_transaction_performed_on_fail,
|
2015-11-05 12:53:33 -05:00
|
|
|
:after_transaction_performed_on_run,
|
2015-11-05 13:32:58 -05:00
|
|
|
:before_all_transactions_performed,
|
2015-11-05 12:53:33 -05:00
|
|
|
:before_transaction_performed_on_fail,
|
|
|
|
:before_transaction_performed_on_run
|
|
|
|
|
2012-01-16 11:27:15 -05:00
|
|
|
include AASM
|
2015-11-05 12:53:33 -05:00
|
|
|
|
2012-01-16 11:27:15 -05:00
|
|
|
aasm :column => :status do
|
2015-11-05 13:32:58 -05:00
|
|
|
before_all_transactions :before_all_transactions
|
|
|
|
after_all_transactions :after_all_transactions
|
|
|
|
|
2012-01-16 11:27:15 -05:00
|
|
|
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
|
2015-11-05 12:53:33 -05:00
|
|
|
after_transaction do
|
|
|
|
@after_transaction_performed_on_run = true
|
|
|
|
end
|
|
|
|
|
|
|
|
before_transaction do
|
|
|
|
@before_transaction_performed_on_run = true
|
|
|
|
end
|
|
|
|
|
2012-01-16 11:27:15 -05:00
|
|
|
transitions :to => :running, :from => :sleeping
|
|
|
|
end
|
2015-11-05 12:53:33 -05:00
|
|
|
|
2012-01-16 11:27:15 -05:00
|
|
|
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
|
2015-11-05 12:53:33 -05:00
|
|
|
|
2013-10-22 06:30:04 -04:00
|
|
|
event :fail do
|
2015-11-05 12:53:33 -05:00
|
|
|
after_transaction do
|
|
|
|
@after_transaction_performed_on_fail = true
|
|
|
|
end
|
|
|
|
|
|
|
|
before_transaction do
|
|
|
|
@before_transaction_performed_on_fail = true
|
|
|
|
end
|
|
|
|
|
2013-10-22 06:30:04 -04:00
|
|
|
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
|
2015-11-05 13:32:58 -05:00
|
|
|
|
|
|
|
def after_all_transactions
|
|
|
|
@after_all_transactions_performed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_all_transactions
|
|
|
|
@before_all_transactions_performed = true
|
|
|
|
end
|
2012-01-16 11:27:15 -05:00
|
|
|
end
|
2015-07-01 07:41:44 -04:00
|
|
|
|
|
|
|
class MultipleValidator < ActiveRecord::Base
|
|
|
|
|
|
|
|
include AASM
|
|
|
|
aasm :left, :column => :status do
|
|
|
|
state :sleeping, :initial => true
|
|
|
|
state :running
|
|
|
|
state :failed, :after_enter => :fail
|
|
|
|
|
|
|
|
event :run, :after_commit => :change_name! do
|
|
|
|
transitions :to => :running, :from => :sleeping
|
|
|
|
end
|
|
|
|
event :sleep do
|
|
|
|
after_commit do |name|
|
|
|
|
change_name_on_sleep name
|
|
|
|
end
|
|
|
|
transitions :to => :sleeping, :from => :running
|
|
|
|
end
|
|
|
|
event :fail do
|
|
|
|
transitions :to => :failed, :from => [:sleeping, :running]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
validates_presence_of :name
|
|
|
|
|
|
|
|
def change_name!
|
|
|
|
self.name = "name changed"
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_name_on_sleep name
|
|
|
|
self.name = name
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail
|
|
|
|
raise StandardError.new('failed on purpose')
|
|
|
|
end
|
|
|
|
end
|