mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
added specs for :after_commit hook
This commit is contained in:
parent
9f9bb07bc4
commit
41cdf5673d
2 changed files with 31 additions and 1 deletions
|
@ -4,13 +4,26 @@ class Validator < ActiveRecord::Base
|
||||||
include AASM
|
include AASM
|
||||||
aasm :column => :status do
|
aasm :column => :status do
|
||||||
state :sleeping, :initial => true
|
state :sleeping, :initial => true
|
||||||
state :running
|
state :running, :after_commit => :change_name!
|
||||||
|
state :failed, :after_enter => :fail, :after_commit => :change_name!
|
||||||
event :run do
|
event :run do
|
||||||
transitions :to => :running, :from => :sleeping
|
transitions :to => :running, :from => :sleeping
|
||||||
end
|
end
|
||||||
event :sleep do
|
event :sleep do
|
||||||
transitions :to => :sleeping, :from => :running
|
transitions :to => :sleeping, :from => :running
|
||||||
end
|
end
|
||||||
|
event :fail do
|
||||||
|
transitions :to => :failed, :from => [:sleeping, :running]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
|
|
||||||
|
def change_name!
|
||||||
|
self.name = "name changed"
|
||||||
|
save!
|
||||||
|
end
|
||||||
|
|
||||||
|
def fail
|
||||||
|
raise StandardError.new('failed on purpose')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -202,5 +202,22 @@ describe 'transitions with persistence' do
|
||||||
transactor.should be_running
|
transactor.should be_running
|
||||||
worker.reload.status.should == 'sleeping'
|
worker.reload.status.should == 'sleeping'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "after_commit callback" do
|
||||||
|
it "should fire :after_commit if transaction was successful" do
|
||||||
|
validator = Validator.create(:name => 'name')
|
||||||
|
validator.should be_sleeping
|
||||||
|
validator.run!
|
||||||
|
validator.should be_running
|
||||||
|
validator.name.should_not == "name"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not fire :after_commit if transaction failed" do
|
||||||
|
validator = Validator.create(:name => 'name')
|
||||||
|
lambda { validator.fail! }.should raise_error(StandardError, 'failed on purpose')
|
||||||
|
validator.name.should == "name"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue