mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Added support for lambdas and arrays of method symbols to success callback
This commit is contained in:
parent
02cacead7e
commit
f44a808636
3 changed files with 73 additions and 1 deletions
|
@ -132,7 +132,7 @@ module AASM
|
|||
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
|
||||
|
|
11
lib/event.rb
11
lib/event.rb
|
@ -32,6 +32,17 @@ module AASM
|
|||
@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|
|
||||
|
|
|
@ -49,3 +49,64 @@ describe AASM::SupportingClasses::Event, 'when firing an event' do
|
|||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue