Skip to content

Commit

Permalink
Fix #297 duplicate after_all_transitions after reload
Browse files Browse the repository at this point in the history
  • Loading branch information
lingceng committed Jan 20, 2016
1 parent bdf7e4c commit e750b83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/aasm/state_machine.rb
Expand Up @@ -44,7 +44,7 @@ def add_event(name, options, &block)
def add_global_callbacks(name, *callbacks, &block)
@global_callbacks[name] ||= []
callbacks.each do |callback|
@global_callbacks[name] << callback
@global_callbacks[name] << callback unless @global_callbacks[name].include? callback
end
@global_callbacks[name] << block if block
end
Expand Down
33 changes: 33 additions & 0 deletions spec/unit/callbacks_spec.rb
Expand Up @@ -94,6 +94,39 @@
callback.close!
end


it "works fine after reload" do
show_debug_log = false

callback = Callbacks::Basic.new(:log => show_debug_log)
callback.aasm.current_state

# reload the class
Callbacks.send(:remove_const, :Basic)
load 'models/callbacks/basic.rb'

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_all_transitions).once.ordered
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!
end

it "does not run any state callback if the event guard fails" do
callback = Callbacks::Basic.new(:log => false)
callback.aasm.current_state
Expand Down

0 comments on commit e750b83

Please sign in to comment.