Skip to content

Commit

Permalink
Adding support to have arrays of procs, strings and symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
joaovitor committed Apr 9, 2009
1 parent e593ec2 commit f2c9c28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/event.rb
Expand Up @@ -31,15 +31,16 @@ def fire(obj, to_state=nil, *args)
def transitions_from_state?(state)
@transitions.any? { |t| t.from == state }
end

def execute_success_callback(obj)
case success

def execute_success_callback(obj, success = nil)
callback = success || @success
case(callback)
when String, Symbol
obj.send(success)
when Array
success.each { |meth| obj.send(meth) }
obj.send(callback)
when Proc
success.call(obj)
callback.call(obj)
when Array
callback.each{|meth|self.execute_success_callback(obj, meth)}
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/unit/event_spec.rb
Expand Up @@ -98,6 +98,20 @@ class ThisNameBetterNotBeInUse
model.with_array!
end

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

model = ThisNameBetterNotBeInUse.new
model.should_receive(:success_callback1)
model.should_receive(:success_callback2)
model.should_receive(:proc_success_callback)
model.with_array_including_procs!
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
Expand Down

0 comments on commit f2c9c28

Please sign in to comment.