aasm 4.2.0 → 4.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/Gemfile +2 -2
- data/PLANNED_CHANGES.md +24 -4
- data/README.md +75 -5
- data/lib/aasm/aasm.rb +50 -36
- data/lib/aasm/base.rb +36 -18
- data/lib/aasm/core/event.rb +6 -5
- data/lib/aasm/core/state.rb +3 -2
- data/lib/aasm/core/transition.rb +5 -4
- data/lib/aasm/errors.rb +7 -4
- data/lib/aasm/instance_base.rb +14 -13
- data/lib/aasm/localizer.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +41 -66
- data/lib/aasm/persistence/base.rb +7 -7
- data/lib/aasm/persistence/mongo_mapper_persistence.rb +34 -51
- data/lib/aasm/persistence/mongoid_persistence.rb +15 -36
- data/lib/aasm/persistence/plain_persistence.rb +8 -7
- data/lib/aasm/persistence/sequel_persistence.rb +12 -10
- data/lib/aasm/state_machine.rb +11 -6
- data/lib/aasm/version.rb +1 -1
- data/spec/database.rb +27 -1
- data/spec/models/active_record/basic_active_record_two_state_machines_example.rb +25 -0
- data/spec/models/active_record/complex_active_record_example.rb +33 -0
- data/spec/models/active_record/derivate_new_dsl.rb +4 -0
- data/spec/models/active_record/false_state.rb +18 -0
- data/spec/models/active_record/gate.rb +20 -0
- data/spec/models/active_record/no_direct_assignment.rb +11 -0
- data/spec/models/active_record/no_scope.rb +11 -0
- data/spec/models/active_record/provided_and_persisted_state.rb +3 -3
- data/spec/models/active_record/simple_new_dsl.rb +9 -0
- data/spec/models/active_record/thief.rb +15 -0
- data/spec/models/active_record/with_enum.rb +20 -0
- data/spec/models/active_record/with_false_enum.rb +16 -0
- data/spec/models/active_record/with_true_enum.rb +20 -0
- data/spec/models/basic_two_state_machines_example.rb +25 -0
- data/spec/models/callbacks/basic_multiple.rb +75 -0
- data/spec/models/callbacks/guard_within_block_multiple.rb +66 -0
- data/spec/models/callbacks/multiple_transitions_transition_guard_multiple.rb +65 -0
- data/spec/models/callbacks/private_method_multiple.rb +44 -0
- data/spec/models/callbacks/with_args_multiple.rb +61 -0
- data/spec/models/callbacks/{with_state_args.rb → with_state_arg.rb} +0 -0
- data/spec/models/callbacks/with_state_arg_multiple.rb +26 -0
- data/spec/models/complex_example.rb +134 -0
- data/spec/models/conversation.rb +47 -1
- data/spec/models/foo.rb +57 -0
- data/spec/models/foo_callback_multiple.rb +45 -0
- data/spec/models/guardian_multiple.rb +48 -0
- data/spec/models/initial_state_proc.rb +16 -0
- data/spec/models/invalid_persistor.rb +15 -0
- data/spec/models/mongo_mapper/complex_mongo_mapper_example.rb +37 -0
- data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +11 -0
- data/spec/models/mongo_mapper/simple_mongo_mapper.rb +12 -0
- data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +13 -0
- data/spec/models/mongoid/complex_mongoid_example.rb +37 -0
- data/spec/models/mongoid/no_scope_mongoid.rb +11 -0
- data/spec/models/mongoid/simple_mongoid.rb +12 -0
- data/spec/models/mongoid/simple_new_dsl_mongoid.rb +13 -0
- data/spec/models/no_initial_state.rb +13 -0
- data/spec/models/parametrised_event.rb +1 -1
- data/spec/models/parametrised_event_multiple.rb +29 -0
- data/spec/models/provided_state.rb +3 -3
- data/spec/models/sequel/complex_sequel_example.rb +45 -0
- data/spec/models/sequel/sequel_multiple.rb +25 -0
- data/spec/models/sequel/sequel_simple.rb +25 -0
- data/spec/models/simple_multiple_example.rb +30 -0
- data/spec/models/sub_class.rb +4 -0
- data/spec/models/sub_class_with_more_states.rb +11 -0
- data/spec/models/super_class.rb +28 -0
- data/spec/models/transactor.rb +27 -0
- data/spec/models/valid_state_name.rb +12 -0
- data/spec/models/validator.rb +39 -0
- data/spec/unit/basic_two_state_machines_example_spec.rb +10 -0
- data/spec/unit/callback_multiple_spec.rb +295 -0
- data/spec/unit/callbacks_spec.rb +1 -1
- data/spec/unit/complex_multiple_example_spec.rb +99 -0
- data/spec/unit/edge_cases_spec.rb +16 -0
- data/spec/unit/event_multiple_spec.rb +73 -0
- data/spec/unit/event_spec.rb +11 -6
- data/spec/unit/guard_multiple_spec.rb +60 -0
- data/spec/unit/initial_state_multiple_spec.rb +15 -0
- data/spec/unit/inspection_multiple_spec.rb +201 -0
- data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +560 -0
- data/spec/unit/persistence/active_record_persistence_spec.rb +17 -12
- data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +146 -0
- data/spec/unit/persistence/{mongo_mapper_persistance_spec.rb → mongo_mapper_persistence_spec.rb} +7 -49
- data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +127 -0
- data/spec/unit/persistence/mongoid_persistence_spec.rb +79 -0
- data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +153 -0
- data/spec/unit/persistence/sequel_persistence_spec.rb +7 -24
- data/spec/unit/reloading_spec.rb +1 -1
- data/spec/unit/simple_multiple_example_spec.rb +63 -0
- data/spec/unit/state_spec.rb +3 -1
- data/spec/unit/subclassing_multiple_spec.rb +39 -0
- data/spec/unit/transition_spec.rb +31 -22
- metadata +73 -9
- data/spec/unit/persistence/mongoid_persistance_spec.rb +0 -146
@@ -0,0 +1,153 @@
|
|
1
|
+
describe 'sequel' do
|
2
|
+
begin
|
3
|
+
require 'sequel'
|
4
|
+
require 'logger'
|
5
|
+
require 'spec_helper'
|
6
|
+
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/sequel/*.rb"].sort.each do |f|
|
8
|
+
require File.expand_path(f)
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:all) do
|
12
|
+
@model = SequelMultiple
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "instance methods" do
|
16
|
+
let(:model) {@model.new}
|
17
|
+
|
18
|
+
it "should respond to aasm persistence methods" do
|
19
|
+
expect(model).to respond_to(:aasm_read_state)
|
20
|
+
expect(model).to respond_to(:aasm_write_state)
|
21
|
+
expect(model).to respond_to(:aasm_write_state_without_persistence)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return the initial state when new and the aasm field is nil" do
|
25
|
+
expect(model.aasm(:left).current_state).to eq(:alpha)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should save the initial state" do
|
29
|
+
model.save
|
30
|
+
expect(model.status).to eq("alpha")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return the aasm column when new and the aasm field is not nil" do
|
34
|
+
model.status = "beta"
|
35
|
+
expect(model.aasm(:left).current_state).to eq(:beta)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return the aasm column when not new and the aasm_column is not nil" do
|
39
|
+
allow(model).to receive(:new?).and_return(false)
|
40
|
+
model.status = "gamma"
|
41
|
+
expect(model.aasm(:left).current_state).to eq(:gamma)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should allow a nil state" do
|
45
|
+
allow(model).to receive(:new?).and_return(false)
|
46
|
+
model.status = nil
|
47
|
+
expect(model.aasm(:left).current_state).to be_nil
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should not change the state if state is not loaded" do
|
51
|
+
model.release
|
52
|
+
model.save
|
53
|
+
model.class.select(:id).first.save
|
54
|
+
model.reload
|
55
|
+
expect(model.aasm(:left).current_state).to eq(:beta)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should call aasm_ensure_initial_state on validation before create" do
|
59
|
+
expect(model).to receive(:aasm_ensure_initial_state).and_return(true)
|
60
|
+
model.valid?
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
64
|
+
expect(model).to receive(:aasm_ensure_initial_state).and_return(true)
|
65
|
+
model.save(:validate => false)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'subclasses' do
|
70
|
+
it "should have the same states as its parent class" do
|
71
|
+
expect(Class.new(@model).aasm(:left).states).to eq(@model.aasm(:left).states)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should have the same events as its parent class" do
|
75
|
+
expect(Class.new(@model).aasm(:left).events).to eq(@model.aasm(:left).events)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should have the same column as its parent even for the new dsl" do
|
79
|
+
expect(@model.aasm(:left).attribute_name).to eq(:status)
|
80
|
+
expect(Class.new(@model).aasm(:left).attribute_name).to eq(:status)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'initial states' do
|
85
|
+
it 'should support conditions' do
|
86
|
+
@model.aasm(:left) do
|
87
|
+
initial_state lambda{ |m| m.default }
|
88
|
+
end
|
89
|
+
|
90
|
+
expect(@model.new(:default => :beta).aasm(:left).current_state).to eq(:beta)
|
91
|
+
expect(@model.new(:default => :gamma).aasm(:left).current_state).to eq(:gamma)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "complex example" do
|
96
|
+
it "works" do
|
97
|
+
record = ComplexSequelExample.new
|
98
|
+
expect(record.aasm(:left).current_state).to eql :one
|
99
|
+
expect(record.left).to be_nil
|
100
|
+
expect(record.aasm(:right).current_state).to eql :alpha
|
101
|
+
expect(record.right).to be_nil
|
102
|
+
|
103
|
+
record.save
|
104
|
+
expect_aasm_states record, :one, :alpha
|
105
|
+
record.reload
|
106
|
+
expect_aasm_states record, :one, :alpha
|
107
|
+
|
108
|
+
record.increment!
|
109
|
+
expect_aasm_states record, :two, :alpha
|
110
|
+
record.reload
|
111
|
+
expect_aasm_states record, :two, :alpha
|
112
|
+
|
113
|
+
record.level_up!
|
114
|
+
expect_aasm_states record, :two, :beta
|
115
|
+
record.reload
|
116
|
+
expect_aasm_states record, :two, :beta
|
117
|
+
|
118
|
+
record.increment!
|
119
|
+
expect { record.increment! }.to raise_error(AASM::InvalidTransition)
|
120
|
+
expect_aasm_states record, :three, :beta
|
121
|
+
record.reload
|
122
|
+
expect_aasm_states record, :three, :beta
|
123
|
+
|
124
|
+
record.level_up!
|
125
|
+
expect_aasm_states record, :three, :gamma
|
126
|
+
record.reload
|
127
|
+
expect_aasm_states record, :three, :gamma
|
128
|
+
|
129
|
+
record.level_down # without saving
|
130
|
+
expect_aasm_states record, :three, :beta
|
131
|
+
record.reload
|
132
|
+
expect_aasm_states record, :three, :gamma
|
133
|
+
|
134
|
+
record.level_down # without saving
|
135
|
+
expect_aasm_states record, :three, :beta
|
136
|
+
record.reset!
|
137
|
+
expect_aasm_states record, :one, :beta
|
138
|
+
end
|
139
|
+
|
140
|
+
def expect_aasm_states(record, left_state, right_state)
|
141
|
+
expect(record.aasm(:left).current_state).to eql left_state.to_sym
|
142
|
+
expect(record.left).to eql left_state.to_s
|
143
|
+
expect(record.aasm(:right).current_state).to eql right_state.to_sym
|
144
|
+
expect(record.right).to eql right_state.to_s
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
rescue LoadError
|
149
|
+
puts "------------------------------------------------------------------------"
|
150
|
+
puts "Not running Sequel multiple-specs because sequel gem is not installed!!!"
|
151
|
+
puts "------------------------------------------------------------------------"
|
152
|
+
end
|
153
|
+
end
|
@@ -1,34 +1,15 @@
|
|
1
|
-
|
2
1
|
describe 'sequel' do
|
3
2
|
begin
|
4
3
|
require 'sequel'
|
5
4
|
require 'logger'
|
6
5
|
require 'spec_helper'
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# if you want to see the statements while running the spec enable the following line
|
12
|
-
# db.loggers << Logger.new($stderr)
|
13
|
-
db.create_table(:models) do
|
14
|
-
primary_key :id
|
15
|
-
String :status
|
16
|
-
end
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/sequel/*.rb"].sort.each do |f|
|
8
|
+
require File.expand_path(f)
|
9
|
+
end
|
17
10
|
|
18
|
-
|
19
|
-
|
20
|
-
attr_accessor :default
|
21
|
-
include AASM
|
22
|
-
aasm :column => :status
|
23
|
-
aasm do
|
24
|
-
state :alpha, :initial => true
|
25
|
-
state :beta
|
26
|
-
state :gamma
|
27
|
-
event :release do
|
28
|
-
transitions :from => [:alpha, :beta, :gamma], :to => :beta
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
11
|
+
before(:all) do
|
12
|
+
@model = SequelSimple
|
32
13
|
end
|
33
14
|
|
34
15
|
describe "instance methods" do
|
@@ -112,6 +93,8 @@ describe 'sequel' do
|
|
112
93
|
end
|
113
94
|
|
114
95
|
rescue LoadError
|
96
|
+
puts "------------------------------------------------------------------------"
|
115
97
|
puts "Not running Sequel specs because sequel gem is not installed!!!"
|
98
|
+
puts "------------------------------------------------------------------------"
|
116
99
|
end
|
117
100
|
end
|
data/spec/unit/reloading_spec.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'state machine' do
|
4
|
+
let(:simple) { SimpleMultipleExample.new }
|
5
|
+
|
6
|
+
it 'starts with an initial state' do
|
7
|
+
expect(simple.aasm(:move).current_state).to eq(:standing)
|
8
|
+
expect(simple).to respond_to(:standing?)
|
9
|
+
expect(simple).to be_standing
|
10
|
+
|
11
|
+
expect(simple.aasm(:work).current_state).to eq(:sleeping)
|
12
|
+
expect(simple).to respond_to(:sleeping?)
|
13
|
+
expect(simple).to be_sleeping
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'allows transitions to other states' do
|
17
|
+
expect(simple).to respond_to(:walk)
|
18
|
+
expect(simple).to respond_to(:walk!)
|
19
|
+
simple.walk!
|
20
|
+
expect(simple).to respond_to(:walking?)
|
21
|
+
expect(simple).to be_walking
|
22
|
+
|
23
|
+
expect(simple).to respond_to(:run)
|
24
|
+
expect(simple).to respond_to(:run!)
|
25
|
+
simple.run
|
26
|
+
expect(simple).to respond_to(:running?)
|
27
|
+
expect(simple).to be_running
|
28
|
+
|
29
|
+
expect(simple).to respond_to(:start)
|
30
|
+
expect(simple).to respond_to(:start!)
|
31
|
+
simple.start
|
32
|
+
expect(simple).to respond_to(:processing?)
|
33
|
+
expect(simple).to be_processing
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'denies transitions to other states' do
|
37
|
+
expect {simple.hold}.to raise_error(AASM::InvalidTransition)
|
38
|
+
expect {simple.hold!}.to raise_error(AASM::InvalidTransition)
|
39
|
+
simple.walk
|
40
|
+
expect {simple.walk}.to raise_error(AASM::InvalidTransition)
|
41
|
+
expect {simple.walk!}.to raise_error(AASM::InvalidTransition)
|
42
|
+
simple.run
|
43
|
+
expect {simple.walk}.to raise_error(AASM::InvalidTransition)
|
44
|
+
expect {simple.walk!}.to raise_error(AASM::InvalidTransition)
|
45
|
+
|
46
|
+
expect {simple.stop}.to raise_error(AASM::InvalidTransition)
|
47
|
+
expect {simple.stop!}.to raise_error(AASM::InvalidTransition)
|
48
|
+
simple.start
|
49
|
+
expect {simple.start}.to raise_error(AASM::InvalidTransition)
|
50
|
+
expect {simple.start!}.to raise_error(AASM::InvalidTransition)
|
51
|
+
simple.stop
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'defines constants for each state name' do
|
55
|
+
expect(SimpleMultipleExample::STATE_STANDING).to eq(:standing)
|
56
|
+
expect(SimpleMultipleExample::STATE_WALKING).to eq(:walking)
|
57
|
+
expect(SimpleMultipleExample::STATE_RUNNING).to eq(:running)
|
58
|
+
|
59
|
+
expect(SimpleMultipleExample::STATE_SLEEPING).to eq(:sleeping)
|
60
|
+
expect(SimpleMultipleExample::STATE_PROCESSING).to eq(:processing)
|
61
|
+
expect(SimpleMultipleExample::STATE_RUNNING).to eq(:running)
|
62
|
+
end
|
63
|
+
end
|
data/spec/unit/state_spec.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe AASM::Core::State do
|
4
|
+
let(:state_machine) { AASM::StateMachine.new(:name) }
|
5
|
+
|
4
6
|
before(:each) do
|
5
7
|
@name = :astate
|
6
8
|
@options = { :crazy_custom_key => 'key' }
|
7
9
|
end
|
8
10
|
|
9
11
|
def new_state(options={})
|
10
|
-
AASM::Core::State.new(@name, Conversation, @options.merge(options))
|
12
|
+
AASM::Core::State.new(@name, Conversation, state_machine, @options.merge(options))
|
11
13
|
end
|
12
14
|
|
13
15
|
it 'should set the name' do
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'subclassing with multiple state machines' do
|
4
|
+
|
5
|
+
it 'should have the parent states' do
|
6
|
+
SuperClassMultiple.aasm(:left).states.each do |state|
|
7
|
+
expect(SubClassWithMoreStatesMultiple.aasm(:left).states).to include(state)
|
8
|
+
end
|
9
|
+
expect(SubClassMultiple.aasm(:left).states).to eq(SuperClassMultiple.aasm(:left).states)
|
10
|
+
|
11
|
+
SuperClassMultiple.aasm(:right).states.each do |state|
|
12
|
+
expect(SubClassWithMoreStatesMultiple.aasm(:right).states).to include(state)
|
13
|
+
end
|
14
|
+
expect(SubClassMultiple.aasm(:right).states).to eq(SuperClassMultiple.aasm(:right).states)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should not add the child states to the parent machine' do
|
18
|
+
expect(SuperClassMultiple.aasm(:left).states).not_to include(:foo)
|
19
|
+
expect(SuperClassMultiple.aasm(:right).states).not_to include(:archived)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have the same events as its parent" do
|
23
|
+
expect(SubClassMultiple.aasm(:left).events).to eq(SuperClassMultiple.aasm(:left).events)
|
24
|
+
expect(SubClassMultiple.aasm(:right).events).to eq(SuperClassMultiple.aasm(:right).events)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should know how to respond to question methods' do
|
28
|
+
expect(SubClassMultiple.new.may_foo?).to be_truthy
|
29
|
+
expect(SubClassMultiple.new.may_close?).to be_truthy
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should not break if I call methods from super class' do
|
33
|
+
son = SubClassMultiple.new
|
34
|
+
son.update_state
|
35
|
+
expect(son.aasm(:left).current_state).to eq(:ended)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
@@ -53,13 +53,13 @@ describe 'transitions' do
|
|
53
53
|
|
54
54
|
end
|
55
55
|
|
56
|
-
describe 'blocks' do
|
57
|
-
end
|
58
|
-
|
59
56
|
describe AASM::Core::Transition do
|
57
|
+
let(:state_machine) { AASM::StateMachine.new(:name) }
|
58
|
+
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
59
|
+
|
60
60
|
it 'should set from, to, and opts attr readers' do
|
61
61
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
62
|
-
st = AASM::Core::Transition.new(opts)
|
62
|
+
st = AASM::Core::Transition.new(event, opts)
|
63
63
|
|
64
64
|
expect(st.from).to eq(opts[:from])
|
65
65
|
expect(st.to).to eq(opts[:to])
|
@@ -71,7 +71,7 @@ describe AASM::Core::Transition do
|
|
71
71
|
st = AASM::Core::Transition.allocate
|
72
72
|
expect(st).to receive(:warn).with('[DEPRECATION] :on_transition is deprecated, use :after instead')
|
73
73
|
|
74
|
-
st.send :initialize, opts do
|
74
|
+
st.send :initialize, event, opts do
|
75
75
|
guard :gg
|
76
76
|
on_transition :after_callback
|
77
77
|
end
|
@@ -81,7 +81,7 @@ describe AASM::Core::Transition do
|
|
81
81
|
|
82
82
|
it 'should set after and guard from dsl' do
|
83
83
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
84
|
-
st = AASM::Core::Transition.new(opts) do
|
84
|
+
st = AASM::Core::Transition.new(event, opts) do
|
85
85
|
guard :gg
|
86
86
|
after :after_callback
|
87
87
|
end
|
@@ -92,7 +92,7 @@ describe AASM::Core::Transition do
|
|
92
92
|
|
93
93
|
it 'should pass equality check if from and to are the same' do
|
94
94
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
95
|
-
st = AASM::Core::Transition.new(opts)
|
95
|
+
st = AASM::Core::Transition.new(event, opts)
|
96
96
|
|
97
97
|
obj = double('object')
|
98
98
|
allow(obj).to receive(:from).and_return(opts[:from])
|
@@ -103,7 +103,7 @@ describe AASM::Core::Transition do
|
|
103
103
|
|
104
104
|
it 'should fail equality check if from are not the same' do
|
105
105
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
106
|
-
st = AASM::Core::Transition.new(opts)
|
106
|
+
st = AASM::Core::Transition.new(event, opts)
|
107
107
|
|
108
108
|
obj = double('object')
|
109
109
|
allow(obj).to receive(:from).and_return('blah')
|
@@ -114,7 +114,7 @@ describe AASM::Core::Transition do
|
|
114
114
|
|
115
115
|
it 'should fail equality check if to are not the same' do
|
116
116
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
117
|
-
st = AASM::Core::Transition.new(opts)
|
117
|
+
st = AASM::Core::Transition.new(event, opts)
|
118
118
|
|
119
119
|
obj = double('object')
|
120
120
|
allow(obj).to receive(:from).and_return(opts[:from])
|
@@ -125,16 +125,19 @@ describe AASM::Core::Transition do
|
|
125
125
|
end
|
126
126
|
|
127
127
|
describe AASM::Core::Transition, '- when performing guard checks' do
|
128
|
+
let(:state_machine) { AASM::StateMachine.new(:name) }
|
129
|
+
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
130
|
+
|
128
131
|
it 'should return true of there is no guard' do
|
129
132
|
opts = {:from => 'foo', :to => 'bar'}
|
130
|
-
st = AASM::Core::Transition.new(opts)
|
133
|
+
st = AASM::Core::Transition.new(event, opts)
|
131
134
|
|
132
135
|
expect(st.allowed?(nil)).to be_truthy
|
133
136
|
end
|
134
137
|
|
135
138
|
it 'should call the method on the object if guard is a symbol' do
|
136
139
|
opts = {:from => 'foo', :to => 'bar', :guard => :test}
|
137
|
-
st = AASM::Core::Transition.new(opts)
|
140
|
+
st = AASM::Core::Transition.new(event, opts)
|
138
141
|
|
139
142
|
obj = double('object')
|
140
143
|
expect(obj).to receive(:test)
|
@@ -144,7 +147,7 @@ describe AASM::Core::Transition, '- when performing guard checks' do
|
|
144
147
|
|
145
148
|
it 'should call the method on the object if unless is a symbol' do
|
146
149
|
opts = {:from => 'foo', :to => 'bar', :unless => :test}
|
147
|
-
st = AASM::Core::Transition.new(opts)
|
150
|
+
st = AASM::Core::Transition.new(event, opts)
|
148
151
|
|
149
152
|
obj = double('object')
|
150
153
|
expect(obj).to receive(:test)
|
@@ -154,7 +157,7 @@ describe AASM::Core::Transition, '- when performing guard checks' do
|
|
154
157
|
|
155
158
|
it 'should call the method on the object if guard is a string' do
|
156
159
|
opts = {:from => 'foo', :to => 'bar', :guard => 'test'}
|
157
|
-
st = AASM::Core::Transition.new(opts)
|
160
|
+
st = AASM::Core::Transition.new(event, opts)
|
158
161
|
|
159
162
|
obj = double('object')
|
160
163
|
expect(obj).to receive(:test)
|
@@ -164,7 +167,7 @@ describe AASM::Core::Transition, '- when performing guard checks' do
|
|
164
167
|
|
165
168
|
it 'should call the method on the object if unless is a string' do
|
166
169
|
opts = {:from => 'foo', :to => 'bar', :unless => 'test'}
|
167
|
-
st = AASM::Core::Transition.new(opts)
|
170
|
+
st = AASM::Core::Transition.new(event, opts)
|
168
171
|
|
169
172
|
obj = double('object')
|
170
173
|
expect(obj).to receive(:test)
|
@@ -174,7 +177,7 @@ describe AASM::Core::Transition, '- when performing guard checks' do
|
|
174
177
|
|
175
178
|
it 'should call the proc passing the object if the guard is a proc' do
|
176
179
|
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new { test }}
|
177
|
-
st = AASM::Core::Transition.new(opts)
|
180
|
+
st = AASM::Core::Transition.new(event, opts)
|
178
181
|
|
179
182
|
obj = double('object')
|
180
183
|
expect(obj).to receive(:test)
|
@@ -184,9 +187,12 @@ describe AASM::Core::Transition, '- when performing guard checks' do
|
|
184
187
|
end
|
185
188
|
|
186
189
|
describe AASM::Core::Transition, '- when executing the transition with a Proc' do
|
190
|
+
let(:state_machine) { AASM::StateMachine.new(:name) }
|
191
|
+
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
192
|
+
|
187
193
|
it 'should call a Proc on the object with args' do
|
188
194
|
opts = {:from => 'foo', :to => 'bar', :after => Proc.new {|a| test(a) }}
|
189
|
-
st = AASM::Core::Transition.new(opts)
|
195
|
+
st = AASM::Core::Transition.new(event, opts)
|
190
196
|
args = {:arg1 => '1', :arg2 => '2'}
|
191
197
|
obj = double('object', :aasm => 'aasm')
|
192
198
|
|
@@ -202,7 +208,7 @@ describe AASM::Core::Transition, '- when executing the transition with a Proc' d
|
|
202
208
|
prc = Proc.new { prc_object = self }
|
203
209
|
|
204
210
|
opts = {:from => 'foo', :to => 'bar', :after => prc }
|
205
|
-
st = AASM::Core::Transition.new(opts)
|
211
|
+
st = AASM::Core::Transition.new(event, opts)
|
206
212
|
args = {:arg1 => '1', :arg2 => '2'}
|
207
213
|
obj = double('object', :aasm => 'aasm')
|
208
214
|
|
@@ -212,9 +218,12 @@ describe AASM::Core::Transition, '- when executing the transition with a Proc' d
|
|
212
218
|
end
|
213
219
|
|
214
220
|
describe AASM::Core::Transition, '- when executing the transition with an :after method call' do
|
221
|
+
let(:state_machine) { AASM::StateMachine.new(:name) }
|
222
|
+
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
223
|
+
|
215
224
|
it 'should accept a String for the method name' do
|
216
225
|
opts = {:from => 'foo', :to => 'bar', :after => 'test'}
|
217
|
-
st = AASM::Core::Transition.new(opts)
|
226
|
+
st = AASM::Core::Transition.new(event, opts)
|
218
227
|
args = {:arg1 => '1', :arg2 => '2'}
|
219
228
|
obj = double('object', :aasm => 'aasm')
|
220
229
|
|
@@ -225,7 +234,7 @@ describe AASM::Core::Transition, '- when executing the transition with an :after
|
|
225
234
|
|
226
235
|
it 'should accept a Symbol for the method name' do
|
227
236
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
228
|
-
st = AASM::Core::Transition.new(opts)
|
237
|
+
st = AASM::Core::Transition.new(event, opts)
|
229
238
|
args = {:arg1 => '1', :arg2 => '2'}
|
230
239
|
obj = double('object', :aasm => 'aasm')
|
231
240
|
|
@@ -236,7 +245,7 @@ describe AASM::Core::Transition, '- when executing the transition with an :after
|
|
236
245
|
|
237
246
|
it 'should pass args if the target method accepts them' do
|
238
247
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
239
|
-
st = AASM::Core::Transition.new(opts)
|
248
|
+
st = AASM::Core::Transition.new(event, opts)
|
240
249
|
args = {:arg1 => '1', :arg2 => '2'}
|
241
250
|
obj = double('object', :aasm => 'aasm')
|
242
251
|
|
@@ -251,7 +260,7 @@ describe AASM::Core::Transition, '- when executing the transition with an :after
|
|
251
260
|
|
252
261
|
it 'should NOT pass args if the target method does NOT accept them' do
|
253
262
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
254
|
-
st = AASM::Core::Transition.new(opts)
|
263
|
+
st = AASM::Core::Transition.new(event, opts)
|
255
264
|
args = {:arg1 => '1', :arg2 => '2'}
|
256
265
|
obj = double('object', :aasm => 'aasm')
|
257
266
|
|
@@ -266,7 +275,7 @@ describe AASM::Core::Transition, '- when executing the transition with an :after
|
|
266
275
|
|
267
276
|
it 'should allow accessing the from_state and the to_state' do
|
268
277
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
269
|
-
transition = AASM::Core::Transition.new(opts)
|
278
|
+
transition = AASM::Core::Transition.new(event, opts)
|
270
279
|
args = {:arg1 => '1', :arg2 => '2'}
|
271
280
|
obj = double('object', :aasm => AASM::InstanceBase.new('object'))
|
272
281
|
|