mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
introduce before and after all transactions
This commit is contained in:
parent
5e1a30e01d
commit
ce66570a27
3 changed files with 58 additions and 5 deletions
|
@ -113,6 +113,14 @@ module AASM
|
||||||
@state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block)
|
@state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def after_all_transactions(*callbacks, &block)
|
||||||
|
@state_machine.add_global_callbacks(:after_all_transactions, *callbacks, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def before_all_transactions(*callbacks, &block)
|
||||||
|
@state_machine.add_global_callbacks(:before_all_transactions, *callbacks, &block)
|
||||||
|
end
|
||||||
|
|
||||||
def before_all_events(*callbacks, &block)
|
def before_all_events(*callbacks, &block)
|
||||||
@state_machine.add_global_callbacks(:before_all_events, *callbacks, &block)
|
@state_machine.add_global_callbacks(:before_all_events, *callbacks, &block)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
require 'active_record'
|
require 'active_record'
|
||||||
|
|
||||||
class Validator < ActiveRecord::Base
|
class Validator < ActiveRecord::Base
|
||||||
|
attr_accessor :after_all_transactions_performed,
|
||||||
attr_accessor :after_transaction_performed_on_fail,
|
:after_transaction_performed_on_fail,
|
||||||
:after_transaction_performed_on_run,
|
:after_transaction_performed_on_run,
|
||||||
|
:before_all_transactions_performed,
|
||||||
:before_transaction_performed_on_fail,
|
:before_transaction_performed_on_fail,
|
||||||
:before_transaction_performed_on_run
|
:before_transaction_performed_on_run
|
||||||
|
|
||||||
include AASM
|
include AASM
|
||||||
|
|
||||||
aasm :column => :status do
|
aasm :column => :status do
|
||||||
|
before_all_transactions :before_all_transactions
|
||||||
|
after_all_transactions :after_all_transactions
|
||||||
|
|
||||||
state :sleeping, :initial => true
|
state :sleeping, :initial => true
|
||||||
state :running
|
state :running
|
||||||
state :failed, :after_enter => :fail
|
state :failed, :after_enter => :fail
|
||||||
|
@ -61,6 +65,14 @@ class Validator < ActiveRecord::Base
|
||||||
def fail
|
def fail
|
||||||
raise StandardError.new('failed on purpose')
|
raise StandardError.new('failed on purpose')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def after_all_transactions
|
||||||
|
@after_all_transactions_performed = true
|
||||||
|
end
|
||||||
|
|
||||||
|
def before_all_transactions
|
||||||
|
@before_all_transactions_performed = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class MultipleValidator < ActiveRecord::Base
|
class MultipleValidator < ActiveRecord::Base
|
||||||
|
|
|
@ -472,7 +472,7 @@ describe 'transitions with persistence' do
|
||||||
|
|
||||||
describe 'before and after transaction callbacks' do
|
describe 'before and after transaction callbacks' do
|
||||||
[:after, :before].each do |event_type|
|
[:after, :before].each do |event_type|
|
||||||
describe "before_transaction callback" do
|
describe "#{event_type}_transaction callback" do
|
||||||
it "should fire :#{event_type}_transaction if transaction was successful" do
|
it "should fire :#{event_type}_transaction if transaction was successful" do
|
||||||
validator = Validator.create(:name => 'name')
|
validator = Validator.create(:name => 'name')
|
||||||
expect(validator).to be_sleeping
|
expect(validator).to be_sleeping
|
||||||
|
@ -481,7 +481,7 @@ describe 'transitions with persistence' do
|
||||||
expect(validator).to be_running
|
expect(validator).to be_running
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should fire :before_transaction if transaction failed" do
|
it "should fire :#{event_type}_transaction if transaction failed" do
|
||||||
validator = Validator.create(:name => 'name')
|
validator = Validator.create(:name => 'name')
|
||||||
expect do
|
expect do
|
||||||
begin
|
begin
|
||||||
|
@ -492,7 +492,7 @@ describe 'transitions with persistence' do
|
||||||
expect(validator).to_not be_running
|
expect(validator).to_not be_running
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not fire if not saving" do
|
it "should not fire :#{event_type}_transaction if not saving" do
|
||||||
validator = Validator.create(:name => 'name')
|
validator = Validator.create(:name => 'name')
|
||||||
expect(validator).to be_sleeping
|
expect(validator).to be_sleeping
|
||||||
expect { validator.run }.to_not change { validator.send("#{event_type}_transaction_performed_on_run") }
|
expect { validator.run }.to_not change { validator.send("#{event_type}_transaction_performed_on_run") }
|
||||||
|
@ -503,6 +503,39 @@ describe 'transitions with persistence' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'before and after all transactions callbacks' do
|
||||||
|
[:after, :before].each do |event_type|
|
||||||
|
describe "#{event_type}_all_transactions callback" do
|
||||||
|
it "should fire :#{event_type}_all_transactions if transaction was successful" do
|
||||||
|
validator = Validator.create(:name => 'name')
|
||||||
|
expect(validator).to be_sleeping
|
||||||
|
|
||||||
|
expect { validator.run! }.to change { validator.send("#{event_type}_all_transactions_performed") }.from(nil).to(true)
|
||||||
|
expect(validator).to be_running
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should fire :#{event_type}_all_transactions if transaction failed" do
|
||||||
|
validator = Validator.create(:name => 'name')
|
||||||
|
expect do
|
||||||
|
begin
|
||||||
|
validator.fail!
|
||||||
|
rescue => ignored
|
||||||
|
end
|
||||||
|
end.to change { validator.send("#{event_type}_all_transactions_performed") }.from(nil).to(true)
|
||||||
|
expect(validator).to_not be_running
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not fire :#{event_type}_all_transactions if not saving" do
|
||||||
|
validator = Validator.create(:name => 'name')
|
||||||
|
expect(validator).to be_sleeping
|
||||||
|
expect { validator.run }.to_not change { validator.send("#{event_type}_all_transactions_performed") }
|
||||||
|
expect(validator).to be_running
|
||||||
|
expect(validator.name).to eq("name")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "when not persisting" do
|
context "when not persisting" do
|
||||||
it 'should not rollback all changes' do
|
it 'should not rollback all changes' do
|
||||||
expect(transactor).to be_sleeping
|
expect(transactor).to be_sleeping
|
||||||
|
|
Loading…
Reference in a new issue