mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Add binding event (#438)
* add binding_event * Add specs for binding_event
This commit is contained in:
parent
c6a4955e3d
commit
712a144966
3 changed files with 45 additions and 0 deletions
|
@ -159,6 +159,11 @@ private
|
|||
yield if block_given?
|
||||
end
|
||||
|
||||
binding_event = event.options[:binding_event]
|
||||
if binding_event
|
||||
__send__("#{binding_event}#{'!' if persist}")
|
||||
end
|
||||
|
||||
if persist_successful
|
||||
old_state.fire_callbacks(:after_exit, self,
|
||||
*process_args(event, aasm(state_machine_name).current_state, *args))
|
||||
|
|
|
@ -27,4 +27,16 @@ class SimpleMultipleExample
|
|||
transitions :from => :processing, :to => :sleeping
|
||||
end
|
||||
end
|
||||
|
||||
aasm(:question) do
|
||||
state :answered, :initial => true
|
||||
state :asked
|
||||
|
||||
event :ask, :binding_event => :start do
|
||||
transitions :from => :answered, :to => :asked
|
||||
end
|
||||
event :answer, :binding_event => :stop do
|
||||
transitions :from => :asked, :to => :answered
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -60,4 +60,32 @@ describe 'state machine' do
|
|||
expect(SimpleMultipleExample::STATE_PROCESSING).to eq(:processing)
|
||||
expect(SimpleMultipleExample::STATE_RUNNING).to eq(:running)
|
||||
end
|
||||
|
||||
context 'triggers binding_events in bindind_state_machine' do
|
||||
it 'does persist' do
|
||||
expect(simple).to be_sleeping
|
||||
expect(simple).to be_answered
|
||||
expect(simple).to receive(:start!).and_call_original
|
||||
simple.ask!
|
||||
expect(simple).to be_asked
|
||||
expect(simple).to be_processing
|
||||
expect(simple).to receive(:stop!).and_call_original
|
||||
simple.answer!
|
||||
expect(simple).to be_sleeping
|
||||
expect(simple).to be_answered
|
||||
end
|
||||
|
||||
it 'does not persist' do
|
||||
expect(simple).to be_sleeping
|
||||
expect(simple).to be_answered
|
||||
expect(simple).to receive(:start).and_call_original
|
||||
simple.ask
|
||||
expect(simple).to be_asked
|
||||
expect(simple).to be_processing
|
||||
expect(simple).to receive(:stop).and_call_original
|
||||
simple.answer
|
||||
expect(simple).to be_sleeping
|
||||
expect(simple).to be_answered
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue