From 712a1449668008e978cadb9a0be5bfc49ca249b4 Mon Sep 17 00:00:00 2001 From: Dao Le Date: Tue, 28 Feb 2017 17:55:06 +0800 Subject: [PATCH] Add binding event (#438) * add binding_event * Add specs for binding_event --- lib/aasm/aasm.rb | 5 ++++ spec/models/simple_multiple_example.rb | 12 ++++++++++ spec/unit/simple_multiple_example_spec.rb | 28 +++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 75ad6a0..0711a14 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -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)) diff --git a/spec/models/simple_multiple_example.rb b/spec/models/simple_multiple_example.rb index 81ef414..7b79122 100644 --- a/spec/models/simple_multiple_example.rb +++ b/spec/models/simple_multiple_example.rb @@ -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 diff --git a/spec/unit/simple_multiple_example_spec.rb b/spec/unit/simple_multiple_example_spec.rb index 7dc5ab4..c7eaf21 100644 --- a/spec/unit/simple_multiple_example_spec.rb +++ b/spec/unit/simple_multiple_example_spec.rb @@ -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