mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
introduction of before transaction and after transaction callbacks
This commit is contained in:
parent
8002e5a1c8
commit
5e1a30e01d
5 changed files with 85 additions and 7 deletions
|
@ -13,7 +13,16 @@ module AASM::Core
|
|||
|
||||
# from aasm4
|
||||
@options = options # QUESTION: .dup ?
|
||||
add_options_from_dsl(@options, [:after, :before, :error, :success, :after_commit, :ensure], &block) if block
|
||||
add_options_from_dsl(@options, [
|
||||
:after,
|
||||
:after_commit,
|
||||
:after_transaction,
|
||||
:before,
|
||||
:before_transaction,
|
||||
:ensure,
|
||||
:error,
|
||||
:success,
|
||||
], &block) if block
|
||||
end
|
||||
|
||||
# a neutered version of fire - it doesn't actually fire the event, it just
|
||||
|
|
|
@ -145,11 +145,24 @@ module AASM
|
|||
end
|
||||
|
||||
def aasm_fire_event(state_machine_name, name, options, *args, &block)
|
||||
success = options[:persist] ? self.class.transaction(:requires_new => requires_new?(state_machine_name)) { super } : super
|
||||
event = self.class.aasm(state_machine_name).state_machine.events[name]
|
||||
|
||||
if success && options[:persist]
|
||||
event = self.class.aasm(state_machine_name).state_machine.events[name]
|
||||
event.fire_callbacks(:after_commit, self, *args)
|
||||
if options[:persist]
|
||||
event.fire_callbacks(:before_transaction, self, *args)
|
||||
event.fire_global_callbacks(:before_all_transactions, self, *args)
|
||||
end
|
||||
|
||||
begin
|
||||
success = options[:persist] ? self.class.transaction(:requires_new => requires_new?(state_machine_name)) { super } : super
|
||||
if options[:persist] && success
|
||||
event.fire_callbacks(:after_commit, self, *args)
|
||||
event.fire_global_callbacks(:after_all_commits, self, *args)
|
||||
end
|
||||
ensure
|
||||
if options[:persist]
|
||||
event.fire_callbacks(:after_transaction, self, *args)
|
||||
event.fire_global_callbacks(:after_all_transactions, self, *args)
|
||||
end
|
||||
end
|
||||
|
||||
success
|
||||
|
|
|
@ -2,22 +2,46 @@ require 'active_record'
|
|||
|
||||
class Validator < ActiveRecord::Base
|
||||
|
||||
attr_accessor :after_transaction_performed_on_fail,
|
||||
:after_transaction_performed_on_run,
|
||||
:before_transaction_performed_on_fail,
|
||||
:before_transaction_performed_on_run
|
||||
|
||||
include AASM
|
||||
|
||||
aasm :column => :status do
|
||||
state :sleeping, :initial => true
|
||||
state :running
|
||||
state :failed, :after_enter => :fail
|
||||
|
||||
event :run, :after_commit => :change_name! do
|
||||
after_transaction do
|
||||
@after_transaction_performed_on_run = true
|
||||
end
|
||||
|
||||
before_transaction do
|
||||
@before_transaction_performed_on_run = true
|
||||
end
|
||||
|
||||
transitions :to => :running, :from => :sleeping
|
||||
end
|
||||
|
||||
event :sleep do
|
||||
after_commit do |name|
|
||||
change_name_on_sleep name
|
||||
end
|
||||
transitions :to => :sleeping, :from => :running
|
||||
end
|
||||
|
||||
event :fail do
|
||||
after_transaction do
|
||||
@after_transaction_performed_on_fail = true
|
||||
end
|
||||
|
||||
before_transaction do
|
||||
@before_transaction_performed_on_fail = true
|
||||
end
|
||||
|
||||
transitions :to => :failed, :from => [:sleeping, :running]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -468,7 +468,39 @@ describe 'transitions with persistence' do
|
|||
expect(validator).to be_running
|
||||
expect(validator.name).to eq("name")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'before and after transaction callbacks' do
|
||||
[:after, :before].each do |event_type|
|
||||
describe "before_transaction callback" do
|
||||
it "should fire :#{event_type}_transaction if transaction was successful" do
|
||||
validator = Validator.create(:name => 'name')
|
||||
expect(validator).to be_sleeping
|
||||
|
||||
expect { validator.run! }.to change { validator.send("#{event_type}_transaction_performed_on_run") }.from(nil).to(true)
|
||||
expect(validator).to be_running
|
||||
end
|
||||
|
||||
it "should fire :before_transaction if transaction failed" do
|
||||
validator = Validator.create(:name => 'name')
|
||||
expect do
|
||||
begin
|
||||
validator.fail!
|
||||
rescue => ignored
|
||||
end
|
||||
end.to change { validator.send("#{event_type}_transaction_performed_on_fail") }.from(nil).to(true)
|
||||
expect(validator).to_not be_running
|
||||
end
|
||||
|
||||
it "should not fire 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") }
|
||||
expect(validator).to be_running
|
||||
expect(validator.name).to eq("name")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when not persisting" do
|
||||
|
|
Loading…
Add table
Reference in a new issue