Skip to content

Commit

Permalink
Added support for lambdas and arrays of method symbols to success cal…
Browse files Browse the repository at this point in the history
…lback
  • Loading branch information
Jon Distad committed Oct 9, 2008
1 parent 02cacea commit f44a808
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/aasm.rb
Expand Up @@ -132,7 +132,7 @@ def aasm_fire_event(name, persist, *args)
persist_successful = true
if persist
persist_successful = set_aasm_current_state_with_persistence(new_state)
self.send(self.class.aasm_events[name].success) if persist_successful && self.class.aasm_events[name].success
self.class.aasm_events[name].execute_success_callback(self) if persist_successful
else
self.aasm_current_state = new_state
end
Expand Down
11 changes: 11 additions & 0 deletions lib/event.rb
Expand Up @@ -32,6 +32,17 @@ def transitions_from_state?(state)
@transitions.any? { |t| t.from == state }
end

def execute_success_callback(obj)
case success
when String, Symbol:
obj.send(success)
when Array:
success.each { |meth| obj.send(meth) }
when Proc:
success.call(obj)
end
end

private
def transitions(trans_opts)
Array(trans_opts[:from]).each do |s|
Expand Down
61 changes: 61 additions & 0 deletions spec/unit/event_spec.rb
Expand Up @@ -49,3 +49,64 @@ def new_event
event.fire(obj).should == :closed
end
end

describe AASM::SupportingClasses::Event, 'when executing the success callback' do
class ThisNameBetterNotBeInUse
include AASM

aasm_state :initial
aasm_state :symbol
aasm_state :string
aasm_state :array
aasm_state :proc
end

it "should send the success callback if it's a symbol" do
ThisNameBetterNotBeInUse.instance_eval {
aasm_event :with_symbol, :success => :symbol_success_callback do
transitions :to => :symbol, :from => [:initial]
end
}

model = ThisNameBetterNotBeInUse.new
model.should_receive(:symbol_success_callback)
model.with_symbol!
end

it "should send the success callback if it's a string" do
ThisNameBetterNotBeInUse.instance_eval {
aasm_event :with_string, :success => 'string_success_callback' do
transitions :to => :string, :from => [:initial]
end
}

model = ThisNameBetterNotBeInUse.new
model.should_receive(:string_success_callback)
model.with_string!
end

it "should call each success callback if passed an array of strings and/or symbols" do
ThisNameBetterNotBeInUse.instance_eval {
aasm_event :with_array, :success => [:success_callback1, 'success_callback2'] do
transitions :to => :array, :from => [:initial]
end
}

model = ThisNameBetterNotBeInUse.new
model.should_receive(:success_callback1)
model.should_receive(:success_callback2)
model.with_array!
end

it "should call the success callback if it's a proc" do
ThisNameBetterNotBeInUse.instance_eval {
aasm_event :with_proc, :success => lambda { |obj| obj.proc_success_callback } do
transitions :to => :proc, :from => [:initial]
end
}

model = ThisNameBetterNotBeInUse.new
model.should_receive(:proc_success_callback)
model.with_proc!
end
end

0 comments on commit f44a808

Please sign in to comment.