provide after_all_transitions callback

This commit is contained in:
Thorsten Böttger 2015-09-28 21:19:02 +13:00
parent 5e6a8f0f42
commit 6396847041
5 changed files with 19 additions and 1 deletions

View File

@ -109,6 +109,10 @@ module AASM
EORUBY
end
def after_all_transitions(*callbacks, &block)
@state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block)
end
def states
@state_machine.states
end

View File

@ -30,6 +30,7 @@ module AASM::Core
end
def execute(obj, *args)
invoke_callbacks_compatible_with_guard(event.state_machine.global_callbacks[:after_all_transitions], obj, args)
invoke_callbacks_compatible_with_guard(@after, obj, args)
end

View File

@ -10,12 +10,13 @@ module AASM
(@machines ||= {})[klass.to_s] = machine
end
attr_accessor :states, :events, :initial_state, :config, :name
attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks
def initialize(name)
@initial_state = nil
@states = []
@events = {}
@global_callbacks = {}
@config = AASM::Configuration.new
@name = name
end
@ -40,6 +41,14 @@ module AASM
@events[name] = AASM::Core::Event.new(name, self, options, &block)
end
def add_global_callbacks(name, *callbacks, &block)
@global_callbacks[name] ||= []
callbacks.each do |callback|
@global_callbacks[name] << callback
end
@global_callbacks[name] << block if block
end
private
def set_initial_state(name, options)

View File

@ -18,6 +18,8 @@ module Callbacks
end
aasm do
after_all_transitions :after_all_transitions
state :open, :initial => true,
:before_enter => :before_enter_open,
:enter => :enter_open,
@ -68,6 +70,7 @@ module Callbacks
def transition_guard; log('transition_guard'); !@fail_transition_guard; end
def after_transition; log('after_transition'); end
def after_all_transitions;log('after_all_transitions');end
def before_event; log('before_event'); end
def after_event; log('after_event'); end

View File

@ -17,6 +17,7 @@ describe 'callbacks for the new DSL' do
expect(callback).to receive(:exit_open).once.ordered
# expect(callback).to receive(:event_guard).once.ordered.and_return(true)
# expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
expect(callback).to receive(:after_all_transitions).once.ordered
expect(callback).to receive(:after_transition).once.ordered
expect(callback).to receive(:before_enter_closed).once.ordered
expect(callback).to receive(:enter_closed).once.ordered