mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Fix callback argument for :before_success & :success callback.
Simplify readme for callback arguments
This commit is contained in:
parent
4f697b8840
commit
a8ceb82b04
6 changed files with 25 additions and 15 deletions
|
@ -214,13 +214,11 @@ Also, you can pass parameters to events:
|
|||
|
||||
```ruby
|
||||
job = Job.new
|
||||
job.run(:running, :defragmentation)
|
||||
job.run(:defragmentation)
|
||||
```
|
||||
|
||||
In this case the `set_process` would be called with `:defragmentation` argument.
|
||||
|
||||
Note that when passing arguments to a state transition, the first argument should be the desired end state. In the above example, we wish to transition to `:running` state and run the callback with `:defragmentation` argument. You can also omit or pass in `nil` as the desired end state, and AASM will try to transition to the first end state defined for that event.
|
||||
|
||||
In case of an error during the event processing the error is rescued and passed to `:error`
|
||||
callback, which can handle it or re-raise it for further propagation.
|
||||
|
||||
|
|
|
@ -141,21 +141,20 @@ private
|
|||
persist = options[:persist]
|
||||
|
||||
new_state = aasm(state_machine_name).state_object_for_name(new_state_name)
|
||||
callback_args = process_args(event, aasm(state_machine_name).current_state, *args)
|
||||
|
||||
new_state.fire_callbacks(:before_enter, self,
|
||||
*process_args(event, aasm(state_machine_name).current_state, *args))
|
||||
new_state.fire_callbacks(:before_enter, self, *callback_args)
|
||||
|
||||
new_state.fire_callbacks(:enter, self,
|
||||
*process_args(event, aasm(state_machine_name).current_state, *args)) # TODO: remove for AASM 4?
|
||||
new_state.fire_callbacks(:enter, self, *callback_args) # TODO: remove for AASM 4?
|
||||
|
||||
persist_successful = true
|
||||
if persist
|
||||
persist_successful = aasm(state_machine_name).set_current_state_with_persistence(new_state_name)
|
||||
if persist_successful
|
||||
yield if block_given?
|
||||
event.fire_callbacks(:before_success, self)
|
||||
event.fire_callbacks(:before_success, self, *callback_args)
|
||||
event.fire_transition_callbacks(self, *process_args(event, old_state.name, *args))
|
||||
event.fire_callbacks(:success, self)
|
||||
event.fire_callbacks(:success, self, *callback_args)
|
||||
end
|
||||
else
|
||||
aasm(state_machine_name).current_state = new_state_name
|
||||
|
@ -168,10 +167,8 @@ private
|
|||
end
|
||||
|
||||
if persist_successful
|
||||
old_state.fire_callbacks(:after_exit, self,
|
||||
*process_args(event, aasm(state_machine_name).current_state, *args))
|
||||
new_state.fire_callbacks(:after_enter, self,
|
||||
*process_args(event, aasm(state_machine_name).current_state, *args))
|
||||
old_state.fire_callbacks(:after_exit, self, *callback_args)
|
||||
new_state.fire_callbacks(:after_enter, self, *callback_args)
|
||||
event.fire_callbacks(
|
||||
:after,
|
||||
self,
|
||||
|
|
|
@ -8,7 +8,7 @@ module Callbacks
|
|||
state :closed
|
||||
state :out_to_lunch
|
||||
|
||||
event :close, :before => :before_method, :after => :after_method do
|
||||
event :close, :before => :before_method, :after => :after_method, :before_success => :before_success_method, :success => :success_method3 do
|
||||
transitions :to => :closed, :from => [:open], :after => :transition_method, :success => :success_method
|
||||
transitions :to => :out_to_lunch, :from => [:open], :after => :transition_method2, :success => :success_method2
|
||||
end
|
||||
|
@ -16,6 +16,8 @@ module Callbacks
|
|||
|
||||
def before_method(arg); end
|
||||
|
||||
def before_success_method(arg); end
|
||||
|
||||
def after_method(arg); end
|
||||
|
||||
def transition_method(arg); end
|
||||
|
@ -26,5 +28,7 @@ module Callbacks
|
|||
|
||||
def success_method2(arg); end
|
||||
|
||||
def success_method3(arg); end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ module Callbacks
|
|||
state :closed
|
||||
state :out_to_lunch
|
||||
|
||||
event :close, :before => :before_method, :after => :after_method do
|
||||
event :close, :before => :before_method, :after => :after_method, :before_success => :before_success_method, :success => :success_method do
|
||||
transitions :to => :closed, :from => [:open], :after => :transition_method
|
||||
transitions :to => :out_to_lunch, :from => [:open], :after => :transition_method2
|
||||
end
|
||||
|
@ -16,11 +16,14 @@ module Callbacks
|
|||
|
||||
def before_method(arg); end
|
||||
|
||||
def before_success_method(arg); end
|
||||
|
||||
def after_method(arg); end
|
||||
|
||||
def transition_method(arg); end
|
||||
|
||||
def transition_method2(arg); end
|
||||
|
||||
def success_method(arg); end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -154,6 +154,8 @@ describe 'callbacks for the new DSL' do
|
|||
expect(cb).to receive(:before_method).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:transition_method).never
|
||||
expect(cb).to receive(:transition_method2).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:before_success_method).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:success_method).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:after_method).with(:arg1).once.ordered
|
||||
cb.close!(:out_to_lunch, :arg1)
|
||||
|
||||
|
@ -161,6 +163,8 @@ describe 'callbacks for the new DSL' do
|
|||
some_object = double('some object')
|
||||
expect(cb).to receive(:before_method).with(some_object).once.ordered
|
||||
expect(cb).to receive(:transition_method2).with(some_object).once.ordered
|
||||
expect(cb).to receive(:before_success_method).with(some_object).once.ordered
|
||||
expect(cb).to receive(:success_method).with(some_object).once.ordered
|
||||
expect(cb).to receive(:after_method).with(some_object).once.ordered
|
||||
cb.close!(:out_to_lunch, some_object)
|
||||
end
|
||||
|
|
|
@ -315,7 +315,9 @@ describe 'callbacks for the new DSL' do
|
|||
expect(cb).to receive(:before_method).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:transition_method).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:transition_method).never
|
||||
expect(cb).to receive(:before_success_method).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:success_method).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:success_method3).with(:arg1).once.ordered
|
||||
expect(cb).to receive(:success_method).never
|
||||
expect(cb).to receive(:after_method).with(:arg1).once.ordered
|
||||
cb.close!(:arg1)
|
||||
|
@ -325,7 +327,9 @@ describe 'callbacks for the new DSL' do
|
|||
expect(cb).to receive(:before_method).with(some_object).once.ordered
|
||||
expect(cb).to receive(:transition_method).with(some_object).once.ordered
|
||||
expect(cb).to receive(:transition_method).never
|
||||
expect(cb).to receive(:before_success_method).with(some_object).once.ordered
|
||||
expect(cb).to receive(:success_method).with(some_object).once.ordered
|
||||
expect(cb).to receive(:success_method3).with(some_object).once.ordered
|
||||
expect(cb).to receive(:success_method).never
|
||||
expect(cb).to receive(:after_method).with(some_object).once.ordered
|
||||
cb.close!(some_object)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue