Add binding event (#438)

* add binding_event

* Add specs for binding_event
This commit is contained in:
Dao Le 2017-02-28 17:55:06 +08:00 committed by Anil Kumar Maurya
parent c6a4955e3d
commit 712a144966
3 changed files with 45 additions and 0 deletions

View File

@ -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))

View File

@ -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

View File

@ -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