Skip to content

Commit

Permalink
small refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Nov 26, 2011
1 parent ae98094 commit 0c79ca0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
22 changes: 11 additions & 11 deletions lib/aasm/aasm.rb
Expand Up @@ -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
Expand Down Expand Up @@ -156,21 +156,21 @@ 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)

unless new_state_name.nil?
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
Expand All @@ -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
Expand Down
39 changes: 20 additions & 19 deletions lib/aasm/supporting_classes/event.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
12 changes: 6 additions & 6 deletions lib/aasm/supporting_classes/state.rb
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/state_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 0c79ca0

Please sign in to comment.