mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Added success callback which takes place after persistent aasm writes
This commit is contained in:
parent
9e240e70cb
commit
fc86784602
4 changed files with 27 additions and 6 deletions
|
@ -33,9 +33,9 @@ module AASM
|
|||
end
|
||||
end
|
||||
|
||||
def aasm_event(name, &block)
|
||||
def aasm_event(name, options = {}, &block)
|
||||
unless aasm_events.has_key?(name)
|
||||
aasm_events[name] = AASM::SupportingClasses::Event.new(name, &block)
|
||||
aasm_events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
|
||||
end
|
||||
|
||||
define_method("#{name.to_s}!") do
|
||||
|
@ -46,6 +46,9 @@ module AASM
|
|||
end
|
||||
|
||||
self.aasm_current_state_with_persistence = new_state
|
||||
|
||||
self.send(self.class.aasm_events[name].success) if self.class.aasm_events[name].success
|
||||
|
||||
true
|
||||
else
|
||||
if self.respond_to?(:aasm_event_failed)
|
||||
|
|
|
@ -3,10 +3,11 @@ require File.join(File.dirname(__FILE__), 'state_transition')
|
|||
module AASM
|
||||
module SupportingClasses
|
||||
class Event
|
||||
attr_reader :name
|
||||
attr_reader :name, :success
|
||||
|
||||
def initialize(name, &block)
|
||||
def initialize(name, options = {}, &block)
|
||||
@name = name
|
||||
@success = options[:success]
|
||||
@transitions = []
|
||||
instance_eval(&block) if block
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@ class Foo
|
|||
aasm_state :open
|
||||
aasm_state :closed
|
||||
|
||||
aasm_event :close do
|
||||
aasm_event :close, :success => :success_callback do
|
||||
transitions :to => :closed, :from => [:open]
|
||||
end
|
||||
|
||||
|
@ -17,6 +17,9 @@ class Foo
|
|||
def always_false
|
||||
false
|
||||
end
|
||||
|
||||
def success_callback
|
||||
end
|
||||
end
|
||||
|
||||
class Bar
|
||||
|
@ -112,6 +115,14 @@ describe AASM, '- event firing with persistence' do
|
|||
foo.aasm_current_state.should == :closed
|
||||
end
|
||||
|
||||
it 'should call the success callback if one was provided' do
|
||||
foo = Foo.new
|
||||
|
||||
foo.should_receive(:success_callback)
|
||||
|
||||
foo.close!
|
||||
end
|
||||
|
||||
it 'should attempt to persist if aasm_write_state is defined' do
|
||||
foo = Foo.new
|
||||
|
||||
|
|
|
@ -3,10 +3,11 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
|||
describe AASM::SupportingClasses::Event do
|
||||
before(:each) do
|
||||
@name = :close_order
|
||||
@success = :success_callback
|
||||
end
|
||||
|
||||
def new_event
|
||||
@event = AASM::SupportingClasses::Event.new(@name) do
|
||||
@event = AASM::SupportingClasses::Event.new(@name, {:success => @success}) do
|
||||
transitions :to => :closed, :from => [:open, :received]
|
||||
end
|
||||
end
|
||||
|
@ -16,6 +17,11 @@ describe AASM::SupportingClasses::Event do
|
|||
@event.name.should == @name
|
||||
end
|
||||
|
||||
it 'should set the success option' do
|
||||
new_event
|
||||
@event.success.should == @success
|
||||
end
|
||||
|
||||
it 'should create StateTransitions' do
|
||||
AASM::SupportingClasses::StateTransition.should_receive(:new).with({:to => :closed, :from => :open})
|
||||
AASM::SupportingClasses::StateTransition.should_receive(:new).with({:to => :closed, :from => :received})
|
||||
|
|
Loading…
Reference in a new issue