aasm 4.12.3 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +14 -12
  3. data/Appraisals +39 -16
  4. data/CHANGELOG.md +8 -1
  5. data/Dockerfile +44 -0
  6. data/Gemfile +1 -2
  7. data/README.md +51 -11
  8. data/docker-compose.yml +40 -0
  9. data/gemfiles/rails_3.2.gemfile +6 -6
  10. data/gemfiles/rails_4.0.gemfile +7 -7
  11. data/gemfiles/rails_4.2.gemfile +8 -8
  12. data/gemfiles/rails_4.2_mongoid_5.gemfile +5 -5
  13. data/gemfiles/rails_4.2_nobrainer.gemfile +9 -0
  14. data/gemfiles/rails_5.0.gemfile +5 -6
  15. data/gemfiles/rails_5.0_nobrainer.gemfile +9 -0
  16. data/gemfiles/rails_5.1.gemfile +13 -0
  17. data/lib/aasm.rb +5 -0
  18. data/lib/aasm/aasm.rb +1 -0
  19. data/lib/aasm/core/event.rb +5 -21
  20. data/lib/aasm/core/invoker.rb +129 -0
  21. data/lib/aasm/core/invokers/base_invoker.rb +75 -0
  22. data/lib/aasm/core/invokers/class_invoker.rb +52 -0
  23. data/lib/aasm/core/invokers/literal_invoker.rb +47 -0
  24. data/lib/aasm/core/invokers/proc_invoker.rb +59 -0
  25. data/lib/aasm/core/state.rb +10 -9
  26. data/lib/aasm/core/transition.rb +7 -68
  27. data/lib/aasm/errors.rb +2 -2
  28. data/lib/aasm/persistence.rb +3 -0
  29. data/lib/aasm/persistence/no_brainer_persistence.rb +105 -0
  30. data/lib/aasm/persistence/plain_persistence.rb +2 -1
  31. data/lib/aasm/persistence/sequel_persistence.rb +0 -1
  32. data/lib/aasm/rspec/allow_event.rb +5 -1
  33. data/lib/aasm/rspec/allow_transition_to.rb +5 -1
  34. data/lib/aasm/version.rb +1 -1
  35. data/lib/generators/nobrainer/aasm_generator.rb +28 -0
  36. data/lib/motion-aasm.rb +1 -0
  37. data/spec/generators/no_brainer_generator_spec.rb +29 -0
  38. data/spec/models/nobrainer/complex_no_brainer_example.rb +36 -0
  39. data/spec/models/nobrainer/invalid_persistor_no_brainer.rb +39 -0
  40. data/spec/models/nobrainer/no_scope_no_brainer.rb +21 -0
  41. data/spec/models/nobrainer/nobrainer_relationships.rb +25 -0
  42. data/spec/models/nobrainer/silent_persistor_no_brainer.rb +39 -0
  43. data/spec/models/nobrainer/simple_new_dsl_nobrainer.rb +25 -0
  44. data/spec/models/nobrainer/simple_no_brainer.rb +23 -0
  45. data/spec/models/nobrainer/validator_no_brainer.rb +98 -0
  46. data/spec/models/simple_example.rb +2 -0
  47. data/spec/models/simple_example_with_guard_args.rb +17 -0
  48. data/spec/spec_helpers/active_record.rb +2 -1
  49. data/spec/spec_helpers/dynamoid.rb +7 -5
  50. data/spec/spec_helpers/mongoid.rb +20 -1
  51. data/spec/spec_helpers/nobrainer.rb +15 -0
  52. data/spec/spec_helpers/redis.rb +5 -2
  53. data/spec/spec_helpers/sequel.rb +1 -1
  54. data/spec/unit/callbacks_spec.rb +2 -2
  55. data/spec/unit/exception_spec.rb +1 -1
  56. data/spec/unit/invoker_spec.rb +189 -0
  57. data/spec/unit/invokers/base_invoker_spec.rb +72 -0
  58. data/spec/unit/invokers/class_invoker_spec.rb +95 -0
  59. data/spec/unit/invokers/literal_invoker_spec.rb +86 -0
  60. data/spec/unit/invokers/proc_invoker_spec.rb +86 -0
  61. data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +0 -4
  62. data/spec/unit/persistence/mongoid_persistence_spec.rb +0 -4
  63. data/spec/unit/persistence/no_brainer_persistence_multiple_spec.rb +198 -0
  64. data/spec/unit/persistence/no_brainer_persistence_spec.rb +158 -0
  65. data/spec/unit/rspec_matcher_spec.rb +6 -0
  66. data/spec/unit/state_spec.rb +2 -2
  67. data/spec/unit/transition_spec.rb +1 -1
  68. data/test/minitest_helper.rb +2 -2
  69. data/test/unit/minitest_matcher_test.rb +1 -1
  70. metadata +51 -3
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe AASM::Core::Invokers::ProcInvoker do
4
+ let(:target) { Proc.new {} }
5
+ let(:record) { double }
6
+ let(:args) { [] }
7
+
8
+ subject { described_class.new(target, record, args) }
9
+
10
+ describe '#may_invoke?' do
11
+ context 'when subject is a Proc' do
12
+ it 'then returns "true"' do
13
+ expect(subject.may_invoke?).to eq(true)
14
+ end
15
+ end
16
+
17
+ context 'when subject is not a Proc' do
18
+ let(:target) { nil }
19
+
20
+ it 'then returns "false"' do
21
+ expect(subject.may_invoke?).to eq(false)
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '#log_failure' do
27
+ context 'when subject respond to #source_location' do
28
+ it 'then adds "source_location" to a failures buffer' do
29
+ subject.log_failure
30
+
31
+ expect(subject.failures)
32
+ .to eq([target.source_location.join('#')])
33
+ end
34
+ end
35
+
36
+ context 'when subject does not respond to #source_location' do
37
+ before do
38
+ Method.__send__(:alias_method, :original_source_location, :source_location)
39
+ Method.__send__(:undef_method, :source_location)
40
+ end
41
+
42
+ after do
43
+ Method.__send__(
44
+ :define_method,
45
+ :source_location,
46
+ Method.instance_method(:original_source_location)
47
+ )
48
+ end
49
+
50
+ it 'then adds the subject to a failures buffer' do
51
+ subject.log_failure
52
+
53
+ expect(subject.failures).to eq([target])
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#invoke_subject' do
59
+ context 'when passing no arguments' do
60
+ let(:args) { [1, 2 ,3] }
61
+ let(:target) { ->() {} }
62
+
63
+ it 'then correctly uses passed arguments' do
64
+ expect { subject.invoke_subject }.not_to raise_error
65
+ end
66
+ end
67
+
68
+ context 'when passing variable number arguments' do
69
+ let(:args) { [1, 2 ,3, 4, 5, 6] }
70
+ let(:target) { ->(_a, _b, *_c) {} }
71
+
72
+ it 'then correctly uses passed arguments' do
73
+ expect { subject.invoke_subject }.not_to raise_error
74
+ end
75
+ end
76
+
77
+ context 'when passing one or more arguments' do
78
+ let(:args) { [1, 2 ,3, 4, 5, 6] }
79
+ let(:target) { ->(_a, _b, _c) {} }
80
+
81
+ it 'then correctly uses passed arguments' do
82
+ expect { subject.invoke_subject }.not_to raise_error
83
+ end
84
+ end
85
+ end
86
+ end
@@ -10,10 +10,6 @@ if defined?(Mongoid::Document)
10
10
  before(:all) do
11
11
  # if you want to see the statements while running the spec enable the following line
12
12
  # Mongoid.logger = Logger.new(STDERR)
13
-
14
- Mongoid.configure do |config|
15
- config.connect_to "mongoid_#{Process.pid}"
16
- end
17
13
  end
18
14
 
19
15
  after do
@@ -10,10 +10,6 @@ if defined?(Mongoid::Document)
10
10
  before(:all) do
11
11
  # if you want to see the statements while running the spec enable the following line
12
12
  # Mongoid.logger = Logger.new(STDERR)
13
-
14
- Mongoid.configure do |config|
15
- config.connect_to "mongoid_#{Process.pid}"
16
- end
17
13
  end
18
14
 
19
15
  after do
@@ -0,0 +1,198 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(NoBrainer::Document)
4
+ describe 'nobrainer' do
5
+ Dir[File.dirname(__FILE__) + '/../../models/nobrainer/*.rb'].sort.each do |f|
6
+ require File.expand_path(f)
7
+ end
8
+
9
+ before(:all) do
10
+ # if you want to see the statements while running the spec enable the
11
+ # following line
12
+ # NoBrainer.configure do |config|
13
+ # config.logger = Logger.new(STDERR)
14
+ # end
15
+ end
16
+
17
+ after do
18
+ NoBrainer.purge!
19
+ end
20
+
21
+ describe 'named scopes with the old DSL' do
22
+ context 'Does not already respond_to? the scope name' do
23
+ it 'should add a scope for each state' do
24
+ expect(SimpleNoBrainerMultiple).to respond_to(:unknown_scope)
25
+ expect(SimpleNoBrainerMultiple).to respond_to(:another_unknown_scope)
26
+
27
+ expect(SimpleNoBrainerMultiple.unknown_scope.class).to eq(NoBrainer::Criteria)
28
+ expect(SimpleNoBrainerMultiple.another_unknown_scope.class).to eq(NoBrainer::Criteria)
29
+ end
30
+ end
31
+
32
+ context 'Already respond_to? the scope name' do
33
+ it 'should not add a scope' do
34
+ expect(SimpleNoBrainerMultiple).to respond_to(:new)
35
+ expect(SimpleNoBrainerMultiple.new.class).to eq(SimpleNoBrainerMultiple)
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ describe 'named scopes with the new DSL' do
42
+ context 'Does not already respond_to? the scope name' do
43
+ it 'should add a scope' do
44
+ expect(SimpleNewDslNoBrainerMultiple).to respond_to(:unknown_scope)
45
+ expect(SimpleNewDslNoBrainerMultiple.unknown_scope.class).to eq(NoBrainer::Criteria)
46
+ end
47
+ end
48
+
49
+ context 'Already respond_to? the scope name' do
50
+ it 'should not add a scope' do
51
+ expect(SimpleNewDslNoBrainerMultiple).to respond_to(:new)
52
+ expect(SimpleNewDslNoBrainerMultiple.new.class).to eq(SimpleNewDslNoBrainerMultiple)
53
+ end
54
+ end
55
+
56
+ it 'does not create scopes if requested' do
57
+ expect(NoScopeNoBrainerMultiple).not_to respond_to(:ignored_scope)
58
+ end
59
+ end
60
+
61
+ describe 'instance methods' do
62
+ let(:simple) { SimpleNewDslNoBrainerMultiple.new }
63
+
64
+ it 'should initialize the aasm state on instantiation' do
65
+ expect(SimpleNewDslNoBrainerMultiple.new.status).to eql 'unknown_scope'
66
+ expect(SimpleNewDslNoBrainerMultiple.new.aasm(:left).current_state).to eql :unknown_scope
67
+ end
68
+ end
69
+
70
+ describe 'transitions with persistence' do
71
+ it 'should work for valid models' do
72
+ valid_object = MultipleValidatorNoBrainer.create(name: 'name')
73
+ expect(valid_object).to be_sleeping
74
+ valid_object.status = :running
75
+ expect(valid_object).to be_running
76
+ end
77
+
78
+ it 'should not store states for invalid models' do
79
+ validator = MultipleValidatorNoBrainer.create(name: 'name')
80
+ expect(validator).to be_valid
81
+ expect(validator).to be_sleeping
82
+
83
+ validator.name = nil
84
+ expect(validator).not_to be_valid
85
+ expect { validator.run! }.to raise_error(NoBrainer::Error::DocumentInvalid)
86
+ expect(validator).to be_sleeping
87
+
88
+ validator.reload
89
+ expect(validator).not_to be_running
90
+ expect(validator).to be_sleeping
91
+
92
+ validator.name = 'another name'
93
+ expect(validator).to be_valid
94
+ expect(validator.run!).to be_truthy
95
+ expect(validator).to be_running
96
+
97
+ validator.reload
98
+ expect(validator).to be_running
99
+ expect(validator).not_to be_sleeping
100
+ end
101
+
102
+ it 'should not store states for invalid models silently if configured' do
103
+ validator = MultipleSilentPersistorNoBrainer.create(name: 'name')
104
+ expect(validator).to be_valid
105
+ expect(validator).to be_sleeping
106
+
107
+ validator.name = nil
108
+ expect(validator).not_to be_valid
109
+ expect(validator.run!).to be_falsey
110
+ expect(validator).to be_sleeping
111
+
112
+ validator.reload
113
+ expect(validator).not_to be_running
114
+ expect(validator).to be_sleeping
115
+
116
+ validator.name = 'another name'
117
+ expect(validator).to be_valid
118
+ expect(validator.run!).to be_truthy
119
+ expect(validator).to be_running
120
+
121
+ validator.reload
122
+ expect(validator).to be_running
123
+ expect(validator).not_to be_sleeping
124
+ end
125
+
126
+ it 'should store states for invalid models if configured' do
127
+ persistor = MultipleInvalidPersistorNoBrainer.create(name: 'name')
128
+ expect(persistor).to be_valid
129
+ expect(persistor).to be_sleeping
130
+
131
+ persistor.name = nil
132
+ expect(persistor).not_to be_valid
133
+ expect(persistor.run!).to be_truthy
134
+ expect(persistor).to be_running
135
+
136
+ persistor = MultipleInvalidPersistorNoBrainer.find(persistor.id)
137
+ persistor.valid?
138
+ expect(persistor).to be_valid
139
+ expect(persistor).to be_running
140
+ expect(persistor).not_to be_sleeping
141
+
142
+ persistor.reload
143
+ expect(persistor).to be_running
144
+ expect(persistor).not_to be_sleeping
145
+ end
146
+ end
147
+
148
+ describe 'complex example' do
149
+ it 'works' do
150
+ record = ComplexNoBrainerExample.new
151
+ expect_aasm_states record, :one, :alpha
152
+
153
+ record.save!
154
+ expect_aasm_states record, :one, :alpha
155
+ record.reload
156
+ expect_aasm_states record, :one, :alpha
157
+
158
+ record.increment!
159
+ expect_aasm_states record, :two, :alpha
160
+ record.reload
161
+ expect_aasm_states record, :two, :alpha
162
+
163
+ record.level_up!
164
+ expect_aasm_states record, :two, :beta
165
+ record.reload
166
+ expect_aasm_states record, :two, :beta
167
+
168
+ record.increment!
169
+ expect { record.increment! }.to raise_error(AASM::InvalidTransition)
170
+ expect_aasm_states record, :three, :beta
171
+ record.reload
172
+ expect_aasm_states record, :three, :beta
173
+
174
+ record.level_up!
175
+ expect_aasm_states record, :three, :gamma
176
+ record.reload
177
+ expect_aasm_states record, :three, :gamma
178
+
179
+ record.level_down # without saving
180
+ expect_aasm_states record, :three, :beta
181
+ record.reload
182
+ expect_aasm_states record, :three, :gamma
183
+
184
+ record.level_down # without saving
185
+ expect_aasm_states record, :three, :beta
186
+ record.reset!
187
+ expect_aasm_states record, :one, :beta
188
+ end
189
+
190
+ def expect_aasm_states(record, left_state, right_state)
191
+ expect(record.aasm(:left).current_state).to eql left_state.to_sym
192
+ expect(record.left).to eql left_state.to_s
193
+ expect(record.aasm(:right).current_state).to eql right_state.to_sym
194
+ expect(record.right).to eql right_state.to_s
195
+ end
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,158 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(NoBrainer::Document)
4
+ describe 'nobrainer' do
5
+ Dir[File.dirname(__FILE__) + '/../../models/nobrainer/*.rb'].sort.each do |f|
6
+ require File.expand_path(f)
7
+ end
8
+
9
+ before(:all) do
10
+ # if you want to see the statements while running the spec enable the
11
+ # following line
12
+ # NoBrainer.configure do |config|
13
+ # config.logger = Logger.new(STDERR)
14
+ # end
15
+ end
16
+
17
+ after do
18
+ NoBrainer.purge!
19
+ end
20
+
21
+ describe 'named scopes with the old DSL' do
22
+ context 'Does not already respond_to? the scope name' do
23
+ it 'should add a scope for each state' do
24
+ expect(SimpleNoBrainer).to respond_to(:unknown_scope)
25
+ expect(SimpleNoBrainer).to respond_to(:another_unknown_scope)
26
+
27
+ expect(SimpleNoBrainer.unknown_scope.class).to eq(NoBrainer::Criteria)
28
+ expect(SimpleNoBrainer.another_unknown_scope.class).to eq(NoBrainer::Criteria)
29
+ end
30
+ end
31
+
32
+ context 'Already respond_to? the scope name' do
33
+ it 'should not add a scope' do
34
+ expect(SimpleNoBrainer).to respond_to(:new)
35
+ expect(SimpleNoBrainer.new.class).to eq(SimpleNoBrainer)
36
+ end
37
+ end
38
+ end
39
+
40
+ describe 'named scopes with the new DSL' do
41
+ context 'Does not already respond_to? the scope name' do
42
+ it 'should add a scope' do
43
+ expect(SimpleNewDslNoBrainer).to respond_to(:unknown_scope)
44
+ expect(SimpleNewDslNoBrainer.unknown_scope.class).to eq(NoBrainer::Criteria)
45
+ end
46
+ end
47
+
48
+ context 'Already respond_to? the scope name' do
49
+ it 'should not add a scope' do
50
+ expect(SimpleNewDslNoBrainer).to respond_to(:new)
51
+ expect(SimpleNewDslNoBrainer.new.class).to eq(SimpleNewDslNoBrainer)
52
+ end
53
+ end
54
+
55
+ it 'does not create scopes if requested' do
56
+ expect(NoScopeNoBrainer).not_to respond_to(:ignored_scope)
57
+ end
58
+ end
59
+
60
+ describe 'instance methods' do
61
+ let(:simple) { SimpleNewDslNoBrainer.new }
62
+
63
+ it 'should initialize the aasm state on instantiation' do
64
+ expect(SimpleNewDslNoBrainer.new.status).to eql 'unknown_scope'
65
+ expect(SimpleNewDslNoBrainer.new.aasm.current_state).to eql :unknown_scope
66
+ end
67
+ end
68
+
69
+ describe 'relations object' do
70
+ it 'should load relations object ids' do
71
+ parent = Parent.create
72
+ child_1 = Child.create(parent_id: parent.id)
73
+ child_2 = Child.create(parent_id: parent.id)
74
+ expect(parent.childs.pluck(:id, :status).map(&:id)).to eql [child_1.id, child_2.id]
75
+ end
76
+ end
77
+
78
+ describe 'transitions with persistence' do
79
+ it 'should work for valid models' do
80
+ valid_object = ValidatorNoBrainer.create(name: 'name')
81
+ expect(valid_object).to be_sleeping
82
+ valid_object.status = :running
83
+ expect(valid_object).to be_running
84
+ end
85
+
86
+ it 'should not store states for invalid models' do
87
+ validator = ValidatorNoBrainer.create(name: 'name')
88
+ expect(validator).to be_valid
89
+ expect(validator).to be_sleeping
90
+
91
+ validator.name = nil
92
+ expect(validator).not_to be_valid
93
+ expect { validator.run! }.to raise_error(NoBrainer::Error::DocumentInvalid)
94
+ expect(validator).to be_sleeping
95
+
96
+ validator.reload
97
+ expect(validator).not_to be_running
98
+ expect(validator).to be_sleeping
99
+
100
+ validator.name = 'another name'
101
+ expect(validator).to be_valid
102
+ expect(validator.run!).to be_truthy
103
+ expect(validator).to be_running
104
+
105
+ validator.reload
106
+ expect(validator).to be_running
107
+ expect(validator).not_to be_sleeping
108
+ end
109
+
110
+ it 'should not store states for invalid models silently if configured' do
111
+ validator = SilentPersistorNoBrainer.create(name: 'name')
112
+ expect(validator).to be_valid
113
+ expect(validator).to be_sleeping
114
+
115
+ validator.name = nil
116
+ expect(validator).not_to be_valid
117
+ expect(validator.run!).to be_falsey
118
+ expect(validator).to be_sleeping
119
+
120
+ validator.reload
121
+ expect(validator).not_to be_running
122
+ expect(validator).to be_sleeping
123
+
124
+ validator.name = 'another name'
125
+ expect(validator).to be_valid
126
+ expect(validator.run!).to be_truthy
127
+ expect(validator).to be_running
128
+
129
+ validator.reload
130
+ expect(validator).to be_running
131
+ expect(validator).not_to be_sleeping
132
+ end
133
+
134
+ it 'should store states for invalid models if configured' do
135
+ persistor = InvalidPersistorNoBrainer.create(name: 'name')
136
+ expect(persistor).to be_valid
137
+ expect(persistor).to be_sleeping
138
+
139
+ persistor.name = nil
140
+
141
+ expect(persistor).not_to be_valid
142
+ expect(persistor.run!).to be_truthy
143
+ expect(persistor).to be_running
144
+
145
+ persistor = InvalidPersistorNoBrainer.find(persistor.id)
146
+
147
+ persistor.valid?
148
+ expect(persistor).to be_valid
149
+ expect(persistor).to be_running
150
+ expect(persistor).not_to be_sleeping
151
+
152
+ persistor.reload
153
+ expect(persistor).to be_running
154
+ expect(persistor).not_to be_sleeping
155
+ end
156
+ end
157
+ end
158
+ end