mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
parent
fe63fc03f9
commit
8f7f1b2da8
11 changed files with 67 additions and 65 deletions
|
@ -5,6 +5,10 @@
|
|||
* `aasm_column` has been removed. Use `aasm.attribute_name` instead
|
||||
* `aasm_human_event_name` has been removed. Use `aasm.human_event_name` instead
|
||||
|
||||
## 4.0.4 (not yet released)
|
||||
|
||||
* bugfix: avoid Rails autoloading conflicts (see [issue #137](https://github.com/aasm/aasm/issues/137) and see [issue #139](https://github.com/aasm/aasm/issues/139) for details)
|
||||
|
||||
## 4.0.3
|
||||
|
||||
* bugfix: fire guards only once per transition, part 2 (see [issue #187](https://github.com/aasm/aasm/issues/187) for details)
|
||||
|
|
28
lib/aasm.rb
28
lib/aasm.rb
|
@ -1,17 +1,15 @@
|
|||
require 'ostruct'
|
||||
|
||||
%w(
|
||||
version
|
||||
errors
|
||||
configuration
|
||||
base
|
||||
dsl_helper
|
||||
instance_base
|
||||
transition
|
||||
event
|
||||
state
|
||||
localizer
|
||||
state_machine
|
||||
persistence
|
||||
aasm
|
||||
).each { |file| require File.join(File.dirname(__FILE__), 'aasm', file) }
|
||||
require 'aasm/version'
|
||||
require 'aasm/errors'
|
||||
require 'aasm/configuration'
|
||||
require 'aasm/base'
|
||||
require 'aasm/dsl_helper'
|
||||
require 'aasm/instance_base'
|
||||
require 'aasm/core/transition'
|
||||
require 'aasm/core/event'
|
||||
require 'aasm/core/state'
|
||||
require 'aasm/localizer'
|
||||
require 'aasm/state_machine'
|
||||
require 'aasm/persistence'
|
||||
require 'aasm/aasm'
|
||||
|
|
|
@ -67,7 +67,7 @@ module AASM
|
|||
|
||||
# define an event
|
||||
def event(name, options={}, &block)
|
||||
@state_machine.events[name] = AASM::Event.new(name, options, &block)
|
||||
@state_machine.events[name] = AASM::Core::Event.new(name, options, &block)
|
||||
|
||||
# an addition over standard aasm so that, before firing an event, you can ask
|
||||
# may_event? and get back a boolean that tells you whether the guard method
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module AASM
|
||||
module AASM::Core
|
||||
class Event
|
||||
include DslHelper
|
||||
|
||||
|
@ -61,11 +61,11 @@ module AASM
|
|||
if definitions # define new transitions
|
||||
# Create a separate transition for each from-state to the given state
|
||||
Array(definitions[:from]).each do |s|
|
||||
@transitions << AASM::Transition.new(attach_event_guards(definitions.merge(:from => s.to_sym)), &block)
|
||||
@transitions << AASM::Core::Transition.new(attach_event_guards(definitions.merge(:from => s.to_sym)), &block)
|
||||
end
|
||||
# Create a transition if :to is specified without :from (transitions from ANY state)
|
||||
if @transitions.empty? && definitions[:to]
|
||||
@transitions << AASM::Transition.new(attach_event_guards(definitions), &block)
|
||||
@transitions << AASM::Core::Transition.new(attach_event_guards(definitions), &block)
|
||||
end
|
||||
end
|
||||
@transitions
|
|
@ -1,4 +1,4 @@
|
|||
module AASM
|
||||
module AASM::Core
|
||||
class State
|
||||
attr_reader :name, :options
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
module AASM
|
||||
module AASM::Core
|
||||
class Transition
|
||||
include DslHelper
|
||||
|
|
@ -32,7 +32,7 @@ module AASM
|
|||
# allow reloading, extending or redefining a state
|
||||
@states.delete(name) if @states.include?(name)
|
||||
|
||||
@states << AASM::State.new(name, klass, options)
|
||||
@states << AASM::Core::State.new(name, klass, options)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
|
||||
describe 'adding an event' do
|
||||
let(:event) do
|
||||
AASM::Event.new(:close_order, {:success => :success_callback}) do
|
||||
AASM::Core::Event.new(:close_order, {:success => :success_callback}) do
|
||||
before :before_callback
|
||||
after :after_callback
|
||||
transitions :to => :closed, :from => [:open, :received]
|
||||
|
@ -36,7 +36,7 @@ end
|
|||
|
||||
describe 'transition inspection' do
|
||||
let(:event) do
|
||||
AASM::Event.new(:run) do
|
||||
AASM::Core::Event.new(:run) do
|
||||
transitions :to => :running, :from => :sleeping
|
||||
end
|
||||
end
|
||||
|
@ -60,7 +60,7 @@ end
|
|||
|
||||
describe 'transition inspection without from' do
|
||||
let(:event) do
|
||||
AASM::Event.new(:run) do
|
||||
AASM::Core::Event.new(:run) do
|
||||
transitions :to => :running
|
||||
end
|
||||
end
|
||||
|
@ -79,12 +79,12 @@ describe 'firing an event' do
|
|||
it 'should return nil if the transitions are empty' do
|
||||
obj = double('object', :aasm => double('aasm', :current_state => 'open'))
|
||||
|
||||
event = AASM::Event.new(:event)
|
||||
event = AASM::Core::Event.new(:event)
|
||||
expect(event.fire(obj)).to be_nil
|
||||
end
|
||||
|
||||
it 'should return the state of the first matching transition it finds' do
|
||||
event = AASM::Event.new(:event) do
|
||||
event = AASM::Core::Event.new(:event) do
|
||||
transitions :to => :closed, :from => [:open, :received]
|
||||
end
|
||||
|
||||
|
@ -94,7 +94,7 @@ describe 'firing an event' do
|
|||
end
|
||||
|
||||
it 'should call the guard with the params passed in' do
|
||||
event = AASM::Event.new(:event) do
|
||||
event = AASM::Core::Event.new(:event) do
|
||||
transitions :to => :closed, :from => [:open, :received], :guard => :guard_fn
|
||||
end
|
||||
|
||||
|
|
|
@ -12,27 +12,27 @@
|
|||
|
||||
# it "should be created without memory leak" do
|
||||
# machines_count = machines.size
|
||||
# state_count = number_of_objects(AASM::State)
|
||||
# event_count = number_of_objects(AASM::Event)
|
||||
# state_count = number_of_objects(AASM::Core::State)
|
||||
# event_count = number_of_objects(AASM::Core::Event)
|
||||
# puts "event_count = #{event_count}"
|
||||
# transition_count = number_of_objects(AASM::Transition)
|
||||
# transition_count = number_of_objects(AASM::Core::Transition)
|
||||
|
||||
# load File.expand_path(File.dirname(__FILE__) + '/../models/not_auto_loaded/process.rb')
|
||||
# machines.size.should == machines_count + 1 # + Process
|
||||
# number_of_objects(Models::Process).should == 0
|
||||
# number_of_objects(AASM::State).should == state_count + 3 # + Process
|
||||
# puts "event_count = #{number_of_objects(AASM::Event)}"
|
||||
# number_of_objects(AASM::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::Transition).should == transition_count + 2 # + Process
|
||||
# number_of_objects(AASM::Core::State).should == state_count + 3 # + Process
|
||||
# puts "event_count = #{number_of_objects(AASM::Core::Event)}"
|
||||
# number_of_objects(AASM::Core::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::Core::Transition).should == transition_count + 2 # + Process
|
||||
|
||||
# Models.send(:remove_const, "Process") if Models.const_defined?("Process")
|
||||
# load File.expand_path(File.dirname(__FILE__) + '/../models/not_auto_loaded/process.rb')
|
||||
# machines.size.should == machines_count + 1 # + Process
|
||||
# number_of_objects(AASM::State).should == state_count + 3 # + Process
|
||||
# # ObjectSpace.each_object(AASM::Event) {|o| puts o.inspect}
|
||||
# puts "event_count = #{number_of_objects(AASM::Event)}"
|
||||
# number_of_objects(AASM::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::Transition).should == transition_count + 2 # + Process
|
||||
# number_of_objects(AASM::Core::State).should == state_count + 3 # + Process
|
||||
# # ObjectSpace.each_object(AASM::Core::Event) {|o| puts o.inspect}
|
||||
# puts "event_count = #{number_of_objects(AASM::Core::Event)}"
|
||||
# number_of_objects(AASM::Core::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::Core::Transition).should == transition_count + 2 # + Process
|
||||
# end
|
||||
|
||||
# end
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe AASM::State do
|
||||
describe AASM::Core::State do
|
||||
before(:each) do
|
||||
@name = :astate
|
||||
@options = { :crazy_custom_key => 'key' }
|
||||
end
|
||||
|
||||
def new_state(options={})
|
||||
AASM::State.new(@name, Conversation, @options.merge(options))
|
||||
AASM::Core::State.new(@name, Conversation, @options.merge(options))
|
||||
end
|
||||
|
||||
it 'should set the name' do
|
||||
|
|
|
@ -51,10 +51,10 @@ end
|
|||
describe 'blocks' do
|
||||
end
|
||||
|
||||
describe AASM::Transition do
|
||||
describe AASM::Core::Transition do
|
||||
it 'should set from, to, and opts attr readers' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
expect(st.from).to eq(opts[:from])
|
||||
expect(st.to).to eq(opts[:to])
|
||||
|
@ -63,7 +63,7 @@ describe AASM::Transition do
|
|||
|
||||
it 'should set on_transition with deprecation warning' do
|
||||
opts = {:from => 'foo', :to => 'bar'}
|
||||
st = AASM::Transition.allocate
|
||||
st = AASM::Core::Transition.allocate
|
||||
st.should_receive(:warn).with('[DEPRECATION] :on_transition is deprecated, use :after instead')
|
||||
|
||||
st.send :initialize, opts do
|
||||
|
@ -76,7 +76,7 @@ describe AASM::Transition do
|
|||
|
||||
it 'should set after and guard from dsl' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::Transition.new(opts) do
|
||||
st = AASM::Core::Transition.new(opts) do
|
||||
guard :gg
|
||||
after :after_callback
|
||||
end
|
||||
|
@ -87,7 +87,7 @@ describe AASM::Transition do
|
|||
|
||||
it 'should pass equality check if from and to are the same' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
allow(obj).to receive(:from).and_return(opts[:from])
|
||||
|
@ -98,7 +98,7 @@ describe AASM::Transition do
|
|||
|
||||
it 'should fail equality check if from are not the same' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
allow(obj).to receive(:from).and_return('blah')
|
||||
|
@ -109,7 +109,7 @@ describe AASM::Transition do
|
|||
|
||||
it 'should fail equality check if to are not the same' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
allow(obj).to receive(:from).and_return(opts[:from])
|
||||
|
@ -119,17 +119,17 @@ describe AASM::Transition do
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::Transition, '- when performing guard checks' do
|
||||
describe AASM::Core::Transition, '- when performing guard checks' do
|
||||
it 'should return true of there is no guard' do
|
||||
opts = {:from => 'foo', :to => 'bar'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
expect(st.allowed?(nil)).to be_true
|
||||
end
|
||||
|
||||
it 'should call the method on the object if guard is a symbol' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => :test}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
expect(obj).to receive(:test)
|
||||
|
@ -139,7 +139,7 @@ describe AASM::Transition, '- when performing guard checks' do
|
|||
|
||||
it 'should call the method on the object if unless is a symbol' do
|
||||
opts = {:from => 'foo', :to => 'bar', :unless => :test}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
expect(obj).to receive(:test)
|
||||
|
@ -149,7 +149,7 @@ describe AASM::Transition, '- when performing guard checks' do
|
|||
|
||||
it 'should call the method on the object if guard is a string' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'test'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
expect(obj).to receive(:test)
|
||||
|
@ -159,7 +159,7 @@ describe AASM::Transition, '- when performing guard checks' do
|
|||
|
||||
it 'should call the method on the object if unless is a string' do
|
||||
opts = {:from => 'foo', :to => 'bar', :unless => 'test'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
expect(obj).to receive(:test)
|
||||
|
@ -169,7 +169,7 @@ describe AASM::Transition, '- when performing guard checks' do
|
|||
|
||||
it 'should call the proc passing the object if the guard is a proc' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new { test }}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
|
||||
obj = double('object')
|
||||
expect(obj).to receive(:test)
|
||||
|
@ -178,10 +178,10 @@ describe AASM::Transition, '- when performing guard checks' do
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::Transition, '- when executing the transition with a Proc' do
|
||||
describe AASM::Core::Transition, '- when executing the transition with a Proc' do
|
||||
it 'should call a Proc on the object with args' do
|
||||
opts = {:from => 'foo', :to => 'bar', :after => Proc.new {|a| test(a) }}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
|
@ -197,7 +197,7 @@ describe AASM::Transition, '- when executing the transition with a Proc' do
|
|||
prc = Proc.new { prc_object = self }
|
||||
|
||||
opts = {:from => 'foo', :to => 'bar', :after => prc }
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
|
@ -206,10 +206,10 @@ describe AASM::Transition, '- when executing the transition with a Proc' do
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::Transition, '- when executing the transition with an :after method call' do
|
||||
describe AASM::Core::Transition, '- when executing the transition with an :after method call' do
|
||||
it 'should accept a String for the method name' do
|
||||
opts = {:from => 'foo', :to => 'bar', :after => 'test'}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
|
@ -220,7 +220,7 @@ describe AASM::Transition, '- when executing the transition with an :after metho
|
|||
|
||||
it 'should accept a Symbol for the method name' do
|
||||
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
|
@ -231,7 +231,7 @@ describe AASM::Transition, '- when executing the transition with an :after metho
|
|||
|
||||
it 'should pass args if the target method accepts them' do
|
||||
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
|
@ -246,7 +246,7 @@ describe AASM::Transition, '- when executing the transition with an :after metho
|
|||
|
||||
it 'should NOT pass args if the target method does NOT accept them' do
|
||||
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
||||
st = AASM::Transition.new(opts)
|
||||
st = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => 'aasm')
|
||||
|
||||
|
@ -261,7 +261,7 @@ describe AASM::Transition, '- when executing the transition with an :after metho
|
|||
|
||||
it 'should allow accessing the from_state and the to_state' do
|
||||
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
||||
transition = AASM::Transition.new(opts)
|
||||
transition = AASM::Core::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = double('object', :aasm => AASM::InstanceBase.new('object'))
|
||||
|
||||
|
|
Loading…
Reference in a new issue