diff --git a/spec/models/validator.rb b/spec/models/validator.rb index 02ad7ef..3c82695 100644 --- a/spec/models/validator.rb +++ b/spec/models/validator.rb @@ -4,13 +4,26 @@ class Validator < ActiveRecord::Base include AASM aasm :column => :status do state :sleeping, :initial => true - state :running + state :running, :after_commit => :change_name! + state :failed, :after_enter => :fail, :after_commit => :change_name! event :run do transitions :to => :running, :from => :sleeping end event :sleep do 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 fail + raise StandardError.new('failed on purpose') + end end diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index efe983c..1b9bfec 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -202,5 +202,22 @@ describe 'transitions with persistence' do transactor.should be_running worker.reload.status.should == 'sleeping' 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