aasm 3.0.24 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.travis.yml +23 -4
- data/CHANGELOG.md +20 -0
- data/Gemfile +9 -1
- data/LICENSE +1 -1
- data/README.md +65 -12
- data/aasm.gemspec +5 -6
- data/gemfiles/rails_3.2.gemfile +12 -0
- data/gemfiles/rails_4.0.gemfile +11 -0
- data/gemfiles/rails_4.1.gemfile +11 -0
- data/lib/aasm/aasm.rb +31 -27
- data/lib/aasm/base.rb +35 -20
- data/lib/aasm/event.rb +27 -16
- data/lib/aasm/instance_base.rb +3 -1
- data/lib/aasm/persistence/active_record_persistence.rb +27 -9
- data/lib/aasm/persistence/base.rb +1 -1
- data/lib/aasm/persistence/mongoid_persistence.rb +10 -8
- data/lib/aasm/state.rb +1 -0
- data/lib/aasm/transition.rb +13 -6
- data/lib/aasm/version.rb +1 -1
- data/lib/aasm.rb +0 -3
- data/spec/database.rb +33 -0
- data/spec/models/guardian.rb +48 -0
- data/spec/models/mongoid/no_scope_mongoid.rb +1 -1
- data/spec/models/mongoid/simple_mongoid.rb +5 -4
- data/spec/models/mongoid/simple_new_dsl_mongoid.rb +1 -1
- data/spec/models/not_auto_loaded/process.rb +10 -8
- data/spec/models/persistence.rb +5 -13
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/api_spec.rb +12 -12
- data/spec/unit/callbacks_spec.rb +29 -45
- data/spec/unit/complex_example_spec.rb +15 -15
- data/spec/unit/event_spec.rb +89 -76
- data/spec/unit/guard_spec.rb +60 -0
- data/spec/unit/initial_state_spec.rb +4 -5
- data/spec/unit/inspection_spec.rb +40 -53
- data/spec/unit/localizer_spec.rb +22 -18
- data/spec/unit/new_dsl_spec.rb +2 -2
- data/spec/unit/persistence/active_record_persistence_spec.rb +111 -89
- data/spec/unit/persistence/mongoid_persistance_spec.rb +102 -81
- data/spec/unit/simple_example_spec.rb +20 -21
- data/spec/unit/state_spec.rb +16 -16
- data/spec/unit/subclassing_spec.rb +8 -8
- data/spec/unit/transition_spec.rb +59 -44
- metadata +28 -94
- data/lib/aasm/deprecated/aasm.rb +0 -15
- data/spec/models/callback_old_dsl.rb +0 -41
- data/spec/schema.rb +0 -35
|
@@ -36,6 +36,12 @@ module AASM
|
|
|
36
36
|
else
|
|
37
37
|
base.before_validation_on_create(:aasm_ensure_initial_state)
|
|
38
38
|
end
|
|
39
|
+
|
|
40
|
+
# ensure initial aasm state even when validations are skipped
|
|
41
|
+
base.before_create(:aasm_ensure_initial_state)
|
|
42
|
+
|
|
43
|
+
# ensure state is in the list of states
|
|
44
|
+
base.validate :aasm_validate_states
|
|
39
45
|
end
|
|
40
46
|
|
|
41
47
|
module ClassMethods
|
|
@@ -71,10 +77,10 @@ module AASM
|
|
|
71
77
|
# Writes <tt>state</tt> to the state column and persists it to the database
|
|
72
78
|
#
|
|
73
79
|
# foo = Foo.find(1)
|
|
74
|
-
# foo.
|
|
80
|
+
# foo.aasm.current_state # => :opened
|
|
75
81
|
# foo.close!
|
|
76
|
-
# foo.
|
|
77
|
-
# Foo.find(1).
|
|
82
|
+
# foo.aasm.current_state # => :closed
|
|
83
|
+
# Foo.find(1).aasm.current_state # => :closed
|
|
78
84
|
#
|
|
79
85
|
# NOTE: intended to be called from an event
|
|
80
86
|
def aasm_write_state(state)
|
|
@@ -97,13 +103,13 @@ module AASM
|
|
|
97
103
|
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
|
98
104
|
#
|
|
99
105
|
# foo = Foo.find(1)
|
|
100
|
-
# foo.
|
|
106
|
+
# foo.aasm.current_state # => :opened
|
|
101
107
|
# foo.close
|
|
102
|
-
# foo.
|
|
103
|
-
# Foo.find(1).
|
|
108
|
+
# foo.aasm.current_state # => :closed
|
|
109
|
+
# Foo.find(1).aasm.current_state # => :opened
|
|
104
110
|
# foo.save
|
|
105
|
-
# foo.
|
|
106
|
-
# Foo.find(1).
|
|
111
|
+
# foo.aasm.current_state # => :closed
|
|
112
|
+
# Foo.find(1).aasm.current_state # => :closed
|
|
107
113
|
#
|
|
108
114
|
# NOTE: intended to be called from an event
|
|
109
115
|
def aasm_write_state_without_persistence(state)
|
|
@@ -132,7 +138,7 @@ module AASM
|
|
|
132
138
|
end
|
|
133
139
|
|
|
134
140
|
def aasm_fire_event(name, options, *args, &block)
|
|
135
|
-
success = self.class.transaction(:requires_new =>
|
|
141
|
+
success = self.class.transaction(:requires_new => requires_new?) do
|
|
136
142
|
super
|
|
137
143
|
end
|
|
138
144
|
|
|
@@ -143,6 +149,18 @@ module AASM
|
|
|
143
149
|
|
|
144
150
|
success
|
|
145
151
|
end
|
|
152
|
+
|
|
153
|
+
def requires_new?
|
|
154
|
+
AASM::StateMachine[self.class].config.requires_new_transaction
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def aasm_validate_states
|
|
158
|
+
unless AASM::StateMachine[self.class].config.skip_validation_on_save
|
|
159
|
+
if aasm.current_state && !aasm.states.include?(aasm.current_state)
|
|
160
|
+
self.errors.add(AASM::StateMachine[self.class].config.column , "is invalid")
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
146
164
|
end # InstanceMethods
|
|
147
165
|
|
|
148
166
|
end
|
|
@@ -35,7 +35,7 @@ module AASM
|
|
|
35
35
|
def aasm_read_state
|
|
36
36
|
state = send(self.class.aasm_column)
|
|
37
37
|
if new_record?
|
|
38
|
-
state.blank? ? aasm.determine_state_name(self.class.
|
|
38
|
+
state.blank? ? aasm.determine_state_name(self.class.aasm.initial_state) : state.to_sym
|
|
39
39
|
else
|
|
40
40
|
state.nil? ? nil : state.to_sym
|
|
41
41
|
end
|
|
@@ -36,6 +36,8 @@ module AASM
|
|
|
36
36
|
# Mongoid's Validatable gem dependency goes not have a before_validation_on_xxx hook yet.
|
|
37
37
|
# base.before_validation_on_create :aasm_ensure_initial_state
|
|
38
38
|
base.before_validation :aasm_ensure_initial_state
|
|
39
|
+
# ensure initial aasm state even when validations are skipped
|
|
40
|
+
base.before_create :aasm_ensure_initial_state
|
|
39
41
|
end
|
|
40
42
|
|
|
41
43
|
module ClassMethods
|
|
@@ -66,10 +68,10 @@ module AASM
|
|
|
66
68
|
# using update_attribute (which bypasses validation)
|
|
67
69
|
#
|
|
68
70
|
# foo = Foo.find(1)
|
|
69
|
-
# foo.
|
|
71
|
+
# foo.aasm.current_state # => :opened
|
|
70
72
|
# foo.close!
|
|
71
|
-
# foo.
|
|
72
|
-
# Foo.find(1).
|
|
73
|
+
# foo.aasm.current_state # => :closed
|
|
74
|
+
# Foo.find(1).aasm.current_state # => :closed
|
|
73
75
|
#
|
|
74
76
|
# NOTE: intended to be called from an event
|
|
75
77
|
def aasm_write_state(state)
|
|
@@ -87,13 +89,13 @@ module AASM
|
|
|
87
89
|
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
|
88
90
|
#
|
|
89
91
|
# foo = Foo.find(1)
|
|
90
|
-
# foo.
|
|
92
|
+
# foo.aasm.current_state # => :opened
|
|
91
93
|
# foo.close
|
|
92
|
-
# foo.
|
|
93
|
-
# Foo.find(1).
|
|
94
|
+
# foo.aasm.current_state # => :closed
|
|
95
|
+
# Foo.find(1).aasm.current_state # => :opened
|
|
94
96
|
# foo.save
|
|
95
|
-
# foo.
|
|
96
|
-
# Foo.find(1).
|
|
97
|
+
# foo.aasm.current_state # => :closed
|
|
98
|
+
# Foo.find(1).aasm.current_state # => :closed
|
|
97
99
|
#
|
|
98
100
|
# NOTE: intended to be called from an event
|
|
99
101
|
def aasm_write_state_without_persistence(state)
|
data/lib/aasm/state.rb
CHANGED
data/lib/aasm/transition.rb
CHANGED
|
@@ -4,20 +4,24 @@ module AASM
|
|
|
4
4
|
alias_method :options, :opts
|
|
5
5
|
|
|
6
6
|
def initialize(opts)
|
|
7
|
-
@from
|
|
7
|
+
@from = opts[:from]
|
|
8
|
+
@to = opts[:to]
|
|
9
|
+
@guards = Array(opts[:guard] || opts[:guards])
|
|
10
|
+
@on_transition = opts[:on_transition]
|
|
8
11
|
@opts = opts
|
|
9
12
|
end
|
|
10
13
|
|
|
11
14
|
# TODO: should be named allowed? or similar
|
|
12
15
|
def perform(obj, *args)
|
|
13
|
-
|
|
16
|
+
@guards.each do |guard|
|
|
17
|
+
case guard
|
|
14
18
|
when Symbol, String
|
|
15
|
-
obj.send(
|
|
19
|
+
return false unless obj.send(guard, *args)
|
|
16
20
|
when Proc
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
true
|
|
21
|
+
return false unless guard.call(obj, *args)
|
|
22
|
+
end
|
|
20
23
|
end
|
|
24
|
+
true
|
|
21
25
|
end
|
|
22
26
|
|
|
23
27
|
def execute(obj, *args)
|
|
@@ -37,6 +41,9 @@ module AASM
|
|
|
37
41
|
private
|
|
38
42
|
|
|
39
43
|
def _execute(obj, on_transition, *args)
|
|
44
|
+
obj.aasm.from_state = @from if obj.aasm.respond_to?(:from_state=)
|
|
45
|
+
obj.aasm.to_state = @to if obj.aasm.respond_to?(:to_state=)
|
|
46
|
+
|
|
40
47
|
case on_transition
|
|
41
48
|
when Proc
|
|
42
49
|
on_transition.arity == 0 ? on_transition.call : on_transition.call(obj, *args)
|
data/lib/aasm/version.rb
CHANGED
data/lib/aasm.rb
CHANGED
|
@@ -13,6 +13,3 @@ require 'ostruct'
|
|
|
13
13
|
persistence
|
|
14
14
|
aasm
|
|
15
15
|
).each { |file| require File.join(File.dirname(__FILE__), 'aasm', file) }
|
|
16
|
-
|
|
17
|
-
# load the deprecated methods and modules
|
|
18
|
-
Dir[File.join(File.dirname(__FILE__), 'aasm', 'deprecated', '*.rb')].sort.each { |f| require File.expand_path(f) }
|
data/spec/database.rb
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
ActiveRecord::Migration.suppress_messages do
|
|
2
|
+
%w{gates readers writers transients simples simple_new_dsls no_scopes thieves localizer_test_models persisted_states provided_and_persisted_states}.each do |table_name|
|
|
3
|
+
ActiveRecord::Migration.create_table table_name, :force => true do |t|
|
|
4
|
+
t.string "aasm_state"
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
ActiveRecord::Migration.create_table "validators", :force => true do |t|
|
|
9
|
+
t.string "name"
|
|
10
|
+
t.string "status"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
ActiveRecord::Migration.create_table "transactors", :force => true do |t|
|
|
14
|
+
t.string "name"
|
|
15
|
+
t.string "status"
|
|
16
|
+
t.integer "worker_id"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
ActiveRecord::Migration.create_table "workers", :force => true do |t|
|
|
20
|
+
t.string "name"
|
|
21
|
+
t.string "status"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
ActiveRecord::Migration.create_table "invalid_persistors", :force => true do |t|
|
|
25
|
+
t.string "name"
|
|
26
|
+
t.string "status"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
ActiveRecord::Migration.create_table "fathers", :force => true do |t|
|
|
30
|
+
t.string "aasm_state"
|
|
31
|
+
t.string "type"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class Guardian
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
aasm do
|
|
5
|
+
state :alpha, :initial => true
|
|
6
|
+
state :beta
|
|
7
|
+
|
|
8
|
+
event :use_one_guard_that_succeeds do
|
|
9
|
+
transitions :from => :alpha, :to => :beta, :guard => :succeed
|
|
10
|
+
end
|
|
11
|
+
event :use_one_guard_that_fails do
|
|
12
|
+
transitions :from => :alpha, :to => :beta, :guard => :fail
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
event :use_guards_that_succeed do
|
|
16
|
+
transitions :from => :alpha, :to => :beta, :guards => [:succeed, :another_succeed]
|
|
17
|
+
end
|
|
18
|
+
event :use_guards_where_the_first_fails do
|
|
19
|
+
transitions :from => :alpha, :to => :beta, :guards => [:succeed, :fail]
|
|
20
|
+
end
|
|
21
|
+
event :use_guards_where_the_second_fails do
|
|
22
|
+
transitions :from => :alpha, :to => :beta, :guards => [:fail, :succeed]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
event :use_event_guards_that_succeed, :guards => [:succeed, :another_succeed] do
|
|
26
|
+
transitions :from => :alpha, :to => :beta
|
|
27
|
+
end
|
|
28
|
+
event :use_event_and_transition_guards_that_succeed, :guards => [:succeed, :another_succeed] do
|
|
29
|
+
transitions :from => :alpha, :to => :beta, :guards => [:more_succeed]
|
|
30
|
+
end
|
|
31
|
+
event :use_event_guards_where_the_first_fails, :guards => [:succeed, :fail] do
|
|
32
|
+
transitions :from => :alpha, :to => :beta
|
|
33
|
+
end
|
|
34
|
+
event :use_event_guards_where_the_second_fails, :guards => [:fail, :succeed] do
|
|
35
|
+
transitions :from => :alpha, :to => :beta, :guard => :another_succeed
|
|
36
|
+
end
|
|
37
|
+
event :use_event_and_transition_guards_where_third_fails, :guards => [:succeed, :another_succeed] do
|
|
38
|
+
transitions :from => :alpha, :to => :beta, :guards => [:fail]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def fail; false; end
|
|
43
|
+
|
|
44
|
+
def succeed; true; end
|
|
45
|
+
def another_succeed; true; end
|
|
46
|
+
def more_succeed; true; end
|
|
47
|
+
|
|
48
|
+
end
|
|
@@ -2,9 +2,10 @@ class SimpleMongoid
|
|
|
2
2
|
include Mongoid::Document
|
|
3
3
|
include AASM
|
|
4
4
|
|
|
5
|
-
field :status, type
|
|
5
|
+
field :status, :type => String
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
aasm column: :status do
|
|
8
|
+
state :unknown_scope
|
|
9
|
+
state :new
|
|
10
|
+
end
|
|
10
11
|
end
|
|
@@ -2,16 +2,18 @@ module Models
|
|
|
2
2
|
class Process
|
|
3
3
|
include AASM
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
aasm do
|
|
6
|
+
state :sleeping
|
|
7
|
+
state :running
|
|
8
|
+
state :suspended
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
event :start do
|
|
11
|
+
transitions :from => :sleeping, :to => :running
|
|
12
|
+
end
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
event :stop do
|
|
15
|
+
transitions :from => :running, :to => :suspended
|
|
16
|
+
end
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
end
|
data/spec/models/persistence.rb
CHANGED
|
@@ -36,13 +36,6 @@ class Transient < ActiveRecord::Base
|
|
|
36
36
|
include AASM
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
class Simple < ActiveRecord::Base
|
|
40
|
-
include AASM
|
|
41
|
-
aasm_column :status
|
|
42
|
-
aasm_state :unknown_scope
|
|
43
|
-
aasm_state :new
|
|
44
|
-
end
|
|
45
|
-
|
|
46
39
|
class SimpleNewDsl < ActiveRecord::Base
|
|
47
40
|
include AASM
|
|
48
41
|
aasm :column => :status
|
|
@@ -59,9 +52,6 @@ class NoScope < ActiveRecord::Base
|
|
|
59
52
|
end
|
|
60
53
|
end
|
|
61
54
|
|
|
62
|
-
class Derivate < Simple
|
|
63
|
-
end
|
|
64
|
-
|
|
65
55
|
class DerivateNewDsl < SimpleNewDsl
|
|
66
56
|
end
|
|
67
57
|
|
|
@@ -72,8 +62,10 @@ class Thief < ActiveRecord::Base
|
|
|
72
62
|
set_table_name "thieves"
|
|
73
63
|
end
|
|
74
64
|
include AASM
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
65
|
+
aasm do
|
|
66
|
+
state :rich
|
|
67
|
+
state :jailed
|
|
68
|
+
initial_state Proc.new {|thief| thief.skilled ? :rich : :jailed }
|
|
69
|
+
end
|
|
78
70
|
attr_accessor :skilled, :aasm_state
|
|
79
71
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -16,7 +16,7 @@ def load_schema
|
|
|
16
16
|
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
|
17
17
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
|
18
18
|
ActiveRecord::Base.establish_connection(config['sqlite3'])
|
|
19
|
-
|
|
19
|
+
require File.dirname(__FILE__) + "/database.rb"
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# custom spec helpers
|
data/spec/unit/api_spec.rb
CHANGED
|
@@ -3,19 +3,19 @@ require 'models/active_record/api.rb'
|
|
|
3
3
|
|
|
4
4
|
describe "reading the current state" do
|
|
5
5
|
it "uses the AASM default" do
|
|
6
|
-
DefaultState.new.aasm.current_state.
|
|
6
|
+
expect(DefaultState.new.aasm.current_state).to eql :alpha
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
it "uses the provided method" do
|
|
10
|
-
ProvidedState.new.aasm.current_state.
|
|
10
|
+
expect(ProvidedState.new.aasm.current_state).to eql :beta
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
it "uses the persistence storage" do
|
|
14
|
-
PersistedState.new.aasm.current_state.
|
|
14
|
+
expect(PersistedState.new.aasm.current_state).to eql :alpha
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it "uses the provided method even if persisted" do
|
|
18
|
-
ProvidedAndPersistedState.new.aasm.current_state.
|
|
18
|
+
expect(ProvidedAndPersistedState.new.aasm.current_state).to eql :gamma
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
@@ -23,25 +23,25 @@ describe "writing and persisting the current state" do
|
|
|
23
23
|
it "uses the AASM default" do
|
|
24
24
|
o = DefaultState.new
|
|
25
25
|
o.release!
|
|
26
|
-
o.persisted_store.
|
|
26
|
+
expect(o.persisted_store).to be_nil
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
it "uses the provided method" do
|
|
30
30
|
o = ProvidedState.new
|
|
31
31
|
o.release!
|
|
32
|
-
o.persisted_store.
|
|
32
|
+
expect(o.persisted_store).to eql :beta
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "uses the persistence storage" do
|
|
36
36
|
o = PersistedState.new
|
|
37
37
|
o.release!
|
|
38
|
-
o.persisted_store.
|
|
38
|
+
expect(o.persisted_store).to be_nil
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "uses the provided method even if persisted" do
|
|
42
42
|
o = ProvidedAndPersistedState.new
|
|
43
43
|
o.release!
|
|
44
|
-
o.persisted_store.
|
|
44
|
+
expect(o.persisted_store).to eql :beta
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
47
|
|
|
@@ -49,24 +49,24 @@ describe "writing the current state without persisting it" do
|
|
|
49
49
|
it "uses the AASM default" do
|
|
50
50
|
o = DefaultState.new
|
|
51
51
|
o.release
|
|
52
|
-
o.transient_store.
|
|
52
|
+
expect(o.transient_store).to be_nil
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
it "uses the provided method" do
|
|
56
56
|
o = ProvidedState.new
|
|
57
57
|
o.release
|
|
58
|
-
o.transient_store.
|
|
58
|
+
expect(o.transient_store).to eql :beta
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
it "uses the persistence storage" do
|
|
62
62
|
o = PersistedState.new
|
|
63
63
|
o.release
|
|
64
|
-
o.transient_store.
|
|
64
|
+
expect(o.transient_store).to be_nil
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
it "uses the provided method even if persisted" do
|
|
68
68
|
o = ProvidedAndPersistedState.new
|
|
69
69
|
o.release
|
|
70
|
-
o.transient_store.
|
|
70
|
+
expect(o.transient_store).to eql :beta
|
|
71
71
|
end
|
|
72
72
|
end
|
data/spec/unit/callbacks_spec.rb
CHANGED
|
@@ -1,36 +1,18 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
|
-
describe 'callbacks for the old DSL' do
|
|
4
|
-
let(:callback) {CallbackOldDsl.new}
|
|
5
|
-
|
|
6
|
-
it "should get close callbacks" do
|
|
7
|
-
callback.should_receive(:exit_open).once.ordered
|
|
8
|
-
callback.should_receive(:before).once.ordered
|
|
9
|
-
callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
|
|
10
|
-
callback.should_receive(:before_enter_closed).once.ordered
|
|
11
|
-
callback.should_receive(:enter_closed).once.ordered
|
|
12
|
-
callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
|
13
|
-
callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
|
|
14
|
-
callback.should_receive(:after_enter_closed).once.ordered
|
|
15
|
-
callback.should_receive(:after).once.ordered
|
|
16
|
-
|
|
17
|
-
callback.close!
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
3
|
describe 'callbacks for the new DSL' do
|
|
22
4
|
let(:callback) {CallbackNewDsl.new}
|
|
23
5
|
|
|
24
6
|
it "be called in order" do
|
|
25
|
-
callback.
|
|
26
|
-
callback.
|
|
27
|
-
callback.
|
|
28
|
-
callback.
|
|
29
|
-
callback.
|
|
30
|
-
callback.
|
|
31
|
-
callback.
|
|
32
|
-
callback.
|
|
33
|
-
callback.
|
|
7
|
+
expect(callback).to receive(:exit_open).once.ordered
|
|
8
|
+
expect(callback).to receive(:before).once.ordered
|
|
9
|
+
expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes
|
|
10
|
+
expect(callback).to receive(:before_enter_closed).once.ordered
|
|
11
|
+
expect(callback).to receive(:enter_closed).once.ordered
|
|
12
|
+
expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
|
13
|
+
expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes
|
|
14
|
+
expect(callback).to receive(:after_enter_closed).once.ordered
|
|
15
|
+
expect(callback).to receive(:after).once.ordered
|
|
34
16
|
|
|
35
17
|
callback.close!
|
|
36
18
|
end
|
|
@@ -40,8 +22,10 @@ describe 'event callbacks' do
|
|
|
40
22
|
describe "with an error callback defined" do
|
|
41
23
|
before do
|
|
42
24
|
class Foo
|
|
43
|
-
|
|
44
|
-
|
|
25
|
+
aasm do
|
|
26
|
+
event :safe_close, :success => :success_callback, :error => :error_callback do
|
|
27
|
+
transitions :to => :closed, :from => [:open]
|
|
28
|
+
end
|
|
45
29
|
end
|
|
46
30
|
end
|
|
47
31
|
|
|
@@ -51,20 +35,20 @@ describe 'event callbacks' do
|
|
|
51
35
|
it "should run error_callback if an exception is raised and error_callback defined" do
|
|
52
36
|
def @foo.error_callback(e); end
|
|
53
37
|
|
|
54
|
-
@foo.
|
|
55
|
-
@foo.
|
|
38
|
+
allow(@foo).to receive(:enter).and_raise(e=StandardError.new)
|
|
39
|
+
expect(@foo).to receive(:error_callback).with(e)
|
|
56
40
|
|
|
57
41
|
@foo.safe_close!
|
|
58
42
|
end
|
|
59
43
|
|
|
60
44
|
it "should raise NoMethodError if exceptionis raised and error_callback is declared but not defined" do
|
|
61
|
-
@foo.
|
|
62
|
-
|
|
45
|
+
allow(@foo).to receive(:enter).and_raise(StandardError)
|
|
46
|
+
expect{@foo.safe_close!}.to raise_error(NoMethodError)
|
|
63
47
|
end
|
|
64
48
|
|
|
65
49
|
it "should propagate an error if no error callback is declared" do
|
|
66
|
-
@foo.
|
|
67
|
-
|
|
50
|
+
allow(@foo).to receive(:enter).and_raise("Cannot enter safe")
|
|
51
|
+
expect{@foo.close!}.to raise_error(StandardError, "Cannot enter safe")
|
|
68
52
|
end
|
|
69
53
|
end
|
|
70
54
|
|
|
@@ -75,18 +59,18 @@ describe 'event callbacks' do
|
|
|
75
59
|
end
|
|
76
60
|
|
|
77
61
|
it 'should call it for successful bang fire' do
|
|
78
|
-
@foo.
|
|
62
|
+
expect(@foo).to receive(:aasm_event_fired).with(:close, :open, :closed)
|
|
79
63
|
@foo.close!
|
|
80
64
|
end
|
|
81
65
|
|
|
82
66
|
it 'should call it for successful non-bang fire' do
|
|
83
|
-
@foo.
|
|
67
|
+
expect(@foo).to receive(:aasm_event_fired)
|
|
84
68
|
@foo.close
|
|
85
69
|
end
|
|
86
70
|
|
|
87
71
|
it 'should not call it for failing bang fire' do
|
|
88
|
-
@foo.aasm.
|
|
89
|
-
@foo.
|
|
72
|
+
allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
|
|
73
|
+
expect(@foo).not_to receive(:aasm_event_fired)
|
|
90
74
|
@foo.close!
|
|
91
75
|
end
|
|
92
76
|
end
|
|
@@ -98,18 +82,18 @@ describe 'event callbacks' do
|
|
|
98
82
|
end
|
|
99
83
|
|
|
100
84
|
it 'should call it when transition failed for bang fire' do
|
|
101
|
-
@foo.
|
|
102
|
-
|
|
85
|
+
expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
|
|
86
|
+
expect {@foo.null!}.to raise_error(AASM::InvalidTransition)
|
|
103
87
|
end
|
|
104
88
|
|
|
105
89
|
it 'should call it when transition failed for non-bang fire' do
|
|
106
|
-
@foo.
|
|
107
|
-
|
|
90
|
+
expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
|
|
91
|
+
expect {@foo.null}.to raise_error(AASM::InvalidTransition)
|
|
108
92
|
end
|
|
109
93
|
|
|
110
94
|
it 'should not call it if persist fails for bang fire' do
|
|
111
|
-
@foo.aasm.
|
|
112
|
-
@foo.
|
|
95
|
+
allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
|
|
96
|
+
expect(@foo).to receive(:aasm_event_failed)
|
|
113
97
|
@foo.close!
|
|
114
98
|
end
|
|
115
99
|
end
|