This commit is contained in:
Thorsten Böttger 2014-03-04 23:43:41 +01:00
parent 85ed696500
commit 519310ac5f
3 changed files with 29 additions and 2 deletions

View File

@ -138,6 +138,7 @@ module AASM
end
def aasm_fire_event(name, options, *args, &block)
puts "------- aasm_fire_event"
success = self.class.transaction(:requires_new => requires_new?) do
super
end

View File

@ -3,7 +3,7 @@ require 'active_record'
class Validator < ActiveRecord::Base
include AASM
aasm :column => :status do
state :sleeping, :initial => true
state :sleeping, :after_commit => :yawn, :initial => true
state :running, :after_commit => :change_name!
state :failed, :after_enter => :fail, :after_commit => :change_name!
event :run do
@ -23,7 +23,27 @@ class Validator < ActiveRecord::Base
save!
end
def yawn
# well, yawn
end
def fail
raise StandardError.new('failed on purpose')
end
end
aasm 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, :after_commit => :yawn do
transitions :to => :sleeping, :from => :running
end
event :fail do
transitions :to => :failed, :from => [:sleeping, :running]
end
end

View File

@ -203,6 +203,13 @@ describe 'transitions with persistence' do
end
describe "after_commit callback" do
it "should fire :after_commit on initial create" do
validator = Validator.new(:name => "name")
expect(validator).to receive(:yawn).once
validator.save!
expect(validator).to be_sleeping
end
it "should fire :after_commit if transaction was successful" do
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
@ -216,7 +223,6 @@ describe 'transitions with persistence' do
expect { validator.fail! }.to raise_error(StandardError, 'failed on purpose')
expect(validator.name).to eq("name")
end
end
end
end