aasm 4.11.1 → 4.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +15 -16
  3. data/Appraisals +44 -0
  4. data/CHANGELOG.md +14 -0
  5. data/CONTRIBUTING.md +24 -0
  6. data/Gemfile +4 -21
  7. data/README.md +53 -32
  8. data/Rakefile +6 -1
  9. data/TESTING.md +25 -0
  10. data/aasm.gemspec +3 -0
  11. data/gemfiles/rails_3.2.gemfile +13 -0
  12. data/gemfiles/rails_4.0.gemfile +8 -9
  13. data/gemfiles/rails_4.2.gemfile +9 -9
  14. data/gemfiles/rails_4.2_mongoid_5.gemfile +5 -9
  15. data/gemfiles/rails_5.0.gemfile +7 -16
  16. data/lib/aasm/aasm.rb +9 -3
  17. data/lib/aasm/base.rb +3 -1
  18. data/lib/aasm/configuration.rb +4 -0
  19. data/lib/aasm/core/event.rb +17 -3
  20. data/lib/aasm/core/state.rb +7 -0
  21. data/lib/aasm/core/transition.rb +9 -0
  22. data/lib/aasm/persistence.rb +0 -3
  23. data/lib/aasm/persistence/active_record_persistence.rb +1 -1
  24. data/lib/aasm/persistence/mongoid_persistence.rb +48 -9
  25. data/lib/aasm/state_machine.rb +4 -2
  26. data/lib/aasm/state_machine_store.rb +5 -2
  27. data/lib/aasm/version.rb +1 -1
  28. data/lib/motion-aasm.rb +0 -1
  29. data/spec/generators/active_record_generator_spec.rb +42 -39
  30. data/spec/generators/mongoid_generator_spec.rb +4 -6
  31. data/spec/models/{invalid_persistor.rb → active_record/invalid_persistor.rb} +0 -2
  32. data/spec/models/{silent_persistor.rb → active_record/silent_persistor.rb} +0 -2
  33. data/spec/models/{transactor.rb → active_record/transactor.rb} +0 -2
  34. data/spec/models/{validator.rb → active_record/validator.rb} +0 -2
  35. data/spec/models/{worker.rb → active_record/worker.rb} +0 -0
  36. data/spec/models/callbacks/basic.rb +5 -2
  37. data/spec/models/guard_with_params.rb +1 -1
  38. data/spec/models/mongoid/invalid_persistor_mongoid.rb +39 -0
  39. data/spec/models/mongoid/silent_persistor_mongoid.rb +39 -0
  40. data/spec/models/mongoid/validator_mongoid.rb +100 -0
  41. data/spec/models/multiple_transitions_that_differ_only_by_guard.rb +31 -0
  42. data/spec/models/parametrised_event.rb +7 -0
  43. data/spec/models/simple_multiple_example.rb +12 -0
  44. data/spec/models/sub_class.rb +34 -0
  45. data/spec/spec_helper.rb +0 -33
  46. data/spec/spec_helpers/active_record.rb +7 -0
  47. data/spec/spec_helpers/dynamoid.rb +33 -0
  48. data/spec/spec_helpers/mongoid.rb +7 -0
  49. data/spec/spec_helpers/redis.rb +7 -0
  50. data/spec/spec_helpers/remove_warnings.rb +1 -0
  51. data/spec/spec_helpers/sequel.rb +7 -0
  52. data/spec/unit/api_spec.rb +76 -73
  53. data/spec/unit/callbacks_spec.rb +5 -0
  54. data/spec/unit/event_spec.rb +12 -0
  55. data/spec/unit/guard_with_params_spec.rb +4 -0
  56. data/spec/unit/localizer_spec.rb +55 -53
  57. data/spec/unit/multiple_transitions_that_differ_only_by_guard_spec.rb +14 -0
  58. data/spec/unit/override_warning_spec.rb +8 -0
  59. data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +452 -448
  60. data/spec/unit/persistence/active_record_persistence_spec.rb +523 -501
  61. data/spec/unit/persistence/dynamoid_persistence_multiple_spec.rb +4 -9
  62. data/spec/unit/persistence/dynamoid_persistence_spec.rb +4 -9
  63. data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +83 -9
  64. data/spec/unit/persistence/mongoid_persistence_spec.rb +85 -9
  65. data/spec/unit/persistence/redis_persistence_spec.rb +3 -7
  66. data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +4 -9
  67. data/spec/unit/persistence/sequel_persistence_spec.rb +4 -9
  68. data/spec/unit/simple_multiple_example_spec.rb +28 -0
  69. data/spec/unit/subclassing_multiple_spec.rb +37 -2
  70. data/spec/unit/subclassing_spec.rb +17 -2
  71. metadata +66 -28
  72. data/gemfiles/rails_3.2_stable.gemfile +0 -15
  73. data/gemfiles/rails_4.0_mongo_mapper.gemfile +0 -16
  74. data/gemfiles/rails_4.2_mongo_mapper.gemfile +0 -17
  75. data/lib/aasm/persistence/mongo_mapper_persistence.rb +0 -163
  76. data/spec/models/mongo_mapper/complex_mongo_mapper_example.rb +0 -37
  77. data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +0 -21
  78. data/spec/models/mongo_mapper/simple_mongo_mapper.rb +0 -23
  79. data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +0 -25
  80. data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +0 -149
  81. data/spec/unit/persistence/mongo_mapper_persistence_spec.rb +0 -96
@@ -1,5 +1,3 @@
1
- require 'active_record'
2
-
3
1
  class InvalidPersistor < ActiveRecord::Base
4
2
  include AASM
5
3
  aasm :column => :status, :skip_validation_on_save => true do
@@ -1,5 +1,3 @@
1
- require 'active_record'
2
-
3
1
  class SilentPersistor < ActiveRecord::Base
4
2
  include AASM
5
3
  aasm :column => :status, :whiny_persistence => false do
@@ -1,5 +1,3 @@
1
- require 'active_record'
2
-
3
1
  class Transactor < ActiveRecord::Base
4
2
 
5
3
  belongs_to :worker
@@ -1,5 +1,3 @@
1
- require 'active_record'
2
-
3
1
  class Validator < ActiveRecord::Base
4
2
  attr_accessor :after_all_transactions_performed,
5
3
  :after_transaction_performed_on_fail,
@@ -43,6 +43,7 @@ module Callbacks
43
43
  :before => :before_event,
44
44
  :after => :after_event,
45
45
  :guard => :event_guard,
46
+ :before_success => :event_before_success,
46
47
  :ensure => :ensure_event do
47
48
  transitions :to => :closed, :from => [:open],
48
49
  :guard => :transition_guard,
@@ -78,6 +79,8 @@ module Callbacks
78
79
  def event_guard; log('event_guard'); !@fail_event_guard; end
79
80
  def transition_guard; log('transition_guard'); !@fail_transition_guard; end
80
81
 
82
+ def event_before_success; log('event_before_success'); end
83
+
81
84
  def after_transition; log('after_transition'); end
82
85
  def after_all_transitions; log('after_all_transitions'); end
83
86
 
@@ -86,8 +89,8 @@ module Callbacks
86
89
  def after_event; log('after_event'); end
87
90
  def after_all_events; log('after_all_events'); end
88
91
 
89
- def after_transition; log('after_transition'); end
90
- def success_transition; log('transition_success'); end
92
+ def after_transition; log('after_transition'); end
93
+ def success_transition; log('transition_success'); end
91
94
 
92
95
  def ensure_event; log('ensure'); end
93
96
  def ensure_on_all_events; log('ensure'); end
@@ -10,7 +10,7 @@ class GuardWithParams
10
10
 
11
11
  def user_is_manager?(user)
12
12
  ok = false
13
- if user.has_role? :manager
13
+ if user && user.has_role?(:manager)
14
14
  ok = true
15
15
  end
16
16
  return ok
@@ -0,0 +1,39 @@
1
+ class InvalidPersistorMongoid
2
+ include Mongoid::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 MultipleInvalidPersistorMongoid
22
+ include Mongoid::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,39 @@
1
+ class SilentPersistorMongoid
2
+ include Mongoid::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 MultipleSilentPersistorMongoid
22
+ include Mongoid::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,100 @@
1
+ class ValidatorMongoid
2
+ include Mongoid::Document
3
+ include AASM
4
+
5
+ field :name
6
+ field :status
7
+
8
+ attr_accessor :invalid
9
+
10
+ validate do |model|
11
+ errors.add(:validator, "invalid") if invalid
12
+ end
13
+
14
+ include AASM
15
+
16
+ aasm :column => :status, :whiny_persistence => true do
17
+ before_all_transactions :before_all_transactions
18
+ after_all_transactions :after_all_transactions
19
+
20
+ state :sleeping, :initial => true
21
+ state :running
22
+ state :failed, :after_enter => :fail
23
+
24
+ event :run, :after_commit => :change_name! do
25
+ transitions :to => :running, :from => :sleeping
26
+ end
27
+
28
+ event :sleep do
29
+ after_commit do |name|
30
+ change_name_on_sleep name
31
+ end
32
+ transitions :to => :sleeping, :from => :running
33
+ end
34
+
35
+ event :fail do
36
+ transitions :to => :failed, :from => [:sleeping, :running]
37
+ end
38
+ end
39
+
40
+ validates_presence_of :name
41
+
42
+ def change_name!
43
+ self.name = "name changed"
44
+ save!
45
+ end
46
+
47
+ def change_name_on_sleep name
48
+ self.name = name
49
+ save!
50
+ end
51
+
52
+ def fail
53
+ raise StandardError.new('failed on purpose')
54
+ end
55
+ end
56
+
57
+ class MultipleValidatorMongoid
58
+ include Mongoid::Document
59
+ include AASM
60
+
61
+ field :name
62
+ field :status
63
+
64
+ attr_accessor :invalid
65
+
66
+ aasm :left, :column => :status, :whiny_persistence => true do
67
+ state :sleeping, :initial => true
68
+ state :running
69
+ state :failed, :after_enter => :fail
70
+
71
+ event :run, :after_commit => :change_name! do
72
+ transitions :to => :running, :from => :sleeping
73
+ end
74
+ event :sleep do
75
+ after_commit do |name|
76
+ change_name_on_sleep name
77
+ end
78
+ transitions :to => :sleeping, :from => :running
79
+ end
80
+ event :fail do
81
+ transitions :to => :failed, :from => [:sleeping, :running]
82
+ end
83
+ end
84
+
85
+ validates_presence_of :name
86
+
87
+ def change_name!
88
+ self.name = "name changed"
89
+ save!
90
+ end
91
+
92
+ def change_name_on_sleep name
93
+ self.name = name
94
+ save!
95
+ end
96
+
97
+ def fail
98
+ raise StandardError.new('failed on purpose')
99
+ end
100
+ end
@@ -0,0 +1,31 @@
1
+ class MultipleTransitionsThatDifferOnlyByGuard
2
+ include AASM
3
+
4
+ attr_accessor :executed_second
5
+
6
+ aasm do
7
+ state :start, :initial => true
8
+ state :gone
9
+
10
+ event :go do
11
+ transitions :from => :start, :to => :gone, :guard => :returns_false, :after => :this_should_not_execute
12
+ transitions :from => :start, :to => :gone, :guard => :returns_true, :after => :this_should_execute
13
+ end
14
+ end
15
+
16
+ def returns_false
17
+ false
18
+ end
19
+
20
+ def returns_true
21
+ true
22
+ end
23
+
24
+ def this_should_not_execute
25
+ raise RuntimeError, "This should not execute"
26
+ end
27
+
28
+ def this_should_execute
29
+ self.executed_second = true
30
+ end
31
+ end
@@ -11,6 +11,10 @@ class ParametrisedEvent
11
11
  transitions :from => :sleeping, :to => [:showering, :working]
12
12
  end
13
13
 
14
+ event :shower do
15
+ transitions :from => :sleeping, :to => :showering, :after => :wet_hair, :success => :wear_clothes
16
+ end
17
+
14
18
  event :dress do
15
19
  transitions :from => :sleeping, :to => :working, :after => :wear_clothes, :success => :wear_makeup
16
20
  transitions :from => :showering, :to => [:working, :dating], :after => Proc.new { |*args| wear_clothes(*args) }, :success => proc { |*args| wear_makeup(*args) }
@@ -24,6 +28,9 @@ class ParametrisedEvent
24
28
  def wear_makeup(makeup, moisturizer)
25
29
  end
26
30
 
31
+ def wet_hair(dryer)
32
+ end
33
+
27
34
  def condition_hair
28
35
  end
29
36
 
@@ -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
@@ -19,39 +19,6 @@ def load_schema
19
19
  require File.dirname(__FILE__) + "/database.rb"
20
20
  end
21
21
 
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
22
  # custom spec helpers
56
23
  Dir[File.dirname(__FILE__) + "/spec_helpers/**/*.rb"].sort.each { |f| require File.expand_path(f) }
57
24
 
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'active_record'
4
+ puts "active_record gem found, running ActiveRecord specs \e[32m#{'✔'}\e[0m"
5
+ rescue LoadError
6
+ puts "active_record gem not found, not running ActiveRecord specs \e[31m#{'✖'}\e[0m"
7
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'dynamoid'
4
+ require 'aws-sdk-resources'
5
+ puts "dynamoid gem found, running Dynamoid specs \e[32m#{'✔'}\e[0m"
6
+
7
+ ENV['ACCESS_KEY'] ||= 'abcd'
8
+ ENV['SECRET_KEY'] ||= '1234'
9
+
10
+ Aws.config.update({
11
+ region: 'us-west-2',
12
+ credentials: Aws::Credentials.new(ENV['ACCESS_KEY'], ENV['SECRET_KEY'])
13
+ })
14
+
15
+ Dynamoid.configure do |config|
16
+ config.namespace = "dynamoid_tests"
17
+ config.endpoint = 'http://127.0.0.1:30180'
18
+ config.warn_on_scan = false
19
+ end
20
+
21
+ Dynamoid.logger.level = Logger::FATAL
22
+
23
+ RSpec.configure do |c|
24
+ c.before(:each) do
25
+ Dynamoid.adapter.list_tables.each do |table|
26
+ Dynamoid.adapter.delete_table(table) if table =~ /^#{Dynamoid::Config.namespace}/
27
+ end
28
+ Dynamoid.adapter.tables.clear
29
+ end
30
+ end
31
+ rescue LoadError
32
+ puts "dynamoid gem not found, not running Dynamoid specs \e[31m#{'✖'}\e[0m"
33
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'mongoid'
4
+ puts "mongoid gem found, running mongoid specs \e[32m#{'✔'}\e[0m"
5
+ rescue LoadError
6
+ puts "mongoid gem not found, not running mongoid specs \e[31m#{'✖'}\e[0m"
7
+ end