mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
implemented inline before/after callbacks for events
This commit is contained in:
parent
0140e420f2
commit
9f39a22231
3 changed files with 20 additions and 1 deletions
|
@ -85,6 +85,9 @@ class Job
|
|||
end
|
||||
|
||||
event :sleep do
|
||||
after do
|
||||
...
|
||||
end
|
||||
transitions :from => :running, :to => :sleeping
|
||||
end
|
||||
end
|
||||
|
|
|
@ -90,10 +90,10 @@ module AASM
|
|||
if options.key?(:error) then
|
||||
@error = options[:error]
|
||||
end
|
||||
@options = options
|
||||
if block then
|
||||
instance_eval(&block)
|
||||
end
|
||||
@options = options
|
||||
self
|
||||
end
|
||||
|
||||
|
@ -132,6 +132,7 @@ module AASM
|
|||
end
|
||||
end
|
||||
|
||||
## DSL interface
|
||||
def transitions(trans_opts)
|
||||
# Create a separate transition for each from state to the given state
|
||||
Array(trans_opts[:from]).each do |s|
|
||||
|
@ -141,6 +142,11 @@ module AASM
|
|||
@transitions << AASM::SupportingClasses::StateTransition.new(trans_opts) if @transitions.empty? && trans_opts[:to]
|
||||
end
|
||||
|
||||
[:after, :before].each do |callback_name| # add :success, :error ?
|
||||
define_method callback_name do |*args, &block|
|
||||
options[callback_name] = block ? block : args
|
||||
end
|
||||
end
|
||||
end
|
||||
end # SupportingClasses
|
||||
end # AASM
|
||||
|
|
|
@ -3,6 +3,8 @@ require 'spec_helper'
|
|||
describe 'adding an event' do
|
||||
let(:event) do
|
||||
AASM::SupportingClasses::Event.new(:close_order, {:success => :success_callback}) do
|
||||
before :before_callback
|
||||
after :after_callback
|
||||
transitions :to => :closed, :from => [:open, :received]
|
||||
end
|
||||
end
|
||||
|
@ -15,6 +17,14 @@ describe 'adding an event' do
|
|||
event.success.should == :success_callback
|
||||
end
|
||||
|
||||
it 'should set the after callback' do
|
||||
event.options[:after].should == :after_callback
|
||||
end
|
||||
|
||||
it 'should set the before callback' do
|
||||
event.options[:before].should == :before_callback
|
||||
end
|
||||
|
||||
it 'should create transitions' do
|
||||
transitions = event.all_transitions
|
||||
transitions[0].from.should == :open
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue