aasm 4.11.1 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +27 -0
  3. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  4. data/.travis.yml +56 -23
  5. data/Appraisals +67 -0
  6. data/CHANGELOG.md +112 -0
  7. data/CONTRIBUTING.md +24 -0
  8. data/Dockerfile +44 -0
  9. data/Gemfile +3 -21
  10. data/Gemfile.lock_old +151 -0
  11. data/LICENSE +1 -1
  12. data/README.md +540 -139
  13. data/Rakefile +6 -1
  14. data/TESTING.md +25 -0
  15. data/aasm.gemspec +5 -0
  16. data/docker-compose.yml +40 -0
  17. data/gemfiles/norails.gemfile +10 -0
  18. data/gemfiles/rails_4.2.gemfile +13 -11
  19. data/gemfiles/rails_4.2_mongoid_5.gemfile +8 -11
  20. data/gemfiles/rails_4.2_nobrainer.gemfile +9 -0
  21. data/gemfiles/rails_5.0.gemfile +11 -18
  22. data/gemfiles/rails_5.0_nobrainer.gemfile +9 -0
  23. data/gemfiles/rails_5.1.gemfile +14 -0
  24. data/gemfiles/rails_5.2.gemfile +14 -0
  25. data/lib/aasm/aasm.rb +40 -29
  26. data/lib/aasm/base.rb +61 -11
  27. data/lib/aasm/configuration.rb +10 -0
  28. data/lib/aasm/core/event.rb +45 -37
  29. data/lib/aasm/core/invoker.rb +129 -0
  30. data/lib/aasm/core/invokers/base_invoker.rb +75 -0
  31. data/lib/aasm/core/invokers/class_invoker.rb +52 -0
  32. data/lib/aasm/core/invokers/literal_invoker.rb +47 -0
  33. data/lib/aasm/core/invokers/proc_invoker.rb +59 -0
  34. data/lib/aasm/core/state.rb +22 -13
  35. data/lib/aasm/core/transition.rb +17 -69
  36. data/lib/aasm/dsl_helper.rb +24 -22
  37. data/lib/aasm/errors.rb +4 -6
  38. data/lib/aasm/instance_base.rb +22 -4
  39. data/lib/aasm/localizer.rb +13 -3
  40. data/lib/aasm/minitest/allow_event.rb +13 -0
  41. data/lib/aasm/minitest/allow_transition_to.rb +13 -0
  42. data/lib/aasm/minitest/have_state.rb +13 -0
  43. data/lib/aasm/minitest/transition_from.rb +21 -0
  44. data/lib/aasm/minitest.rb +5 -0
  45. data/lib/aasm/minitest_spec.rb +15 -0
  46. data/lib/aasm/persistence/active_record_persistence.rb +49 -105
  47. data/lib/aasm/persistence/base.rb +20 -5
  48. data/lib/aasm/persistence/core_data_query_persistence.rb +2 -1
  49. data/lib/aasm/persistence/dynamoid_persistence.rb +1 -1
  50. data/lib/aasm/persistence/mongoid_persistence.rb +26 -32
  51. data/lib/aasm/persistence/no_brainer_persistence.rb +105 -0
  52. data/lib/aasm/persistence/orm.rb +154 -0
  53. data/lib/aasm/persistence/plain_persistence.rb +2 -1
  54. data/lib/aasm/persistence/redis_persistence.rb +16 -11
  55. data/lib/aasm/persistence/sequel_persistence.rb +36 -64
  56. data/lib/aasm/persistence.rb +3 -3
  57. data/lib/aasm/rspec/allow_event.rb +5 -1
  58. data/lib/aasm/rspec/allow_transition_to.rb +5 -1
  59. data/lib/aasm/rspec/transition_from.rb +5 -1
  60. data/lib/aasm/state_machine.rb +4 -2
  61. data/lib/aasm/state_machine_store.rb +5 -2
  62. data/lib/aasm/version.rb +1 -1
  63. data/lib/aasm.rb +5 -2
  64. data/lib/generators/aasm/orm_helpers.rb +6 -0
  65. data/lib/generators/active_record/aasm_generator.rb +3 -1
  66. data/lib/generators/active_record/templates/migration.rb +1 -1
  67. data/lib/generators/active_record/templates/migration_existing.rb +1 -1
  68. data/lib/generators/nobrainer/aasm_generator.rb +28 -0
  69. data/lib/motion-aasm.rb +3 -1
  70. data/spec/database.rb +20 -7
  71. data/spec/en.yml +0 -3
  72. data/spec/generators/active_record_generator_spec.rb +49 -40
  73. data/spec/generators/mongoid_generator_spec.rb +4 -6
  74. data/spec/generators/no_brainer_generator_spec.rb +29 -0
  75. data/spec/{en_deprecated_style.yml → localizer_test_model_deprecated_style.yml} +6 -3
  76. data/spec/localizer_test_model_new_style.yml +11 -0
  77. data/spec/models/active_record/active_record_callback.rb +93 -0
  78. data/spec/models/active_record/complex_active_record_example.rb +5 -1
  79. data/spec/models/active_record/instance_level_skip_validation_example.rb +19 -0
  80. data/spec/models/{invalid_persistor.rb → active_record/invalid_persistor.rb} +0 -2
  81. data/spec/models/active_record/localizer_test_model.rb +11 -3
  82. data/spec/models/active_record/namespaced.rb +16 -0
  83. data/spec/models/active_record/person.rb +23 -0
  84. data/spec/models/{silent_persistor.rb → active_record/silent_persistor.rb} +0 -2
  85. data/spec/models/active_record/simple_new_dsl.rb +15 -0
  86. data/spec/models/active_record/timestamp_example.rb +16 -0
  87. data/spec/models/{transactor.rb → active_record/transactor.rb} +25 -2
  88. data/spec/models/{validator.rb → active_record/validator.rb} +0 -2
  89. data/spec/models/active_record/work.rb +3 -0
  90. data/spec/models/{worker.rb → active_record/worker.rb} +0 -0
  91. data/spec/models/callbacks/basic.rb +5 -2
  92. data/spec/models/callbacks/with_state_arg.rb +5 -1
  93. data/spec/models/callbacks/with_state_arg_multiple.rb +4 -1
  94. data/spec/models/default_state.rb +1 -1
  95. data/spec/models/guard_arguments_check.rb +17 -0
  96. data/spec/models/guard_with_params.rb +1 -1
  97. data/spec/models/guardian_without_from_specified.rb +18 -0
  98. data/spec/models/mongoid/invalid_persistor_mongoid.rb +39 -0
  99. data/spec/models/mongoid/silent_persistor_mongoid.rb +39 -0
  100. data/spec/models/mongoid/timestamp_example_mongoid.rb +20 -0
  101. data/spec/models/mongoid/validator_mongoid.rb +100 -0
  102. data/spec/models/multiple_transitions_that_differ_only_by_guard.rb +31 -0
  103. data/spec/models/namespaced_multiple_example.rb +14 -0
  104. data/spec/models/nobrainer/complex_no_brainer_example.rb +36 -0
  105. data/spec/models/nobrainer/invalid_persistor_no_brainer.rb +39 -0
  106. data/spec/models/nobrainer/no_scope_no_brainer.rb +21 -0
  107. data/spec/models/nobrainer/nobrainer_relationships.rb +25 -0
  108. data/spec/models/nobrainer/silent_persistor_no_brainer.rb +39 -0
  109. data/spec/models/nobrainer/simple_new_dsl_nobrainer.rb +25 -0
  110. data/spec/models/{mongo_mapper/simple_mongo_mapper.rb → nobrainer/simple_no_brainer.rb} +8 -8
  111. data/spec/models/nobrainer/validator_no_brainer.rb +98 -0
  112. data/spec/models/parametrised_event.rb +7 -0
  113. data/spec/models/{mongo_mapper/complex_mongo_mapper_example.rb → redis/complex_redis_example.rb} +8 -5
  114. data/spec/models/redis/redis_multiple.rb +20 -0
  115. data/spec/models/redis/redis_simple.rb +20 -0
  116. data/spec/models/sequel/complex_sequel_example.rb +4 -3
  117. data/spec/models/sequel/invalid_persistor.rb +52 -0
  118. data/spec/models/sequel/sequel_multiple.rb +13 -13
  119. data/spec/models/sequel/sequel_simple.rb +13 -12
  120. data/spec/models/sequel/silent_persistor.rb +50 -0
  121. data/spec/models/sequel/transactor.rb +112 -0
  122. data/spec/models/sequel/validator.rb +93 -0
  123. data/spec/models/sequel/worker.rb +12 -0
  124. data/spec/models/simple_example.rb +8 -0
  125. data/spec/models/simple_example_with_guard_args.rb +17 -0
  126. data/spec/models/simple_multiple_example.rb +12 -0
  127. data/spec/models/sub_class.rb +34 -0
  128. data/spec/models/timestamps_example.rb +19 -0
  129. data/spec/models/timestamps_with_named_machine_example.rb +13 -0
  130. data/spec/spec_helper.rb +15 -33
  131. data/spec/spec_helpers/active_record.rb +8 -0
  132. data/spec/spec_helpers/dynamoid.rb +35 -0
  133. data/spec/spec_helpers/mongoid.rb +26 -0
  134. data/spec/spec_helpers/nobrainer.rb +15 -0
  135. data/spec/spec_helpers/redis.rb +18 -0
  136. data/spec/spec_helpers/remove_warnings.rb +1 -0
  137. data/spec/spec_helpers/sequel.rb +7 -0
  138. data/spec/unit/abstract_class_spec.rb +27 -0
  139. data/spec/unit/api_spec.rb +79 -72
  140. data/spec/unit/callback_multiple_spec.rb +7 -3
  141. data/spec/unit/callbacks_spec.rb +37 -2
  142. data/spec/unit/complex_example_spec.rb +12 -3
  143. data/spec/unit/complex_multiple_example_spec.rb +20 -4
  144. data/spec/unit/event_multiple_spec.rb +1 -1
  145. data/spec/unit/event_spec.rb +29 -4
  146. data/spec/unit/exception_spec.rb +1 -1
  147. data/spec/unit/guard_arguments_check_spec.rb +9 -0
  148. data/spec/unit/guard_spec.rb +17 -0
  149. data/spec/unit/guard_with_params_spec.rb +4 -0
  150. data/spec/unit/guard_without_from_specified_spec.rb +10 -0
  151. data/spec/unit/inspection_multiple_spec.rb +9 -5
  152. data/spec/unit/inspection_spec.rb +7 -3
  153. data/spec/unit/invoker_spec.rb +189 -0
  154. data/spec/unit/invokers/base_invoker_spec.rb +72 -0
  155. data/spec/unit/invokers/class_invoker_spec.rb +95 -0
  156. data/spec/unit/invokers/literal_invoker_spec.rb +86 -0
  157. data/spec/unit/invokers/proc_invoker_spec.rb +86 -0
  158. data/spec/unit/localizer_spec.rb +85 -52
  159. data/spec/unit/multiple_transitions_that_differ_only_by_guard_spec.rb +14 -0
  160. data/spec/unit/namespaced_multiple_example_spec.rb +22 -0
  161. data/spec/unit/override_warning_spec.rb +8 -0
  162. data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +468 -447
  163. data/spec/unit/persistence/active_record_persistence_spec.rb +639 -486
  164. data/spec/unit/persistence/dynamoid_persistence_multiple_spec.rb +4 -9
  165. data/spec/unit/persistence/dynamoid_persistence_spec.rb +4 -9
  166. data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +83 -13
  167. data/spec/unit/persistence/mongoid_persistence_spec.rb +97 -13
  168. data/spec/unit/persistence/no_brainer_persistence_multiple_spec.rb +198 -0
  169. data/spec/unit/persistence/no_brainer_persistence_spec.rb +158 -0
  170. data/spec/unit/persistence/redis_persistence_multiple_spec.rb +88 -0
  171. data/spec/unit/persistence/redis_persistence_spec.rb +8 -32
  172. data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +6 -11
  173. data/spec/unit/persistence/sequel_persistence_spec.rb +278 -10
  174. data/spec/unit/rspec_matcher_spec.rb +9 -0
  175. data/spec/unit/simple_example_spec.rb +15 -0
  176. data/spec/unit/simple_multiple_example_spec.rb +28 -0
  177. data/spec/unit/state_spec.rb +23 -7
  178. data/spec/unit/subclassing_multiple_spec.rb +37 -2
  179. data/spec/unit/subclassing_spec.rb +17 -2
  180. data/spec/unit/timestamps_spec.rb +32 -0
  181. data/spec/unit/transition_spec.rb +1 -1
  182. data/test/minitest_helper.rb +57 -0
  183. data/test/unit/minitest_matcher_test.rb +80 -0
  184. metadata +213 -37
  185. data/callbacks.txt +0 -51
  186. data/gemfiles/rails_3.2_stable.gemfile +0 -15
  187. data/gemfiles/rails_4.0.gemfile +0 -16
  188. data/gemfiles/rails_4.0_mongo_mapper.gemfile +0 -16
  189. data/gemfiles/rails_4.2_mongo_mapper.gemfile +0 -17
  190. data/lib/aasm/persistence/mongo_mapper_persistence.rb +0 -163
  191. data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +0 -21
  192. data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +0 -25
  193. data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +0 -149
  194. data/spec/unit/persistence/mongo_mapper_persistence_spec.rb +0 -96
@@ -1,4 +1,4 @@
1
- db = Sequel.connect(SEQUEL_DB)
1
+ db = Sequel::DATABASES.first || Sequel.connect(SEQUEL_DB)
2
2
 
3
3
  # if you want to see the statements while running the spec enable the following line
4
4
  # db.loggers << Logger.new($stderr)
@@ -6,20 +6,20 @@ db.create_table(:multiples) do
6
6
  primary_key :id
7
7
  String :status
8
8
  end
9
+ module Sequel
10
+ class Multiple < Sequel::Model(:multiples)
11
+ include AASM
9
12
 
10
- class SequelMultiple < Sequel::Model(db)
11
- set_dataset(:multiples)
12
- include AASM
13
+ attr_accessor :default
13
14
 
14
- attr_accessor :default
15
-
16
- aasm :left, :column => :status
17
- aasm :left do
18
- state :alpha, :initial => true
19
- state :beta
20
- state :gamma
21
- event :release do
22
- transitions :from => [:alpha, :beta, :gamma], :to => :beta
15
+ aasm :left, :column => :status
16
+ aasm :left do
17
+ state :alpha, :initial => true
18
+ state :beta
19
+ state :gamma
20
+ event :release do
21
+ transitions :from => [:alpha, :beta, :gamma], :to => :beta
22
+ end
23
23
  end
24
24
  end
25
25
  end
@@ -1,4 +1,4 @@
1
- db = Sequel.connect(SEQUEL_DB)
1
+ db = Sequel::DATABASES.first || Sequel.connect(SEQUEL_DB)
2
2
 
3
3
  # if you want to see the statements while running the spec enable the following line
4
4
  # db.loggers << Logger.new($stderr)
@@ -7,19 +7,20 @@ db.create_table(:simples) do
7
7
  String :status
8
8
  end
9
9
 
10
- class SequelSimple < Sequel::Model(db)
11
- set_dataset(:simples)
12
- include AASM
10
+ module Sequel
11
+ class Simple < Sequel::Model(:simples)
12
+ include AASM
13
13
 
14
- attr_accessor :default
14
+ attr_accessor :default
15
15
 
16
- aasm :column => :status
17
- aasm do
18
- state :alpha, :initial => true
19
- state :beta
20
- state :gamma
21
- event :release do
22
- transitions :from => [:alpha, :beta, :gamma], :to => :beta
16
+ aasm :column => :status
17
+ aasm do
18
+ state :alpha, :initial => true
19
+ state :beta
20
+ state :gamma
21
+ event :release do
22
+ transitions :from => [:alpha, :beta, :gamma], :to => :beta
23
+ end
23
24
  end
24
25
  end
25
26
  end
@@ -0,0 +1,50 @@
1
+ db = Sequel::DATABASES.first || Sequel.connect(SEQUEL_DB)
2
+
3
+ [:silent_persistors, :multiple_silent_persistors].each do |t|
4
+ db.create_table(t) do
5
+ primary_key :id
6
+ String "name"
7
+ String "status"
8
+ end
9
+ end
10
+
11
+ module Sequel
12
+ class SilentPersistor < Sequel::Model(:silent_persistors)
13
+ plugin :validation_helpers
14
+
15
+ include AASM
16
+ aasm :column => :status, :whiny_persistence => false do
17
+ state :sleeping, :initial => true
18
+ state :running
19
+ event :run do
20
+ transitions :to => :running, :from => :sleeping
21
+ end
22
+ event :sleep do
23
+ transitions :to => :sleeping, :from => :running
24
+ end
25
+ end
26
+ def validate
27
+ validates_presence :name
28
+ end
29
+ end
30
+
31
+ class MultipleSilentPersistor< Sequel::Model(:multiple_silent_persistors)
32
+ plugin :validation_helpers
33
+
34
+ include AASM
35
+ aasm :left, :column => :status, :whiny_persistence => false do
36
+ state :sleeping, :initial => true
37
+ state :running
38
+ event :run do
39
+ transitions :to => :running, :from => :sleeping
40
+ end
41
+ event :sleep do
42
+ transitions :to => :sleeping, :from => :running
43
+ end
44
+ end
45
+
46
+ def validate
47
+ validates_presence :name
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,112 @@
1
+ db = Sequel::DATABASES.first || Sequel.connect(SEQUEL_DB)
2
+
3
+ [:transactors, :no_lock_transactors, :lock_transactors, :lock_no_wait_transactors, :multiple_transactors].each do |table_name|
4
+ db.create_table(table_name) do
5
+ primary_key :id
6
+ String "name"
7
+ String "status"
8
+ Fixnum "worker_id"
9
+ end
10
+ end
11
+
12
+ module Sequel
13
+ class Transactor < Sequel::Model(:transactors)
14
+
15
+ many_to_one :worker, class: 'Sequel::Worker'
16
+
17
+ include AASM
18
+ aasm :column => :status do
19
+ state :sleeping, :initial => true
20
+ state :running, :before_enter => :start_worker, :after_enter => :fail
21
+
22
+ event :run do
23
+ transitions :to => :running, :from => :sleeping
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def start_worker
30
+ worker.update(status: 'running')
31
+ end
32
+
33
+ def fail
34
+ raise StandardError.new('failed on purpose')
35
+ end
36
+
37
+ end
38
+
39
+ class NoLockTransactor < Sequel::Model(:no_lock_transactors)
40
+
41
+ many_to_one :worker, class: 'Sequel::Worker'
42
+
43
+ include AASM
44
+
45
+ aasm :column => :status do
46
+ state :sleeping, :initial => true
47
+ state :running
48
+
49
+ event :run do
50
+ transitions :to => :running, :from => :sleeping
51
+ end
52
+ end
53
+ end
54
+
55
+ class LockTransactor < Sequel::Model(:lock_transactors)
56
+
57
+ many_to_one :worker, class: 'Sequel::Worker'
58
+
59
+ include AASM
60
+
61
+ aasm :column => :status, requires_lock: true do
62
+ state :sleeping, :initial => true
63
+ state :running
64
+
65
+ event :run do
66
+ transitions :to => :running, :from => :sleeping
67
+ end
68
+ end
69
+ end
70
+
71
+ class LockNoWaitTransactor < Sequel::Model(:lock_no_wait_transactors)
72
+
73
+ many_to_one :worker, class: 'Sequel::Worker'
74
+
75
+ include AASM
76
+
77
+ aasm :column => :status, requires_lock: 'FOR UPDATE NOWAIT' do
78
+ state :sleeping, :initial => true
79
+ state :running
80
+
81
+ event :run do
82
+ transitions :to => :running, :from => :sleeping
83
+ end
84
+ end
85
+ end
86
+
87
+ class MultipleTransactor < Sequel::Model(:multiple_transactors)
88
+
89
+ many_to_one :worker, class: 'Sequel::Worker'
90
+
91
+ include AASM
92
+ aasm :left, :column => :status do
93
+ state :sleeping, :initial => true
94
+ state :running, :before_enter => :start_worker, :after_enter => :fail
95
+
96
+ event :run do
97
+ transitions :to => :running, :from => :sleeping
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def start_worker
104
+ worker.update(:status, 'running')
105
+ end
106
+
107
+ def fail
108
+ raise StandardError.new('failed on purpose')
109
+ end
110
+
111
+ end
112
+ end
@@ -0,0 +1,93 @@
1
+ db = Sequel::DATABASES.first || Sequel.connect(SEQUEL_DB)
2
+
3
+ db.create_table(:validators) do
4
+ primary_key :id
5
+ String "name"
6
+ String "status"
7
+ Fixnum "worker_id"
8
+ end
9
+
10
+ module Sequel
11
+ class Validator < Sequel::Model(:validators)
12
+ plugin :validation_helpers
13
+
14
+ attr_accessor :after_all_transactions_performed,
15
+ :after_transaction_performed_on_fail,
16
+ :after_transaction_performed_on_run,
17
+ :before_all_transactions_performed,
18
+ :before_transaction_performed_on_fail,
19
+ :before_transaction_performed_on_run,
20
+ :invalid
21
+
22
+ def validate
23
+ super
24
+ errors.add(:validator, "invalid") if invalid
25
+ validates_presence :name
26
+ end
27
+
28
+ include AASM
29
+
30
+ aasm :column => :status, :whiny_persistence => true do
31
+ before_all_transactions :before_all_transactions
32
+ after_all_transactions :after_all_transactions
33
+
34
+ state :sleeping, :initial => true
35
+ state :running
36
+ state :failed, :after_enter => :fail
37
+
38
+ event :run, :after_commit => :change_name! do
39
+ after_transaction do
40
+ @after_transaction_performed_on_run = true
41
+ end
42
+
43
+ before_transaction do
44
+ @before_transaction_performed_on_run = true
45
+ end
46
+
47
+ transitions :to => :running, :from => :sleeping
48
+ end
49
+
50
+ event :sleep do
51
+ after_commit do |name|
52
+ change_name_on_sleep name
53
+ end
54
+ transitions :to => :sleeping, :from => :running
55
+ end
56
+
57
+ event :fail do
58
+ after_transaction do
59
+ @after_transaction_performed_on_fail = true
60
+ end
61
+
62
+ before_transaction do
63
+ @before_transaction_performed_on_fail = true
64
+ end
65
+
66
+ transitions :to => :failed, :from => [:sleeping, :running]
67
+ end
68
+ end
69
+
70
+ def change_name!
71
+ self.name = "name changed"
72
+ save(raise_on_failure: true)
73
+ end
74
+
75
+ def change_name_on_sleep name
76
+ self.name = name
77
+ save(raise_on_failure: true)
78
+ end
79
+
80
+ def fail
81
+ raise StandardError.new('failed on purpose')
82
+ end
83
+
84
+ def after_all_transactions
85
+ @after_all_transactions_performed = true
86
+ end
87
+
88
+ def before_all_transactions
89
+ @before_all_transactions_performed = true
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,12 @@
1
+ db = Sequel::DATABASES.first || Sequel.connect(SEQUEL_DB)
2
+
3
+ db.create_table(:workers) do
4
+ primary_key :id
5
+ String "name"
6
+ String "status"
7
+ end
8
+
9
+ module Sequel
10
+ class Worker < Sequel::Model(:workers)
11
+ end
12
+ end
@@ -3,13 +3,21 @@ class SimpleExample
3
3
  aasm do
4
4
  state :initialised, :initial => true
5
5
  state :filled_out
6
+ state :denied
6
7
  state :authorised
7
8
 
8
9
  event :fill_out do
9
10
  transitions :from => :initialised, :to => :filled_out
10
11
  end
12
+
13
+ event :deny do
14
+ transitions from: :initialised, to: :denied
15
+ end
16
+
11
17
  event :authorise do
12
18
  transitions :from => :filled_out, :to => :authorised
13
19
  end
20
+
14
21
  end
22
+
15
23
  end
@@ -0,0 +1,17 @@
1
+ class SimpleExampleWithGuardArgs
2
+ include AASM
3
+ aasm do
4
+ state :initialised, :initial => true
5
+ state :filled_out_with_args
6
+
7
+ event :fill_out_with_args do
8
+ transitions :guard => [:arg_is_valid?],
9
+ :from => :initialised,
10
+ :to => :filled_out_with_args
11
+ end
12
+ end
13
+
14
+ def arg_is_valid?(arg)
15
+ return arg
16
+ end
17
+ end
@@ -27,4 +27,16 @@ class SimpleMultipleExample
27
27
  transitions :from => :processing, :to => :sleeping
28
28
  end
29
29
  end
30
+
31
+ aasm(:question) do
32
+ state :answered, :initial => true
33
+ state :asked
34
+
35
+ event :ask, :binding_event => :start do
36
+ transitions :from => :answered, :to => :asked
37
+ end
38
+ event :answer, :binding_event => :stop do
39
+ transitions :from => :asked, :to => :answered
40
+ end
41
+ end
30
42
  end
@@ -1,7 +1,41 @@
1
1
  require_relative 'super_class'
2
2
 
3
3
  class SubClass < SuperClass
4
+ # Add an after callback that is not defined in the parent
5
+ aasm.state_machine.events[:foo].options[:after] = [:after_foo_event]
6
+
7
+ # Add global callback that is not defined in the parent
8
+ aasm.state_machine.global_callbacks[:after_all_transitions] = :after_all_event
9
+
10
+ attr_accessor :called_after
11
+
12
+ def after_foo_event
13
+ self.called_after = true
14
+ end
15
+
16
+ def after_all_event; end
4
17
  end
5
18
 
6
19
  class SubClassMultiple < SuperClassMultiple
20
+ # Add after callbacks that are not defined in the parent
21
+ aasm(:left).state_machine.events[:foo].options[:after] = [:left_after_foo_event]
22
+ aasm(:right).state_machine.events[:close].options[:after] = [:right_after_close_event]
23
+
24
+ # Add global callback that is not defined in the parent
25
+ aasm(:left).state_machine.global_callbacks[:after_all_transitions] = :left_after_all_event
26
+ aasm(:right).state_machine.global_callbacks[:after_all_transitions] = :right_after_all_event
27
+
28
+ attr_accessor :left_called_after, :right_called_after
29
+
30
+ def left_after_foo_event
31
+ self.left_called_after = true
32
+ end
33
+
34
+ def right_after_close_event
35
+ self.right_called_after = true
36
+ end
37
+
38
+ def left_after_all_event; end
39
+
40
+ def right_after_all_event; end
7
41
  end
@@ -0,0 +1,19 @@
1
+ class TimestampsExample
2
+ include AASM
3
+
4
+ attr_accessor :opened_at
5
+ attr_reader :closed_at
6
+
7
+ aasm timestamps: true do
8
+ state :opened
9
+ state :closed
10
+
11
+ event :open do
12
+ transitions to: :opened
13
+ end
14
+
15
+ event :close do
16
+ transitions to: :closed
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ class TimestampsWithNamedMachineExample
2
+ include AASM
3
+
4
+ attr_accessor :opened_at
5
+
6
+ aasm :my_state, timestamps: true do
7
+ state :opened
8
+
9
+ event :open do
10
+ transitions to: :opened
11
+ end
12
+ end
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,19 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter '/spec/'
4
+ end
5
+
6
+ if ENV['CI'] == 'true'
7
+ require 'codecov'
8
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
9
+ end
10
+
1
11
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
2
12
  $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
13
  require 'aasm'
4
14
  require 'rspec'
5
15
  require 'aasm/rspec'
16
+ require 'i18n'
6
17
  require 'pry'
7
18
 
8
19
  # require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying'
@@ -19,41 +30,12 @@ def load_schema
19
30
  require File.dirname(__FILE__) + "/database.rb"
20
31
  end
21
32
 
22
- # Dynamoid initialization
23
- begin
24
- require 'dynamoid'
25
- require 'aws-sdk-resources'
26
-
27
- ENV['ACCESS_KEY'] ||= 'abcd'
28
- ENV['SECRET_KEY'] ||= '1234'
29
-
30
- Aws.config.update({
31
- region: 'us-west-2',
32
- credentials: Aws::Credentials.new(ENV['ACCESS_KEY'], ENV['SECRET_KEY'])
33
- })
34
-
35
- Dynamoid.configure do |config|
36
- config.namespace = "dynamoid_tests"
37
- config.endpoint = 'http://127.0.0.1:30180'
38
- config.warn_on_scan = false
39
- end
40
-
41
- Dynamoid.logger.level = Logger::FATAL
42
-
43
- RSpec.configure do |c|
44
- c.before(:each) do
45
- Dynamoid.adapter.list_tables.each do |table|
46
- Dynamoid.adapter.delete_table(table) if table =~ /^#{Dynamoid::Config.namespace}/
47
- end
48
- Dynamoid.adapter.tables.clear
49
- end
50
- end
51
- rescue LoadError
52
- # Without Dynamoid settings
53
- end
54
-
55
33
  # custom spec helpers
56
34
  Dir[File.dirname(__FILE__) + "/spec_helpers/**/*.rb"].sort.each { |f| require File.expand_path(f) }
57
35
 
58
36
  # example model classes
59
37
  Dir[File.dirname(__FILE__) + "/models/*.rb"].sort.each { |f| require File.expand_path(f) }
38
+
39
+ I18n.load_path << 'spec/en.yml'
40
+ I18n.enforce_available_locales = false
41
+ I18n.default_locale = :en
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'active_record'
4
+
5
+ puts "active_record #{ActiveRecord::VERSION::STRING} gem found, running ActiveRecord specs \e[32m#{'✔'}\e[0m"
6
+ rescue LoadError
7
+ puts "active_record gem not found, not running ActiveRecord specs \e[31m#{'✖'}\e[0m"
8
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'dynamoid'
5
+ require 'aws-sdk-resources'
6
+ puts "dynamoid #{Dynamoid::VERSION} gem found, running Dynamoid specs \e[32m#{'✔'}\e[0m"
7
+
8
+ ENV['ACCESS_KEY'] ||= 'abcd'
9
+ ENV['SECRET_KEY'] ||= '1234'
10
+
11
+ Aws.config.update(
12
+ region: 'us-west-2',
13
+ credentials: Aws::Credentials.new(ENV['ACCESS_KEY'], ENV['SECRET_KEY'])
14
+ )
15
+
16
+ Dynamoid.configure do |config|
17
+ config.namespace = 'dynamoid_tests'
18
+ config.endpoint = "http://#{ENV['DYNAMODB_HOST'] || '127.0.0.1'}:" \
19
+ "#{ENV['DYNAMODB_PORT'] || 30180}"
20
+ config.warn_on_scan = false
21
+ end
22
+
23
+ Dynamoid.logger.level = Logger::FATAL
24
+
25
+ RSpec.configure do |c|
26
+ c.before(:each) do
27
+ Dynamoid.adapter.list_tables.each do |table|
28
+ Dynamoid.adapter.delete_table(table) if table =~ /^#{Dynamoid::Config.namespace}/
29
+ end
30
+ Dynamoid.adapter.tables.clear
31
+ end
32
+ end
33
+ rescue LoadError
34
+ puts "dynamoid gem not found, not running Dynamoid specs \e[31m#{'✖'}\e[0m"
35
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'mongoid'
5
+ puts "mongoid #{Mongoid::VERSION} gem found, running mongoid specs \e[32m#{'✔'}\e[0m"
6
+
7
+ if Mongoid::VERSION.to_f <= 5
8
+ Mongoid::Config.sessions = {
9
+ default: {
10
+ database: "mongoid_#{Process.pid}",
11
+ hosts: ["#{ENV['MONGODB_HOST'] || 'localhost'}:" \
12
+ "#{ENV['MONGODB_PORT'] || 27017}"]
13
+ }
14
+ }
15
+ else
16
+ Mongoid::Config.send(:clients=, {
17
+ default: {
18
+ database: "mongoid_#{Process.pid}",
19
+ hosts: ["#{ENV['MONGODB_HOST'] || 'localhost'}:" \
20
+ "#{ENV['MONGODB_PORT'] || 27017}"]
21
+ }
22
+ })
23
+ end
24
+ rescue LoadError
25
+ puts "mongoid gem not found, not running mongoid specs \e[31m#{'✖'}\e[0m"
26
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'nobrainer'
5
+
6
+ NoBrainer.configure do |config|
7
+ config.app_name = :aasm
8
+ config.environment = :test
9
+ config.warn_on_active_record = false
10
+ end
11
+
12
+ puts "nobrainer #{Gem.loaded_specs['nobrainer'].version} gem found, running nobrainer specs \e[32m#{'✔'}\e[0m"
13
+ rescue LoadError
14
+ puts "nobrainer gem not found, not running nobrainer specs \e[31m#{'✖'}\e[0m"
15
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'redis-objects'
5
+ require 'redis/objects/version'
6
+ puts "redis-objects #{Redis::Objects::VERSION} gem found, running Redis specs \e[32m#{'✔'}\e[0m"
7
+
8
+ Redis.current = Redis.new(host: (ENV['REDIS_HOST'] || '127.0.0.1'),
9
+ port: (ENV['REDIS_PORT'] || 6379))
10
+
11
+ RSpec.configure do |c|
12
+ c.before(:each) do
13
+ Redis.current.keys('redis_*').each { |k| Redis.current.del k }
14
+ end
15
+ end
16
+ rescue LoadError
17
+ puts "redis-objects gem not found, not running Redis specs \e[31m#{'✖'}\e[0m"
18
+ end
@@ -0,0 +1 @@
1
+ AASM::Configuration.hide_warnings = true
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'sequel'
4
+ puts "sequel #{Sequel::VERSION} gem found, running Sequel specs \e[32m#{'✔'}\e[0m"
5
+ rescue LoadError
6
+ puts "sequel gem not found, not running Sequel specs \e[31m#{'✖'}\e[0m"
7
+ end