mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Merge branch 'master' of https://github.com/Intrepidd/aasm into Intrepidd-master
Conflicts: lib/aasm/persistence/active_record_persistence.rb
This commit is contained in:
commit
1c6a6367e0
4 changed files with 35 additions and 8 deletions
|
@ -141,7 +141,7 @@ module AASM
|
|||
|
||||
private
|
||||
|
||||
def aasm_fire_event(event_name, options, *args)
|
||||
def aasm_fire_event(event_name, options, *args, &block)
|
||||
event = self.class.aasm_events[event_name]
|
||||
begin
|
||||
old_state = aasm.state_object_for_name(aasm.current_state)
|
||||
|
@ -151,7 +151,7 @@ private
|
|||
event.fire_callbacks(:before, self)
|
||||
|
||||
if new_state_name = event.fire(self, *args)
|
||||
fired(event, old_state, new_state_name, options)
|
||||
fired(event, old_state, new_state_name, options, &block)
|
||||
else
|
||||
failed(event_name, old_state)
|
||||
end
|
||||
|
@ -174,9 +174,13 @@ private
|
|||
persist_successful = true
|
||||
if persist
|
||||
persist_successful = aasm.set_current_state_with_persistence(new_state_name)
|
||||
event.fire_callbacks(:success, self) if persist_successful
|
||||
if persist_successful
|
||||
yield if block_given?
|
||||
event.fire_callbacks(:success, self)
|
||||
end
|
||||
else
|
||||
aasm.current_state = new_state_name
|
||||
yield if block_given?
|
||||
end
|
||||
|
||||
if persist_successful
|
||||
|
|
|
@ -57,12 +57,12 @@ module AASM
|
|||
aasm.may_fire_event?(name, *args)
|
||||
end
|
||||
|
||||
@clazz.send(:define_method, "#{name.to_s}!") do |*args|
|
||||
aasm_fire_event(name, {:persist => true}, *args)
|
||||
@clazz.send(:define_method, "#{name.to_s}!") do |*args, &block|
|
||||
aasm_fire_event(name, {:persist => true}, *args, &block)
|
||||
end
|
||||
|
||||
@clazz.send(:define_method, "#{name.to_s}") do |*args|
|
||||
aasm_fire_event(name, {:persist => false}, *args)
|
||||
@clazz.send(:define_method, "#{name.to_s}") do |*args, &block|
|
||||
aasm_fire_event(name, {:persist => false}, *args, &block)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -131,7 +131,7 @@ module AASM
|
|||
aasm.enter_initial_state if send(self.class.aasm_column).blank?
|
||||
end
|
||||
|
||||
def aasm_fire_event(name, options, *args)
|
||||
def aasm_fire_event(name, options, *args, &block)
|
||||
success = self.class.transaction(:requires_new => true) do
|
||||
super
|
||||
end
|
||||
|
|
|
@ -26,6 +26,29 @@ describe 'transitions' do
|
|||
silencer.should be_smiling
|
||||
end
|
||||
|
||||
it 'should call the block when success' do
|
||||
silencer = Silencer.new
|
||||
success = false
|
||||
lambda {
|
||||
silencer.smile_any! do
|
||||
success = true
|
||||
end
|
||||
}.should change { success }.to(true)
|
||||
end
|
||||
|
||||
it 'should not call the block when failure' do
|
||||
silencer = Silencer.new
|
||||
success = false
|
||||
lambda {
|
||||
silencer.smile! do
|
||||
success = true
|
||||
end
|
||||
}.should_not change { success }.to(true)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'blocks' do
|
||||
end
|
||||
|
||||
describe AASM::Transition do
|
||||
|
|
Loading…
Reference in a new issue