1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

small refactorings

This commit is contained in:
Thorsten Böttger 2011-11-26 20:07:56 +01:00
parent ae98094ee8
commit 0c79ca0a56
4 changed files with 42 additions and 41 deletions

View file

@ -77,10 +77,10 @@ module AASM
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 @@ private
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 @@ private
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 @@ private
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

View file

@ -55,11 +55,11 @@ module AASM
@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 @@ module AASM
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 @@ module AASM
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 @@ module AASM
@transitions << AASM::SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym}))
end
end
end
end # SupportingClasses
end # AASM

View file

@ -16,12 +16,12 @@ module AASM
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 @@ module AASM
[display_name, name.to_s]
end
private
def update(options = {})
if options.key?(:display) then
@display_name = options.delete(:display)
@ -41,9 +43,7 @@ module AASM
self
end
private
def _call_action(action, record)
def _fire_callbacks(action, record)
case action
when Symbol, String
record.send(action)

View file

@ -41,7 +41,7 @@ describe AASM::SupportingClasses::State do
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 @@ describe AASM::SupportingClasses::State do
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 @@ describe AASM::SupportingClasses::State do
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 @@ describe AASM::SupportingClasses::State do
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 @@ describe AASM::SupportingClasses::State do
record = mock('record')
record.should_receive(:foobar)
state.call_action(:entering, record)
state.fire_callbacks(:entering, record)
end
end