1
0
Fork 0
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:
HoyaBoya 2015-11-05 13:32:58 -05:00
parent 5e1a30e01d
commit ce66570a27
3 changed files with 58 additions and 5 deletions

View file

@ -113,6 +113,14 @@ module AASM
@state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block)
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)
@state_machine.add_global_callbacks(:before_all_events, *callbacks, &block)
end

View file

@ -1,15 +1,19 @@
require 'active_record'
class Validator < ActiveRecord::Base
attr_accessor :after_transaction_performed_on_fail,
attr_accessor :after_all_transactions_performed,
:after_transaction_performed_on_fail,
:after_transaction_performed_on_run,
:before_all_transactions_performed,
:before_transaction_performed_on_fail,
:before_transaction_performed_on_run
include AASM
aasm :column => :status do
before_all_transactions :before_all_transactions
after_all_transactions :after_all_transactions
state :sleeping, :initial => true
state :running
state :failed, :after_enter => :fail
@ -61,6 +65,14 @@ class Validator < ActiveRecord::Base
def fail
raise StandardError.new('failed on purpose')
end
def after_all_transactions
@after_all_transactions_performed = true
end
def before_all_transactions
@before_all_transactions_performed = true
end
end
class MultipleValidator < ActiveRecord::Base

View file

@ -472,7 +472,7 @@ describe 'transitions with persistence' do
describe 'before and after transaction callbacks' do
[: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
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
@ -481,7 +481,7 @@ describe 'transitions with persistence' do
expect(validator).to be_running
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')
expect do
begin
@ -492,7 +492,7 @@ describe 'transitions with persistence' do
expect(validator).to_not be_running
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')
expect(validator).to be_sleeping
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
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
it 'should not rollback all changes' do
expect(transactor).to be_sleeping