From 87253343709a41ca1c295565bf5c5e2c8eadb026 Mon Sep 17 00:00:00 2001 From: Robert Honer Date: Fri, 15 Aug 2014 12:19:41 -0700 Subject: [PATCH] No longer creating transaction when not persisting --- .../persistence/active_record_persistence.rb | 4 +--- .../active_record_persistence_spec.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb index 7b97a1d7..f2f0feb7 100644 --- a/lib/aasm/persistence/active_record_persistence.rb +++ b/lib/aasm/persistence/active_record_persistence.rb @@ -171,9 +171,7 @@ def aasm_ensure_initial_state end def aasm_fire_event(name, options, *args, &block) - success = self.class.transaction(:requires_new => requires_new?) do - super - end + success = options[:persist] ? self.class.transaction(:requires_new => requires_new?) { super } : super if success && options[:persist] new_state = aasm.state_object_for_name(aasm.current_state) diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index ebfd19bb..78e0290e 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -447,6 +447,23 @@ end end + + context "when not persisting" do + it 'should not rollback all changes' do + expect(transactor).to be_sleeping + expect(worker.status).to eq('sleeping') + + # Notice here we're calling "run" and not "run!" with a bang. + expect {transactor.run}.to raise_error(StandardError, 'failed on purpose') + expect(transactor).to be_running + expect(worker.reload.status).to eq('running') + end + + it 'should not create a database transaction' do + expect(transactor.class).not_to receive(:transaction) + expect {transactor.run}.to raise_error(StandardError, 'failed on purpose') + end + end end end