aasm 4.12.3 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +14 -12
  3. data/Appraisals +39 -16
  4. data/CHANGELOG.md +16 -3
  5. data/Dockerfile +44 -0
  6. data/Gemfile +1 -2
  7. data/README.md +41 -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 +6 -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/active_record_persistence.rb +5 -4
  30. data/lib/aasm/persistence/no_brainer_persistence.rb +105 -0
  31. data/lib/aasm/persistence/plain_persistence.rb +2 -1
  32. data/lib/aasm/persistence/sequel_persistence.rb +0 -1
  33. data/lib/aasm/rspec/allow_event.rb +5 -1
  34. data/lib/aasm/rspec/allow_transition_to.rb +5 -1
  35. data/lib/aasm/version.rb +1 -1
  36. data/lib/generators/nobrainer/aasm_generator.rb +28 -0
  37. data/lib/motion-aasm.rb +1 -0
  38. data/spec/database.rb +3 -0
  39. data/spec/generators/no_brainer_generator_spec.rb +29 -0
  40. data/spec/models/active_record/simple_new_dsl.rb +15 -0
  41. data/spec/models/nobrainer/complex_no_brainer_example.rb +36 -0
  42. data/spec/models/nobrainer/invalid_persistor_no_brainer.rb +39 -0
  43. data/spec/models/nobrainer/no_scope_no_brainer.rb +21 -0
  44. data/spec/models/nobrainer/nobrainer_relationships.rb +25 -0
  45. data/spec/models/nobrainer/silent_persistor_no_brainer.rb +39 -0
  46. data/spec/models/nobrainer/simple_new_dsl_nobrainer.rb +25 -0
  47. data/spec/models/nobrainer/simple_no_brainer.rb +23 -0
  48. data/spec/models/nobrainer/validator_no_brainer.rb +98 -0
  49. data/spec/models/simple_example.rb +2 -0
  50. data/spec/models/simple_example_with_guard_args.rb +17 -0
  51. data/spec/spec_helpers/active_record.rb +2 -1
  52. data/spec/spec_helpers/dynamoid.rb +7 -5
  53. data/spec/spec_helpers/mongoid.rb +20 -1
  54. data/spec/spec_helpers/nobrainer.rb +15 -0
  55. data/spec/spec_helpers/redis.rb +5 -2
  56. data/spec/spec_helpers/sequel.rb +1 -1
  57. data/spec/unit/callback_multiple_spec.rb +3 -3
  58. data/spec/unit/callbacks_spec.rb +2 -2
  59. data/spec/unit/exception_spec.rb +1 -1
  60. data/spec/unit/invoker_spec.rb +189 -0
  61. data/spec/unit/invokers/base_invoker_spec.rb +72 -0
  62. data/spec/unit/invokers/class_invoker_spec.rb +95 -0
  63. data/spec/unit/invokers/literal_invoker_spec.rb +86 -0
  64. data/spec/unit/invokers/proc_invoker_spec.rb +86 -0
  65. data/spec/unit/persistence/active_record_persistence_spec.rb +16 -0
  66. data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +0 -4
  67. data/spec/unit/persistence/mongoid_persistence_spec.rb +0 -4
  68. data/spec/unit/persistence/no_brainer_persistence_multiple_spec.rb +198 -0
  69. data/spec/unit/persistence/no_brainer_persistence_spec.rb +158 -0
  70. data/spec/unit/rspec_matcher_spec.rb +6 -0
  71. data/spec/unit/state_spec.rb +2 -2
  72. data/spec/unit/transition_spec.rb +1 -1
  73. data/test/minitest_helper.rb +2 -2
  74. data/test/unit/minitest_matcher_test.rb +1 -1
  75. metadata +51 -3
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(NoBrainer::Document)
4
+ require 'generator_spec'
5
+ require 'generators/nobrainer/aasm_generator'
6
+
7
+ describe NoBrainer::Generators::AASMGenerator, type: :generator do
8
+ destination File.expand_path('../../../tmp', __FILE__)
9
+
10
+ before(:all) do
11
+ prepare_destination
12
+ end
13
+
14
+ it 'creates model with aasm block for default column_name' do
15
+ run_generator %w[user]
16
+ assert_file 'app/models/user.rb', /include AASM\n\n aasm do\n end\n/
17
+ end
18
+
19
+ it 'creates model with aasm block for custom column_name' do
20
+ run_generator %w[user state]
21
+ assert_file 'app/models/user.rb', /aasm :column => 'state' do\n end\n/
22
+ end
23
+
24
+ it 'creates model with aasm block for namespaced model' do
25
+ run_generator %w[Admin::User state]
26
+ assert_file 'app/models/admin/user.rb', /aasm :column => 'state' do\n end\n/
27
+ end
28
+ end
29
+ end
@@ -15,3 +15,18 @@ class MultipleSimpleNewDsl < ActiveRecord::Base
15
15
  state :new
16
16
  end
17
17
  end
18
+
19
+ class AbstractClassDsl < ActiveRecord::Base
20
+ include AASM
21
+
22
+ self.abstract_class = true
23
+
24
+ aasm :column => :status
25
+ aasm do
26
+ state :unknown_scope, :another_unknown_scope
27
+ state :new
28
+ end
29
+ end
30
+
31
+ class ImplementedAbstractClassDsl < AbstractClassDsl
32
+ end
@@ -0,0 +1,36 @@
1
+ class ComplexNoBrainerExample
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :left, type: String
6
+ field :right, type: String
7
+
8
+ aasm :left, column: 'left' do
9
+ state :one, initial: true
10
+ state :two
11
+ state :three
12
+
13
+ event :increment do
14
+ transitions from: :one, to: :two
15
+ transitions from: :two, to: :three
16
+ end
17
+ event :reset do
18
+ transitions from: :three, to: :one
19
+ end
20
+ end
21
+
22
+ aasm :right, column: 'right' do
23
+ state :alpha, initial: true
24
+ state :beta
25
+ state :gamma
26
+
27
+ event :level_up do
28
+ transitions from: :alpha, to: :beta
29
+ transitions from: :beta, to: :gamma
30
+ end
31
+ event :level_down do
32
+ transitions from: :gamma, to: :beta
33
+ transitions from: :beta, to: :alpha
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ class InvalidPersistorNoBrainer
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :name
6
+ field :status
7
+
8
+ aasm :left, column: :status, skip_validation_on_save: true do
9
+ state :sleeping, initial: true
10
+ state :running
11
+ event :run do
12
+ transitions to: :running, from: :sleeping
13
+ end
14
+ event :sleep do
15
+ transitions to: :sleeping, from: :running
16
+ end
17
+ end
18
+ validates_presence_of :name
19
+ end
20
+
21
+ class MultipleInvalidPersistorNoBrainer
22
+ include NoBrainer::Document
23
+ include AASM
24
+
25
+ field :name
26
+ field :status
27
+
28
+ aasm :left, column: :status, skip_validation_on_save: true do
29
+ state :sleeping, initial: true
30
+ state :running
31
+ event :run do
32
+ transitions to: :running, from: :sleeping
33
+ end
34
+ event :sleep do
35
+ transitions to: :sleeping, from: :running
36
+ end
37
+ end
38
+ validates_presence_of :name
39
+ end
@@ -0,0 +1,21 @@
1
+ class NoScopeNoBrainer
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :status, type: String
6
+
7
+ aasm create_scopes: false, column: :status do
8
+ state :ignored_scope
9
+ end
10
+ end
11
+
12
+ class NoScopeNoBrainerMultiple
13
+ include NoBrainer::Document
14
+ include AASM
15
+
16
+ field :status, type: String
17
+
18
+ aasm :left, create_scopes: false, column: :status do
19
+ state :ignored_scope
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ class Parent
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :status, type: String
6
+ has_many :childs
7
+
8
+ aasm column: :status do
9
+ state :unknown_scope
10
+ state :new
11
+ end
12
+ end
13
+
14
+ class Child
15
+ include NoBrainer::Document
16
+ include AASM
17
+
18
+ field :status, type: String
19
+ belongs_to :parent
20
+
21
+ aasm column: :status do
22
+ state :unknown_scope
23
+ state :new
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ class SilentPersistorNoBrainer
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :name
6
+ field :status
7
+
8
+ aasm :left, column: :status, whiny_persistence: false do
9
+ state :sleeping, initial: true
10
+ state :running
11
+ event :run do
12
+ transitions to: :running, from: :sleeping
13
+ end
14
+ event :sleep do
15
+ transitions to: :sleeping, from: :running
16
+ end
17
+ end
18
+ validates_presence_of :name
19
+ end
20
+
21
+ class MultipleSilentPersistorNoBrainer
22
+ include NoBrainer::Document
23
+ include AASM
24
+
25
+ field :name
26
+ field :status
27
+
28
+ aasm :left, column: :status, whiny_persistence: false do
29
+ state :sleeping, initial: true
30
+ state :running
31
+ event :run do
32
+ transitions to: :running, from: :sleeping
33
+ end
34
+ event :sleep do
35
+ transitions to: :sleeping, from: :running
36
+ end
37
+ end
38
+ validates_presence_of :name
39
+ end
@@ -0,0 +1,25 @@
1
+ class SimpleNewDslNoBrainer
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :status, type: String
6
+
7
+ aasm column: :status
8
+ aasm do
9
+ state :unknown_scope, initial: true
10
+ state :new
11
+ end
12
+ end
13
+
14
+ class SimpleNewDslNoBrainerMultiple
15
+ include NoBrainer::Document
16
+ include AASM
17
+
18
+ field :status, type: String
19
+
20
+ aasm :left, column: :status
21
+ aasm :left do
22
+ state :unknown_scope, initial: true
23
+ state :new
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ class SimpleNoBrainer
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :status, type: String
6
+
7
+ aasm column: :status do
8
+ state :unknown_scope, :another_unknown_scope
9
+ state :new
10
+ end
11
+ end
12
+
13
+ class SimpleNoBrainerMultiple
14
+ include NoBrainer::Document
15
+ include AASM
16
+
17
+ field :status, type: String
18
+
19
+ aasm :left, column: :status do
20
+ state :unknown_scope, :another_unknown_scope
21
+ state :new
22
+ end
23
+ end
@@ -0,0 +1,98 @@
1
+ class ValidatorNoBrainer
2
+ include NoBrainer::Document
3
+ include AASM
4
+
5
+ field :name
6
+ field :status
7
+
8
+ attr_accessor :invalid
9
+
10
+ validate do |_|
11
+ errors.add(:validator, 'invalid') if invalid
12
+ end
13
+
14
+ aasm column: :status, whiny_persistence: true do
15
+ before_all_transactions :before_all_transactions
16
+ after_all_transactions :after_all_transactions
17
+
18
+ state :sleeping, initial: true
19
+ state :running
20
+ state :failed, after_enter: :fail
21
+
22
+ event :run, after_commit: :change_name! do
23
+ transitions to: :running, from: :sleeping
24
+ end
25
+
26
+ event :sleep do
27
+ after_commit do |name|
28
+ change_name_on_sleep name
29
+ end
30
+ transitions to: :sleeping, from: :running
31
+ end
32
+
33
+ event :fail do
34
+ transitions to: :failed, from: %i[sleeping running]
35
+ end
36
+ end
37
+
38
+ validates_presence_of :name
39
+
40
+ def change_name!
41
+ self.name = 'name changed'
42
+ save!
43
+ end
44
+
45
+ def change_name_on_sleep(name)
46
+ self.name = name
47
+ save!
48
+ end
49
+
50
+ def fail
51
+ raise StandardError, 'failed on purpose'
52
+ end
53
+ end
54
+
55
+ class MultipleValidatorNoBrainer
56
+ include NoBrainer::Document
57
+ include AASM
58
+
59
+ field :name
60
+ field :status
61
+
62
+ attr_accessor :invalid
63
+
64
+ aasm :left, column: :status, whiny_persistence: true do
65
+ state :sleeping, initial: true
66
+ state :running
67
+ state :failed, after_enter: :fail
68
+
69
+ event :run, after_commit: :change_name! do
70
+ transitions to: :running, from: :sleeping
71
+ end
72
+ event :sleep do
73
+ after_commit do |name|
74
+ change_name_on_sleep name
75
+ end
76
+ transitions to: :sleeping, from: :running
77
+ end
78
+ event :fail do
79
+ transitions to: :failed, from: %i[sleeping running]
80
+ end
81
+ end
82
+
83
+ validates_presence_of :name
84
+
85
+ def change_name!
86
+ self.name = 'name changed'
87
+ save!
88
+ end
89
+
90
+ def change_name_on_sleep(name)
91
+ self.name = name
92
+ save!
93
+ end
94
+
95
+ def fail
96
+ raise StandardError, 'failed on purpose'
97
+ end
98
+ end
@@ -11,5 +11,7 @@ class SimpleExample
11
11
  event :authorise do
12
12
  transitions :from => :filled_out, :to => :authorised
13
13
  end
14
+
14
15
  end
16
+
15
17
  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
@@ -1,7 +1,8 @@
1
1
  # encoding: utf-8
2
2
  begin
3
3
  require 'active_record'
4
- puts "active_record gem found, running ActiveRecord specs \e[32m#{'✔'}\e[0m"
4
+
5
+ puts "active_record #{ActiveRecord::VERSION::STRING} gem found, running ActiveRecord specs \e[32m#{'✔'}\e[0m"
5
6
  rescue LoadError
6
7
  puts "active_record gem not found, not running ActiveRecord specs \e[31m#{'✖'}\e[0m"
7
8
  end
@@ -1,20 +1,22 @@
1
1
  # encoding: utf-8
2
+
2
3
  begin
3
4
  require 'dynamoid'
4
5
  require 'aws-sdk-resources'
5
- puts "dynamoid gem found, running Dynamoid specs \e[32m#{'✔'}\e[0m"
6
+ puts "dynamoid #{Dynamoid::VERSION} gem found, running Dynamoid specs \e[32m#{'✔'}\e[0m"
6
7
 
7
8
  ENV['ACCESS_KEY'] ||= 'abcd'
8
9
  ENV['SECRET_KEY'] ||= '1234'
9
10
 
10
- Aws.config.update({
11
+ Aws.config.update(
11
12
  region: 'us-west-2',
12
13
  credentials: Aws::Credentials.new(ENV['ACCESS_KEY'], ENV['SECRET_KEY'])
13
- })
14
+ )
14
15
 
15
16
  Dynamoid.configure do |config|
16
- config.namespace = "dynamoid_tests"
17
- config.endpoint = 'http://127.0.0.1:30180'
17
+ config.namespace = 'dynamoid_tests'
18
+ config.endpoint = "http://#{ENV['DYNAMODB_HOST'] || '127.0.0.1'}:" \
19
+ "#{ENV['DYNAMODB_PORT'] || 30180}"
18
20
  config.warn_on_scan = false
19
21
  end
20
22
 
@@ -1,7 +1,26 @@
1
1
  # encoding: utf-8
2
+
2
3
  begin
3
4
  require 'mongoid'
4
- puts "mongoid gem found, running mongoid specs \e[32m#{'✔'}\e[0m"
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
5
24
  rescue LoadError
6
25
  puts "mongoid gem not found, not running mongoid specs \e[31m#{'✖'}\e[0m"
7
26
  end