diff --git a/CHANGELOG.md b/CHANGELOG.md index 75f9b2a..61169b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ## 4.0.4 (not yet released) + * corrected callback order in README * bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance (see [issue #191](https://github.com/aasm/aasm/issues/191) for details) * bugfix: avoid Rails autoloading conflicts (see [issue #137](https://github.com/aasm/aasm/issues/137) and [issue #139](https://github.com/aasm/aasm/issues/139) for details) diff --git a/README.md b/README.md index a54844a..d2055a5 100644 --- a/README.md +++ b/README.md @@ -145,11 +145,11 @@ begin transition guards old_state before_exit old_state exit + transition after new_state before_enter new_state enter - ...update state... - transition after - event success # if persist successful + ...update state... + event success # if persist successful old_state after_exit new_state after_enter event after diff --git a/spec/models/callbacks/basic.rb b/spec/models/callbacks/basic.rb index 05f992a..bb79a52 100644 --- a/spec/models/callbacks/basic.rb +++ b/spec/models/callbacks/basic.rb @@ -25,11 +25,11 @@ module Callbacks :exit => :exit_closed, :after_exit => :after_exit_closed - event :close, :before => :before, :after => :after, :guard => :event_guard do - transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :transitioning + event :close, :before => :before_event, :after => :after_event, :guard => :event_guard do + transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :after_transition end - event :open, :before => :before, :after => :after do + event :open, :before => :before_event, :after => :after_event do transitions :to => :open, :from => :closed end end @@ -54,9 +54,9 @@ module Callbacks def event_guard; log('event_guard'); !@fail_event_guard; end def transition_guard; log('transition_guard'); !@fail_transition_guard; end - def transitioning; log('transitioning'); end + def after_transition; log('after_transition'); end - def before; log('before'); end - def after; log('after'); end + def before_event; log('before_event'); end + def after_event; log('after_event'); end end end diff --git a/spec/unit/callbacks_spec.rb b/spec/unit/callbacks_spec.rb index 1830b61..ce9fcb7 100644 --- a/spec/unit/callbacks_spec.rb +++ b/spec/unit/callbacks_spec.rb @@ -4,23 +4,27 @@ Dir[File.dirname(__FILE__) + "/../models/callbacks/*.rb"].sort.each { |f| requir describe 'callbacks for the new DSL' do it "be called in order" do - callback = Callbacks::Basic.new + show_debug_log = false + + callback = Callbacks::Basic.new(:log => show_debug_log) callback.aasm.current_state - expect(callback).to receive(:before).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(:before_exit_open).once.ordered # these should be before the state changes - 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(:transitioning).once.ordered - expect(callback).to receive(:before_enter_closed).once.ordered - expect(callback).to receive(:enter_closed).once.ordered - expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes - expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes - expect(callback).to receive(:after_enter_closed).once.ordered - expect(callback).to receive(:after).once.ordered + unless show_debug_log + expect(callback).to receive(:before_event).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(:before_exit_open).once.ordered # these should be before the state changes + 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_transition).once.ordered + expect(callback).to receive(:before_enter_closed).once.ordered + expect(callback).to receive(:enter_closed).once.ordered + expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes + expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes + expect(callback).to receive(:after_enter_closed).once.ordered + expect(callback).to receive(:after_event).once.ordered + end # puts "------- close!" callback.close! @@ -30,18 +34,18 @@ describe 'callbacks for the new DSL' do callback = Callbacks::Basic.new(:log => false) callback.aasm.current_state - expect(callback).to receive(:before).once.ordered + expect(callback).to receive(:before_event).once.ordered expect(callback).to receive(:event_guard).once.ordered.and_return(false) expect(callback).to_not receive(:transition_guard) expect(callback).to_not receive(:before_exit_open) expect(callback).to_not receive(:exit_open) - expect(callback).to_not receive(:transitioning) + expect(callback).to_not receive(:after_transition) expect(callback).to_not receive(:before_enter_closed) expect(callback).to_not receive(:enter_closed) expect(callback).to_not receive(:aasm_write_state) expect(callback).to_not receive(:after_exit_open) expect(callback).to_not receive(:after_enter_closed) - expect(callback).to_not receive(:after) + expect(callback).to_not receive(:after_event) expect { callback.close! @@ -55,18 +59,18 @@ describe 'callbacks for the new DSL' do callback.aasm.current_state unless show_debug_log - expect(callback).to receive(:before).once.ordered + expect(callback).to receive(:before_event).once.ordered expect(callback).to receive(:event_guard).once.ordered.and_return(true) expect(callback).to receive(:transition_guard).once.ordered.and_return(false) expect(callback).to_not receive(:before_exit_open) expect(callback).to_not receive(:exit_open) - expect(callback).to_not receive(:transitioning) + expect(callback).to_not receive(:after_transition) expect(callback).to_not receive(:before_enter_closed) expect(callback).to_not receive(:enter_closed) expect(callback).to_not receive(:aasm_write_state) expect(callback).to_not receive(:after_exit_open) expect(callback).to_not receive(:after_enter_closed) - expect(callback).to_not receive(:after) + expect(callback).to_not receive(:after_event) end expect {