aasm 4.12.0 → 4.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -1
  3. data/Appraisals +2 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +1 -1
  6. data/README.md +102 -8
  7. data/gemfiles/rails_4.2.gemfile +1 -0
  8. data/gemfiles/rails_5.0.gemfile +1 -0
  9. data/lib/aasm/base.rb +8 -0
  10. data/lib/aasm/core/event.rb +1 -1
  11. data/lib/aasm/core/transition.rb +1 -1
  12. data/lib/aasm/instance_base.rb +0 -2
  13. data/lib/aasm/minitest.rb +5 -0
  14. data/lib/aasm/minitest/allow_event.rb +13 -0
  15. data/lib/aasm/minitest/allow_transition_to.rb +13 -0
  16. data/lib/aasm/minitest/have_state.rb +13 -0
  17. data/lib/aasm/minitest/transition_from.rb +21 -0
  18. data/lib/aasm/minitest_spec.rb +15 -0
  19. data/lib/aasm/persistence/active_record_persistence.rb +25 -101
  20. data/lib/aasm/persistence/base.rb +7 -3
  21. data/lib/aasm/persistence/mongoid_persistence.rb +15 -60
  22. data/lib/aasm/persistence/orm.rb +142 -0
  23. data/lib/aasm/persistence/redis_persistence.rb +16 -11
  24. data/lib/aasm/persistence/sequel_persistence.rb +36 -63
  25. data/lib/aasm/version.rb +1 -1
  26. data/lib/generators/active_record/templates/migration.rb +1 -1
  27. data/lib/generators/active_record/templates/migration_existing.rb +1 -1
  28. data/lib/motion-aasm.rb +2 -0
  29. data/spec/models/active_record/complex_active_record_example.rb +5 -1
  30. data/spec/models/guardian_without_from_specified.rb +18 -0
  31. data/spec/models/namespaced_multiple_example.rb +14 -0
  32. data/spec/models/redis/complex_redis_example.rb +40 -0
  33. data/spec/models/redis/redis_multiple.rb +20 -0
  34. data/spec/models/redis/redis_simple.rb +20 -0
  35. data/spec/models/sequel/complex_sequel_example.rb +4 -3
  36. data/spec/models/sequel/invalid_persistor.rb +52 -0
  37. data/spec/models/sequel/sequel_multiple.rb +13 -13
  38. data/spec/models/sequel/sequel_simple.rb +13 -12
  39. data/spec/models/sequel/silent_persistor.rb +50 -0
  40. data/spec/models/sequel/transactor.rb +112 -0
  41. data/spec/models/sequel/validator.rb +93 -0
  42. data/spec/models/sequel/worker.rb +12 -0
  43. data/spec/spec_helpers/redis.rb +8 -0
  44. data/spec/unit/guard_spec.rb +17 -0
  45. data/spec/unit/guard_without_from_specified_spec.rb +10 -0
  46. data/spec/unit/namespaced_multiple_example_spec.rb +22 -0
  47. data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +4 -4
  48. data/spec/unit/persistence/active_record_persistence_spec.rb +4 -4
  49. data/spec/unit/persistence/redis_persistence_multiple_spec.rb +88 -0
  50. data/spec/unit/persistence/redis_persistence_spec.rb +5 -25
  51. data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +2 -2
  52. data/spec/unit/persistence/sequel_persistence_spec.rb +275 -2
  53. data/test/minitest_helper.rb +57 -0
  54. data/test/unit/minitest_matcher_test.rb +80 -0
  55. metadata +35 -2
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "4.12.0"
2
+ VERSION = "4.12.1"
3
3
  end
@@ -1,4 +1,4 @@
1
- class AASMCreate<%= table_name.camelize %> < ActiveRecord::Migration
1
+ class AASMCreate<%= table_name.camelize %> < ActiveRecord::Migration[<%= ActiveRecord::VERSION::STRING.to_f %>]
2
2
  def change
3
3
  create_table(:<%= table_name %>) do |t|
4
4
  t.string :<%= column_name %>
@@ -1,4 +1,4 @@
1
- class Add<%= column_name.camelize %>To<%= table_name.camelize %> < ActiveRecord::Migration
1
+ class Add<%= column_name.camelize %>To<%= table_name.camelize %> < ActiveRecord::Migration[<%= ActiveRecord::VERSION::STRING.to_f %>]
2
2
  def change
3
3
  add_column :<%= table_name %>, :<%= column_name %>, :string
4
4
  end
@@ -11,6 +11,8 @@ file_dependencies = {
11
11
 
12
12
  exclude_files = [
13
13
  'aasm/rspec.*',
14
+ 'aasm/minitest.*',
15
+ 'aasm/minitest_spec.*',
14
16
  'aasm/persistence/active_record_persistence.rb',
15
17
  'aasm/persistence/dynamoid_persistence.rb',
16
18
  'aasm/persistence/mongoid_persistence.rb',
@@ -7,7 +7,7 @@ class ComplexActiveRecordExample < ActiveRecord::Base
7
7
  state :three
8
8
 
9
9
  event :increment do
10
- transitions :from => :one, :to => :two
10
+ transitions :from => :one, :to => :two, guard: :allowed?
11
11
  transitions :from => :two, :to => :three
12
12
  end
13
13
  event :reset do
@@ -15,6 +15,10 @@ class ComplexActiveRecordExample < ActiveRecord::Base
15
15
  end
16
16
  end
17
17
 
18
+ def allowed?
19
+ true
20
+ end
21
+
18
22
  aasm :right, :column => 'right' do
19
23
  state :alpha, :initial => true
20
24
  state :beta
@@ -0,0 +1,18 @@
1
+ class GuardianWithoutFromSpecified
2
+ include AASM
3
+
4
+ aasm do
5
+ state :alpha, :initial => true
6
+ state :beta
7
+ state :gamma
8
+
9
+ event :use_guards_where_the_first_fails do
10
+ transitions :to => :beta, :guard => :fail
11
+ transitions :to => :gamma, :guard => :succeed
12
+ end
13
+ end
14
+
15
+
16
+ def fail; false; end
17
+ def succeed; true; end
18
+ end
@@ -25,4 +25,18 @@ class NamespacedMultipleExample
25
25
  transitions :from => :approved, :to => :unapproved
26
26
  end
27
27
  end
28
+
29
+ # Test namespace event methods with suffix (ie may_sell_car?, sell_car!)
30
+ aasm(:car, namespace: :car) do
31
+ state :unsold, :initial => true
32
+ state :sold
33
+
34
+ event :sell do
35
+ transitions :from => :unsold, :to => :sold
36
+ end
37
+
38
+ event :return do
39
+ transitions :from => :sold, :to => :unsold
40
+ end
41
+ end
28
42
  end
@@ -0,0 +1,40 @@
1
+ class RedisComplexExample
2
+ include Redis::Objects
3
+ include AASM
4
+
5
+ value :left
6
+ value :right
7
+
8
+ def id
9
+ 1
10
+ end
11
+
12
+ aasm :left, :column => 'left' do
13
+ state :one, :initial => true
14
+ state :two
15
+ state :three
16
+
17
+ event :increment do
18
+ transitions :from => :one, :to => :two
19
+ transitions :from => :two, :to => :three
20
+ end
21
+ event :reset do
22
+ transitions :from => :three, :to => :one
23
+ end
24
+ end
25
+
26
+ aasm :right, :column => 'right' do
27
+ state :alpha, :initial => true
28
+ state :beta
29
+ state :gamma
30
+
31
+ event :level_up do
32
+ transitions :from => :alpha, :to => :beta
33
+ transitions :from => :beta, :to => :gamma
34
+ end
35
+ event :level_down do
36
+ transitions :from => :gamma, :to => :beta
37
+ transitions :from => :beta, :to => :alpha
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ class RedisMultiple
2
+ include Redis::Objects
3
+ include AASM
4
+
5
+ value :status
6
+
7
+ def id
8
+ 1
9
+ end
10
+
11
+ aasm :left, :column => :status
12
+ aasm :left do
13
+ state :alpha, :initial => true
14
+ state :beta
15
+ state :gamma
16
+ event :release do
17
+ transitions :from => [:alpha, :beta, :gamma], :to => :beta
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ class RedisSimple
2
+ include Redis::Objects
3
+ include AASM
4
+
5
+ value :status
6
+
7
+ def id
8
+ 1
9
+ end
10
+
11
+ aasm :column => :status
12
+ aasm do
13
+ state :alpha, :initial => true
14
+ state :beta
15
+ state :gamma
16
+ event :release do
17
+ transitions :from => [:alpha, :beta, :gamma], :to => :beta
18
+ end
19
+ end
20
+ 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)
@@ -8,8 +8,8 @@ db.create_table(:complex_sequel_examples) do
8
8
  String :right
9
9
  end
10
10
 
11
- class ComplexSequelExample < Sequel::Model(db)
12
- set_dataset(:complex_sequel_examples)
11
+ module Sequel
12
+ class ComplexExample < Sequel::Model(:complex_sequel_examples)
13
13
 
14
14
  include AASM
15
15
 
@@ -43,3 +43,4 @@ class ComplexSequelExample < Sequel::Model(db)
43
43
  end
44
44
 
45
45
  end
46
+ end
@@ -0,0 +1,52 @@
1
+ db = Sequel::DATABASES.first || Sequel.connect(SEQUEL_DB)
2
+
3
+ [:invalid_persistors, :multiple_invalid_persistors].each do |table_name|
4
+ db.create_table(table_name) do
5
+ primary_key :id
6
+ String "name"
7
+ String "status"
8
+ end
9
+ end
10
+
11
+ module Sequel
12
+ class InvalidPersistor < Sequel::Model(:invalid_persistors)
13
+ plugin :validation_helpers
14
+
15
+ include AASM
16
+ aasm :column => :status, :skip_validation_on_save => true 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
+
27
+ def validate
28
+ super
29
+ validates_presence :name
30
+ end
31
+ end
32
+
33
+ class MultipleInvalidPersistor < Sequel::Model(:multiple_invalid_persistors)
34
+ plugin :validation_helpers
35
+
36
+ include AASM
37
+ aasm :left, :column => :status, :skip_validation_on_save => true do
38
+ state :sleeping, :initial => true
39
+ state :running
40
+ event :run do
41
+ transitions :to => :running, :from => :sleeping
42
+ end
43
+ event :sleep do
44
+ transitions :to => :sleeping, :from => :running
45
+ end
46
+ end
47
+ def validate
48
+ super
49
+ validates_presence :name
50
+ end
51
+ end
52
+ 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)
@@ -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