diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index b760c3e1..f80d36b0 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -77,10 +77,10 @@ def aasm_enter_initial_state state_name = aasm_determine_state_name(self.class.aasm_initial_state) state = aasm_state_object_for_state(state_name) - state.call_action(:before_enter, self) - state.call_action(:enter, self) + state.fire_callbacks(:before_enter, self) + state.fire_callbacks(:enter, self) self.aasm_current_state = state_name - state.call_action(:after_enter, self) + state.fire_callbacks(:after_enter, self) state_name end @@ -156,10 +156,10 @@ def aasm_fire_event(name, persist, *args) old_state = aasm_state_object_for_state(aasm_current_state) - old_state.call_action(:exit, self) + old_state.fire_callbacks(:exit, self) # new event before callback - event.call_action(:before, self) + event.fire_callbacks(:before, self) new_state_name = event.fire(self, *args) @@ -167,10 +167,10 @@ def aasm_fire_event(name, persist, *args) new_state = aasm_state_object_for_state(new_state_name) # new before_ callbacks - old_state.call_action(:before_exit, self) - new_state.call_action(:before_enter, self) + old_state.fire_callbacks(:before_exit, self) + new_state.fire_callbacks(:before_enter, self) - new_state.call_action(:enter, self) + new_state.fire_callbacks(:enter, self) persist_successful = true if persist @@ -181,9 +181,9 @@ def aasm_fire_event(name, persist, *args) end if persist_successful - old_state.call_action(:after_exit, self) - new_state.call_action(:after_enter, self) - event.call_action(:after, self) + old_state.fire_callbacks(:after_exit, self) + new_state.fire_callbacks(:after_enter, self) + event.fire_callbacks(:after, self) self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired) else diff --git a/lib/aasm/supporting_classes/event.rb b/lib/aasm/supporting_classes/event.rb index 1b261e46..580b3fdd 100644 --- a/lib/aasm/supporting_classes/event.rb +++ b/lib/aasm/supporting_classes/event.rb @@ -55,11 +55,11 @@ def all_transitions @transitions end - def call_action(action, record) + def fire_callbacks(action, record) action = @options[action] action.is_a?(Array) ? - action.each {|a| _call_action(a, record)} : - _call_action(action, record) + action.each {|a| _fire_callbacks(a, record)} : + _fire_callbacks(action, record) end def ==(event) @@ -70,20 +70,6 @@ def ==(event) end end - def update(options = {}, &block) - if options.key?(:success) then - @success = options[:success] - end - if options.key?(:error) then - @error = options[:error] - end - if block then - instance_eval(&block) - end - @options = options - self - end - def execute_success_callback(obj, success = nil) callback = success || @success case(callback) @@ -110,9 +96,23 @@ def execute_error_callback(obj, error, error_callback=nil) end end - private + private - def _call_action(action, record) + def update(options = {}, &block) + if options.key?(:success) then + @success = options[:success] + end + if options.key?(:error) then + @error = options[:error] + end + if block then + instance_eval(&block) + end + @options = options + self + end + + def _fire_callbacks(action, record) case action when Symbol, String record.send(action) @@ -126,6 +126,7 @@ def transitions(trans_opts) @transitions << AASM::SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym})) end end + end end # SupportingClasses end # AASM diff --git a/lib/aasm/supporting_classes/state.rb b/lib/aasm/supporting_classes/state.rb index f443ccbf..8155a06b 100644 --- a/lib/aasm/supporting_classes/state.rb +++ b/lib/aasm/supporting_classes/state.rb @@ -16,12 +16,12 @@ def ==(state) end end - def call_action(action, record) + def fire_callbacks(action, record) action = @options[action] catch :halt_aasm_chain do action.is_a?(Array) ? - action.each {|a| _call_action(a, record)} : - _call_action(action, record) + action.each {|a| _fire_callbacks(a, record)} : + _fire_callbacks(action, record) end end @@ -33,6 +33,8 @@ def for_select [display_name, name.to_s] end + private + def update(options = {}) if options.key?(:display) then @display_name = options.delete(:display) @@ -41,9 +43,7 @@ def update(options = {}) self end - private - - def _call_action(action, record) + def _fire_callbacks(action, record) case action when Symbol, String record.send(action) diff --git a/spec/unit/state_spec.rb b/spec/unit/state_spec.rb index e51f2d10..f02fdfd8 100644 --- a/spec/unit/state_spec.rb +++ b/spec/unit/state_spec.rb @@ -41,7 +41,7 @@ def new_state(options={}) record = mock('record') record.should_receive(:foo) - state.call_action(:entering, record) + state.fire_callbacks(:entering, record) end it 'should send a message to the record for an action if the action is present as a string' do @@ -50,7 +50,7 @@ def new_state(options={}) record = mock('record') record.should_receive(:foo) - state.call_action(:entering, record) + state.fire_callbacks(:entering, record) end it 'should send a message to the record for each action' do @@ -62,7 +62,7 @@ def new_state(options={}) record.should_receive(:c) record.should_receive(:foobar) - state.call_action(:entering, record) + state.fire_callbacks(:entering, record) end it "should stop calling actions if one of them raises :halt_aasm_chain" do @@ -73,7 +73,7 @@ def new_state(options={}) record.should_receive(:b).and_throw(:halt_aasm_chain) record.should_not_receive(:c) - state.call_action(:entering, record) + state.fire_callbacks(:entering, record) end it 'should call a proc, passing in the record for an action if the action is present' do @@ -82,6 +82,6 @@ def new_state(options={}) record = mock('record') record.should_receive(:foobar) - state.call_action(:entering, record) + state.fire_callbacks(:entering, record) end end