aasm 4.5.1 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +809 -129
  4. data/lib/aasm/aasm.rb +74 -37
  5. data/lib/aasm/base.rb +188 -41
  6. data/lib/aasm/configuration.rb +27 -2
  7. data/lib/aasm/core/event.rb +75 -47
  8. data/lib/aasm/core/invoker.rb +129 -0
  9. data/lib/aasm/core/invokers/base_invoker.rb +75 -0
  10. data/lib/aasm/core/invokers/class_invoker.rb +52 -0
  11. data/lib/aasm/core/invokers/literal_invoker.rb +49 -0
  12. data/lib/aasm/core/invokers/proc_invoker.rb +59 -0
  13. data/lib/aasm/core/state.rb +22 -13
  14. data/lib/aasm/core/transition.rb +30 -23
  15. data/lib/aasm/dsl_helper.rb +24 -22
  16. data/lib/aasm/errors.rb +8 -5
  17. data/lib/aasm/instance_base.rb +63 -15
  18. data/lib/aasm/localizer.rb +13 -3
  19. data/lib/aasm/minitest/allow_event.rb +13 -0
  20. data/lib/aasm/minitest/allow_transition_to.rb +13 -0
  21. data/lib/aasm/minitest/have_state.rb +13 -0
  22. data/lib/aasm/minitest/transition_from.rb +21 -0
  23. data/lib/aasm/minitest.rb +5 -0
  24. data/lib/aasm/minitest_spec.rb +15 -0
  25. data/lib/aasm/persistence/active_record_persistence.rb +87 -79
  26. data/lib/aasm/persistence/base.rb +30 -30
  27. data/lib/aasm/persistence/core_data_query_persistence.rb +94 -0
  28. data/lib/aasm/persistence/dynamoid_persistence.rb +92 -0
  29. data/lib/aasm/persistence/mongoid_persistence.rb +49 -35
  30. data/lib/aasm/persistence/no_brainer_persistence.rb +105 -0
  31. data/lib/aasm/persistence/orm.rb +154 -0
  32. data/lib/aasm/persistence/plain_persistence.rb +2 -1
  33. data/lib/aasm/persistence/redis_persistence.rb +112 -0
  34. data/lib/aasm/persistence/sequel_persistence.rb +37 -67
  35. data/lib/aasm/persistence.rb +20 -5
  36. data/lib/aasm/rspec/allow_event.rb +5 -1
  37. data/lib/aasm/rspec/allow_transition_to.rb +5 -1
  38. data/lib/aasm/rspec/transition_from.rb +8 -4
  39. data/lib/aasm/state_machine.rb +6 -12
  40. data/lib/aasm/state_machine_store.rb +76 -0
  41. data/lib/aasm/version.rb +1 -1
  42. data/lib/aasm.rb +8 -2
  43. data/lib/generators/aasm/aasm_generator.rb +16 -0
  44. data/lib/generators/aasm/orm_helpers.rb +41 -0
  45. data/lib/generators/active_record/aasm_generator.rb +40 -0
  46. data/lib/generators/active_record/templates/migration.rb +8 -0
  47. data/lib/generators/active_record/templates/migration_existing.rb +5 -0
  48. data/lib/generators/mongoid/aasm_generator.rb +28 -0
  49. data/lib/generators/nobrainer/aasm_generator.rb +28 -0
  50. data/lib/motion-aasm.rb +37 -0
  51. metadata +104 -259
  52. data/.document +0 -6
  53. data/.gitignore +0 -19
  54. data/.travis.yml +0 -37
  55. data/API +0 -34
  56. data/CHANGELOG.md +0 -272
  57. data/CODE_OF_CONDUCT.md +0 -13
  58. data/Gemfile +0 -15
  59. data/HOWTO +0 -12
  60. data/PLANNED_CHANGES.md +0 -11
  61. data/README_FROM_VERSION_3_TO_4.md +0 -240
  62. data/Rakefile +0 -26
  63. data/aasm.gemspec +0 -31
  64. data/callbacks.txt +0 -51
  65. data/gemfiles/rails_3.2.gemfile +0 -14
  66. data/gemfiles/rails_4.0.gemfile +0 -12
  67. data/gemfiles/rails_4.0_mongo_mapper.gemfile +0 -14
  68. data/gemfiles/rails_4.1.gemfile +0 -12
  69. data/gemfiles/rails_4.1_mongo_mapper.gemfile +0 -14
  70. data/gemfiles/rails_4.2.gemfile +0 -12
  71. data/gemfiles/rails_4.2_mongo_mapper.gemfile +0 -14
  72. data/gemfiles/rails_4.2_mongoid_5.gemfile +0 -12
  73. data/lib/aasm/persistence/mongo_mapper_persistence.rb +0 -157
  74. data/spec/database.rb +0 -63
  75. data/spec/database.yml +0 -3
  76. data/spec/en.yml +0 -9
  77. data/spec/en_deprecated_style.yml +0 -10
  78. data/spec/models/active_record/basic_active_record_two_state_machines_example.rb +0 -25
  79. data/spec/models/active_record/complex_active_record_example.rb +0 -33
  80. data/spec/models/active_record/derivate_new_dsl.rb +0 -7
  81. data/spec/models/active_record/false_state.rb +0 -35
  82. data/spec/models/active_record/gate.rb +0 -39
  83. data/spec/models/active_record/localizer_test_model.rb +0 -34
  84. data/spec/models/active_record/no_direct_assignment.rb +0 -21
  85. data/spec/models/active_record/no_scope.rb +0 -21
  86. data/spec/models/active_record/persisted_state.rb +0 -12
  87. data/spec/models/active_record/provided_and_persisted_state.rb +0 -24
  88. data/spec/models/active_record/reader.rb +0 -7
  89. data/spec/models/active_record/readme_job.rb +0 -21
  90. data/spec/models/active_record/simple_new_dsl.rb +0 -17
  91. data/spec/models/active_record/thief.rb +0 -29
  92. data/spec/models/active_record/transient.rb +0 -6
  93. data/spec/models/active_record/with_enum.rb +0 -39
  94. data/spec/models/active_record/with_false_enum.rb +0 -31
  95. data/spec/models/active_record/with_true_enum.rb +0 -39
  96. data/spec/models/active_record/writer.rb +0 -6
  97. data/spec/models/basic_two_state_machines_example.rb +0 -25
  98. data/spec/models/callbacks/basic.rb +0 -78
  99. data/spec/models/callbacks/basic_multiple.rb +0 -75
  100. data/spec/models/callbacks/guard_within_block.rb +0 -66
  101. data/spec/models/callbacks/guard_within_block_multiple.rb +0 -66
  102. data/spec/models/callbacks/multiple_transitions_transition_guard.rb +0 -65
  103. data/spec/models/callbacks/multiple_transitions_transition_guard_multiple.rb +0 -65
  104. data/spec/models/callbacks/private_method.rb +0 -44
  105. data/spec/models/callbacks/private_method_multiple.rb +0 -44
  106. data/spec/models/callbacks/with_args.rb +0 -61
  107. data/spec/models/callbacks/with_args_multiple.rb +0 -61
  108. data/spec/models/callbacks/with_state_arg.rb +0 -26
  109. data/spec/models/callbacks/with_state_arg_multiple.rb +0 -26
  110. data/spec/models/complex_example.rb +0 -222
  111. data/spec/models/conversation.rb +0 -93
  112. data/spec/models/default_state.rb +0 -12
  113. data/spec/models/double_definer.rb +0 -21
  114. data/spec/models/foo.rb +0 -92
  115. data/spec/models/foo_callback_multiple.rb +0 -45
  116. data/spec/models/guardian.rb +0 -48
  117. data/spec/models/guardian_multiple.rb +0 -48
  118. data/spec/models/initial_state_proc.rb +0 -31
  119. data/spec/models/invalid_persistor.rb +0 -31
  120. data/spec/models/mongo_mapper/complex_mongo_mapper_example.rb +0 -37
  121. data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +0 -21
  122. data/spec/models/mongo_mapper/simple_mongo_mapper.rb +0 -23
  123. data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +0 -25
  124. data/spec/models/mongoid/complex_mongoid_example.rb +0 -37
  125. data/spec/models/mongoid/no_scope_mongoid.rb +0 -21
  126. data/spec/models/mongoid/simple_mongoid.rb +0 -23
  127. data/spec/models/mongoid/simple_new_dsl_mongoid.rb +0 -25
  128. data/spec/models/no_initial_state.rb +0 -25
  129. data/spec/models/not_auto_loaded/process.rb +0 -21
  130. data/spec/models/parametrised_event.rb +0 -29
  131. data/spec/models/parametrised_event_multiple.rb +0 -29
  132. data/spec/models/process_with_new_dsl.rb +0 -31
  133. data/spec/models/provided_state.rb +0 -24
  134. data/spec/models/sequel/complex_sequel_example.rb +0 -45
  135. data/spec/models/sequel/sequel_multiple.rb +0 -25
  136. data/spec/models/sequel/sequel_simple.rb +0 -25
  137. data/spec/models/silencer.rb +0 -27
  138. data/spec/models/simple_example.rb +0 -15
  139. data/spec/models/simple_multiple_example.rb +0 -30
  140. data/spec/models/state_machine_with_failed_event.rb +0 -12
  141. data/spec/models/sub_class.rb +0 -7
  142. data/spec/models/sub_class_with_more_states.rb +0 -18
  143. data/spec/models/sub_classing.rb +0 -3
  144. data/spec/models/super_class.rb +0 -46
  145. data/spec/models/this_name_better_not_be_in_use.rb +0 -11
  146. data/spec/models/transactor.rb +0 -53
  147. data/spec/models/valid_state_name.rb +0 -23
  148. data/spec/models/validator.rb +0 -79
  149. data/spec/models/worker.rb +0 -2
  150. data/spec/spec_helper.rb +0 -25
  151. data/spec/unit/api_spec.rb +0 -77
  152. data/spec/unit/basic_two_state_machines_example_spec.rb +0 -10
  153. data/spec/unit/callback_multiple_spec.rb +0 -295
  154. data/spec/unit/callbacks_spec.rb +0 -296
  155. data/spec/unit/complex_example_spec.rb +0 -84
  156. data/spec/unit/complex_multiple_example_spec.rb +0 -99
  157. data/spec/unit/edge_cases_spec.rb +0 -16
  158. data/spec/unit/event_multiple_spec.rb +0 -73
  159. data/spec/unit/event_naming_spec.rb +0 -11
  160. data/spec/unit/event_spec.rb +0 -322
  161. data/spec/unit/guard_multiple_spec.rb +0 -60
  162. data/spec/unit/guard_spec.rb +0 -60
  163. data/spec/unit/initial_state_multiple_spec.rb +0 -15
  164. data/spec/unit/initial_state_spec.rb +0 -12
  165. data/spec/unit/inspection_multiple_spec.rb +0 -201
  166. data/spec/unit/inspection_spec.rb +0 -111
  167. data/spec/unit/localizer_spec.rb +0 -76
  168. data/spec/unit/memory_leak_spec.rb +0 -38
  169. data/spec/unit/new_dsl_spec.rb +0 -12
  170. data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +0 -573
  171. data/spec/unit/persistence/active_record_persistence_spec.rb +0 -552
  172. data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +0 -146
  173. data/spec/unit/persistence/mongo_mapper_persistence_spec.rb +0 -93
  174. data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +0 -127
  175. data/spec/unit/persistence/mongoid_persistence_spec.rb +0 -79
  176. data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +0 -153
  177. data/spec/unit/persistence/sequel_persistence_spec.rb +0 -100
  178. data/spec/unit/readme_spec.rb +0 -42
  179. data/spec/unit/reloading_spec.rb +0 -15
  180. data/spec/unit/rspec_matcher_spec.rb +0 -79
  181. data/spec/unit/simple_example_spec.rb +0 -42
  182. data/spec/unit/simple_multiple_example_spec.rb +0 -63
  183. data/spec/unit/state_spec.rb +0 -89
  184. data/spec/unit/subclassing_multiple_spec.rb +0 -39
  185. data/spec/unit/subclassing_spec.rb +0 -31
  186. data/spec/unit/transition_spec.rb +0 -291
@@ -0,0 +1,105 @@
1
+ require 'aasm/persistence/orm'
2
+ module AASM
3
+ module Persistence
4
+ module NoBrainerPersistence
5
+ # This method:
6
+ #
7
+ # * extends the model with ClassMethods
8
+ # * includes InstanceMethods
9
+ #
10
+ # Adds
11
+ #
12
+ # before_validation :aasm_ensure_initial_state
13
+ #
14
+ # As a result, it doesn't matter when you define your methods - the following 2 are equivalent
15
+ #
16
+ # class Foo
17
+ # include NoBrainer::Document
18
+ # def aasm_write_state(state)
19
+ # "bar"
20
+ # end
21
+ # include AASM
22
+ # end
23
+ #
24
+ # class Foo
25
+ # include NoBrainer::Document
26
+ # include AASM
27
+ # def aasm_write_state(state)
28
+ # "bar"
29
+ # end
30
+ # end
31
+ #
32
+ def self.included(base)
33
+ base.send(:include, AASM::Persistence::Base)
34
+ base.send(:include, AASM::Persistence::ORM)
35
+ base.send(:include, AASM::Persistence::NoBrainerPersistence::InstanceMethods)
36
+ base.extend AASM::Persistence::NoBrainerPersistence::ClassMethods
37
+
38
+ base.after_initialize :aasm_ensure_initial_state
39
+ end
40
+
41
+ module ClassMethods
42
+ def aasm_create_scope(state_machine_name, scope_name)
43
+ scope_options = lambda {
44
+ where(aasm(state_machine_name).attribute_name.to_sym => scope_name.to_s)
45
+ }
46
+ send(:scope, scope_name, scope_options)
47
+ end
48
+ end
49
+
50
+ module InstanceMethods
51
+
52
+ private
53
+
54
+ def aasm_save
55
+ self.save
56
+ end
57
+
58
+ def aasm_raise_invalid_record
59
+ raise NoBrainer::Error::DocumentInvalid.new(self)
60
+ end
61
+
62
+ def aasm_supports_transactions?
63
+ false
64
+ end
65
+
66
+ def aasm_update_column(attribute_name, value)
67
+ write_attribute(attribute_name, value)
68
+ save(validate: false)
69
+
70
+ true
71
+ end
72
+
73
+ def aasm_read_attribute(name)
74
+ read_attribute(name)
75
+ end
76
+
77
+ def aasm_write_attribute(name, value)
78
+ write_attribute(name, value)
79
+ end
80
+
81
+ # Ensures that if the aasm_state column is nil and the record is new
82
+ # that the initial state gets populated before validation on create
83
+ #
84
+ # foo = Foo.new
85
+ # foo.aasm_state # => nil
86
+ # foo.valid?
87
+ # foo.aasm_state # => "open" (where :open is the initial state)
88
+ #
89
+ #
90
+ # foo = Foo.find(:first)
91
+ # foo.aasm_state # => 1
92
+ # foo.aasm_state = nil
93
+ # foo.valid?
94
+ # foo.aasm_state # => nil
95
+ #
96
+ def aasm_ensure_initial_state
97
+ AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |name|
98
+ aasm_column = self.class.aasm(name).attribute_name
99
+ aasm(name).enter_initial_state if !read_attribute(aasm_column) || read_attribute(aasm_column).empty?
100
+ end
101
+ end
102
+ end # InstanceMethods
103
+ end
104
+ end # Persistence
105
+ end # AASM
@@ -0,0 +1,154 @@
1
+ module AASM
2
+ module Persistence
3
+ # This module adds transactional support for any database that supports it.
4
+ # This includes rollback capability and rollback/commit callbacks.
5
+ module ORM
6
+
7
+ # Writes <tt>state</tt> to the state column and persists it to the database
8
+ #
9
+ # foo = Foo.find(1)
10
+ # foo.aasm.current_state # => :opened
11
+ # foo.close!
12
+ # foo.aasm.current_state # => :closed
13
+ # Foo.find(1).aasm.current_state # => :closed
14
+ #
15
+ # NOTE: intended to be called from an event
16
+ def aasm_write_state(state, name=:default)
17
+ attribute_name = self.class.aasm(name).attribute_name
18
+ old_value = aasm_read_attribute(attribute_name)
19
+ aasm_write_state_attribute state, name
20
+
21
+ success = if aasm_skipping_validations(name)
22
+ aasm_update_column(attribute_name, aasm_raw_attribute_value(state, name))
23
+ else
24
+ aasm_save
25
+ end
26
+
27
+ unless success
28
+ aasm_rollback(name, old_value)
29
+ aasm_raise_invalid_record if aasm_whiny_persistence(name)
30
+ end
31
+
32
+ success
33
+ end
34
+
35
+ # Writes <tt>state</tt> to the state field, but does not persist it to the database
36
+ #
37
+ # foo = Foo.find(1)
38
+ # foo.aasm.current_state # => :opened
39
+ # foo.close
40
+ # foo.aasm.current_state # => :closed
41
+ # Foo.find(1).aasm.current_state # => :opened
42
+ # foo.save
43
+ # foo.aasm.current_state # => :closed
44
+ # Foo.find(1).aasm.current_state # => :closed
45
+ #
46
+ # NOTE: intended to be called from an event
47
+ def aasm_write_state_without_persistence(state, name=:default)
48
+ aasm_write_state_attribute(state, name)
49
+ end
50
+
51
+ private
52
+
53
+ # Save the record and return true if it succeeded/false otherwise.
54
+ def aasm_save
55
+ raise("Define #aasm_save_without_error in the AASM Persistence class.")
56
+ end
57
+
58
+ def aasm_raise_invalid_record
59
+ raise("Define #aasm_raise_invalid_record in the AASM Persistence class.")
60
+ end
61
+
62
+ # Update only the column without running validations.
63
+ def aasm_update_column(attribute_name, value)
64
+ raise("Define #aasm_update_column in the AASM Persistence class.")
65
+ end
66
+
67
+ def aasm_read_attribute(name)
68
+ raise("Define #aasm_read_attribute the AASM Persistence class.")
69
+ end
70
+
71
+ def aasm_write_attribute(name, value)
72
+ raise("Define #aasm_write_attribute in the AASM Persistence class.")
73
+ end
74
+
75
+ # Returns true or false if transaction completed successfully.
76
+ def aasm_transaction(requires_new, requires_lock)
77
+ raise("Define #aasm_transaction the AASM Persistence class.")
78
+ end
79
+
80
+ def aasm_supports_transactions?
81
+ true
82
+ end
83
+
84
+ def aasm_execute_after_commit
85
+ yield
86
+ end
87
+
88
+ def aasm_write_state_attribute(state, name=:default)
89
+ aasm_write_attribute(self.class.aasm(name).attribute_name, aasm_raw_attribute_value(state, name))
90
+ end
91
+
92
+ def aasm_raw_attribute_value(state, _name=:default)
93
+ state.to_s
94
+ end
95
+
96
+ def aasm_rollback(name, old_value)
97
+ aasm_write_attribute(self.class.aasm(name).attribute_name, old_value)
98
+ false
99
+ end
100
+
101
+ def aasm_whiny_persistence(state_machine_name)
102
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.whiny_persistence
103
+ end
104
+
105
+ def aasm_skipping_validations(state_machine_name)
106
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.skip_validation_on_save
107
+ end
108
+
109
+ def use_transactions?(state_machine_name)
110
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.use_transactions
111
+ end
112
+
113
+ def requires_new?(state_machine_name)
114
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.requires_new_transaction
115
+ end
116
+
117
+ def requires_lock?(state_machine_name)
118
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.requires_lock
119
+ end
120
+
121
+ # Returns true if event was fired successfully and transaction completed.
122
+ def aasm_fire_event(state_machine_name, name, options, *args, &block)
123
+ return super unless aasm_supports_transactions? && options[:persist]
124
+
125
+ event = self.class.aasm(state_machine_name).state_machine.events[name]
126
+ event.fire_callbacks(:before_transaction, self, *args)
127
+ event.fire_global_callbacks(:before_all_transactions, self, *args)
128
+
129
+ begin
130
+ success = if options[:persist] && use_transactions?(state_machine_name)
131
+ aasm_transaction(requires_new?(state_machine_name), requires_lock?(state_machine_name)) do
132
+ super
133
+ end
134
+ else
135
+ super
136
+ end
137
+
138
+ if success && !(event.options.keys & [:after_commit, :after_all_commits]).empty?
139
+ aasm_execute_after_commit do
140
+ event.fire_callbacks(:after_commit, self, *args)
141
+ event.fire_global_callbacks(:after_all_commits, self, *args)
142
+ end
143
+ end
144
+
145
+ success
146
+ ensure
147
+ event.fire_callbacks(:after_transaction, self, *args)
148
+ event.fire_global_callbacks(:after_all_transactions, self, *args)
149
+ end
150
+ end
151
+
152
+ end # Transactional
153
+ end # Persistence
154
+ end # AASM
@@ -5,7 +5,8 @@ module AASM
5
5
  # may be overwritten by persistence mixins
6
6
  def aasm_read_state(name=:default)
7
7
  # all the following lines behave like @current_state ||= aasm(name).enter_initial_state
8
- current = aasm(name).instance_variable_get("@current_state_#{name}")
8
+ current = aasm(name).instance_variable_defined?("@current_state_#{name}") &&
9
+ aasm(name).instance_variable_get("@current_state_#{name}")
9
10
  return current if current
10
11
  aasm(name).instance_variable_set("@current_state_#{name}", aasm(name).enter_initial_state)
11
12
  end
@@ -0,0 +1,112 @@
1
+ module AASM
2
+ module Persistence
3
+ module RedisPersistence
4
+
5
+ def self.included(base)
6
+ base.send(:include, AASM::Persistence::Base)
7
+ base.send(:include, AASM::Persistence::RedisPersistence::InstanceMethods)
8
+ end
9
+
10
+ module InstanceMethods
11
+ # Initialize with default values
12
+ #
13
+ # Redis::Objects removes the key from Redis when set to `nil`
14
+ def initialize(*args)
15
+ super
16
+ aasm_ensure_initial_state
17
+ end
18
+ # Returns the value of the aasm.attribute_name - called from <tt>aasm.current_state</tt>
19
+ #
20
+ # If it's a new record, and the aasm state column is blank it returns the initial state
21
+ #
22
+ # class Foo
23
+ # include Redis::Objects
24
+ # include AASM
25
+ # aasm :column => :status do
26
+ # state :opened
27
+ # state :closed
28
+ # end
29
+ # end
30
+ #
31
+ # foo = Foo.new
32
+ # foo.current_state # => :opened
33
+ # foo.close
34
+ # foo.current_state # => :closed
35
+ #
36
+ # foo = Foo[1]
37
+ # foo.current_state # => :opened
38
+ # foo.aasm_state = nil
39
+ # foo.current_state # => nil
40
+ #
41
+ # NOTE: intended to be called from an event
42
+ #
43
+ # This allows for nil aasm states - be sure to add validation to your model
44
+ def aasm_read_state(name=:default)
45
+ state = send(self.class.aasm(name).attribute_name)
46
+
47
+ if state.value.nil?
48
+ nil
49
+ else
50
+ state.value.to_sym
51
+ end
52
+ end
53
+
54
+ # Ensures that if the aasm_state column is nil and the record is new
55
+ # that the initial state gets populated before validation on create
56
+ #
57
+ # foo = Foo.new
58
+ # foo.aasm_state # => nil
59
+ # foo.valid?
60
+ # foo.aasm_state # => "open" (where :open is the initial state)
61
+ #
62
+ #
63
+ # foo = Foo.find(:first)
64
+ # foo.aasm_state # => 1
65
+ # foo.aasm_state = nil
66
+ # foo.valid?
67
+ # foo.aasm_state # => nil
68
+ #
69
+ def aasm_ensure_initial_state
70
+ AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |name|
71
+ aasm_column = self.class.aasm(name).attribute_name
72
+ aasm(name).enter_initial_state if !send(aasm_column).value || send(aasm_column).value.empty?
73
+ end
74
+ end
75
+
76
+ # Writes <tt>state</tt> to the state column and persists it to the database
77
+ #
78
+ # foo = Foo[1]
79
+ # foo.aasm.current_state # => :opened
80
+ # foo.close!
81
+ # foo.aasm.current_state # => :closed
82
+ # Foo[1].aasm.current_state # => :closed
83
+ #
84
+ # NOTE: intended to be called from an event
85
+ def aasm_write_state(state, name=:default)
86
+ aasm_column = self.class.aasm(name).attribute_name
87
+ send("#{aasm_column}").value = state
88
+ end
89
+
90
+ # Writes <tt>state</tt> to the state column, but does not persist it to the database
91
+ # (but actually it still does)
92
+ #
93
+ # With Redis::Objects it's not possible to skip persisting - it's not an ORM,
94
+ # it does not operate like an AR model and does not know how to postpone changes.
95
+ #
96
+ # foo = Foo[1]
97
+ # foo.aasm.current_state # => :opened
98
+ # foo.close
99
+ # foo.aasm.current_state # => :closed
100
+ # Foo[1].aasm.current_state # => :opened
101
+ # foo.save
102
+ # foo.aasm.current_state # => :closed
103
+ # Foo[1].aasm.current_state # => :closed
104
+ #
105
+ # NOTE: intended to be called from an event
106
+ def aasm_write_state_without_persistence(state, name=:default)
107
+ aasm_write_state(state, name)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -1,10 +1,10 @@
1
- require_relative 'base'
2
-
1
+ require 'aasm/persistence/orm'
3
2
  module AASM
4
3
  module Persistence
5
4
  module SequelPersistence
6
5
  def self.included(base)
7
6
  base.send(:include, AASM::Persistence::Base)
7
+ base.send(:include, AASM::Persistence::ORM)
8
8
  base.send(:include, AASM::Persistence::SequelPersistence::InstanceMethods)
9
9
  end
10
10
 
@@ -15,46 +15,45 @@ module AASM
15
15
  end
16
16
 
17
17
  def before_create
18
- aasm_ensure_initial_state
19
18
  super
20
19
  end
21
20
 
22
- # Returns the value of the aasm.attribute_name - called from <tt>aasm.current_state</tt>
23
- #
24
- # If it's a new record, and the aasm state column is blank it returns the initial state
25
- #
26
- # class Foo < Sequel::Model
27
- # include AASM
28
- # aasm :column => :status do
29
- # state :opened
30
- # state :closed
31
- # end
32
- # end
33
- #
34
- # foo = Foo.new
35
- # foo.current_state # => :opened
36
- # foo.close
37
- # foo.current_state # => :closed
38
- #
39
- # foo = Foo[1]
40
- # foo.current_state # => :opened
41
- # foo.aasm_state = nil
42
- # foo.current_state # => nil
43
- #
44
- # NOTE: intended to be called from an event
45
- #
46
- # This allows for nil aasm states - be sure to add validation to your model
47
- def aasm_read_state(name=:default)
48
- state = send(self.class.aasm(name).attribute_name)
49
- if new? && state.to_s.strip.empty?
50
- aasm(name).determine_state_name(self.class.aasm(name).initial_state)
51
- elsif state.nil?
52
- nil
53
- else
54
- state.to_sym
21
+ def aasm_raise_invalid_record
22
+ raise Sequel::ValidationFailed.new(self)
23
+ end
24
+
25
+ def aasm_new_record?
26
+ new?
27
+ end
28
+
29
+ # Returns nil if fails silently
30
+ # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/InstanceMethods.html#method-i-save
31
+ def aasm_save
32
+ !save(raise_on_failure: false).nil?
33
+ end
34
+
35
+ def aasm_read_attribute(name)
36
+ send(name)
37
+ end
38
+
39
+ def aasm_write_attribute(name, value)
40
+ send("#{name}=", value)
41
+ end
42
+
43
+ def aasm_transaction(requires_new, requires_lock)
44
+ self.class.db.transaction(savepoint: requires_new) do
45
+ if requires_lock
46
+ # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/InstanceMethods.html#method-i-lock-21
47
+ requires_lock.is_a?(String) ? lock!(requires_lock) : lock!
48
+ end
49
+ yield
55
50
  end
56
51
  end
57
52
 
53
+ def aasm_update_column(attribute_name, value)
54
+ this.update(attribute_name => value)
55
+ end
56
+
58
57
  # Ensures that if the aasm_state column is nil and the record is new
59
58
  # that the initial state gets populated before validation on create
60
59
  #
@@ -71,42 +70,13 @@ module AASM
71
70
  # foo.aasm_state # => nil
72
71
  #
73
72
  def aasm_ensure_initial_state
74
- AASM::StateMachine[self.class].keys.each do |state_machine_name|
73
+ AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
75
74
  aasm(state_machine_name).enter_initial_state if
76
75
  (new? || values.key?(self.class.aasm(state_machine_name).attribute_name)) &&
77
- send(self.class.aasm(state_machine_name).attribute_name).to_s.strip.empty?
76
+ send(self.class.aasm(state_machine_name).attribute_name).to_s.strip.empty?
78
77
  end
79
78
  end
80
79
 
81
- # Writes <tt>state</tt> to the state column and persists it to the database
82
- #
83
- # foo = Foo[1]
84
- # foo.aasm.current_state # => :opened
85
- # foo.close!
86
- # foo.aasm.current_state # => :closed
87
- # Foo[1].aasm.current_state # => :closed
88
- #
89
- # NOTE: intended to be called from an event
90
- def aasm_write_state state, name=:default
91
- aasm_column = self.class.aasm(name).attribute_name
92
- update_only({aasm_column => state.to_s}, aasm_column)
93
- end
94
-
95
- # Writes <tt>state</tt> to the state column, but does not persist it to the database
96
- #
97
- # foo = Foo[1]
98
- # foo.aasm.current_state # => :opened
99
- # foo.close
100
- # foo.aasm.current_state # => :closed
101
- # Foo[1].aasm.current_state # => :opened
102
- # foo.save
103
- # foo.aasm.current_state # => :closed
104
- # Foo[1].aasm.current_state # => :closed
105
- #
106
- # NOTE: intended to be called from an event
107
- def aasm_write_state_without_persistence state, name=:default
108
- send("#{self.class.aasm(name).attribute_name}=", state.to_s)
109
- end
110
80
  end
111
81
  end
112
82
  end
@@ -7,13 +7,25 @@ module AASM
7
7
  hierarchy = base.ancestors.map {|klass| klass.to_s}
8
8
 
9
9
  if hierarchy.include?("ActiveRecord::Base")
10
+ require_persistence :active_record
10
11
  include_persistence base, :active_record
11
12
  elsif hierarchy.include?("Mongoid::Document")
13
+ require_persistence :mongoid
12
14
  include_persistence base, :mongoid
13
- elsif hierarchy.include?("MongoMapper::Document")
14
- include_persistence base, :mongo_mapper
15
+ elsif hierarchy.include?("NoBrainer::Document")
16
+ require_persistence :no_brainer
17
+ include_persistence base, :no_brainer
15
18
  elsif hierarchy.include?("Sequel::Model")
19
+ require_persistence :sequel
16
20
  include_persistence base, :sequel
21
+ elsif hierarchy.include?("Dynamoid::Document")
22
+ require_persistence :dynamoid
23
+ include_persistence base, :dynamoid
24
+ elsif hierarchy.include?("Redis::Objects")
25
+ require_persistence :redis
26
+ include_persistence base, :redis
27
+ elsif hierarchy.include?("CDQManagedObject")
28
+ include_persistence base, :core_data_query
17
29
  else
18
30
  include_persistence base, :plain
19
31
  end
@@ -21,9 +33,12 @@ module AASM
21
33
 
22
34
  private
23
35
 
24
- def include_persistence(base, type)
36
+ def require_persistence(type)
25
37
  require File.join(File.dirname(__FILE__), 'persistence', "#{type}_persistence")
26
- base.send(:include, constantize("AASM::Persistence::#{capitalize(type)}Persistence"))
38
+ end
39
+
40
+ def include_persistence(base, type)
41
+ base.send(:include, constantize("#{capitalize(type)}Persistence"))
27
42
  end
28
43
 
29
44
  def capitalize(string_or_symbol)
@@ -31,7 +46,7 @@ module AASM
31
46
  end
32
47
 
33
48
  def constantize(string)
34
- instance_eval(string)
49
+ AASM::Persistence.const_get(string)
35
50
  end
36
51
 
37
52
  end # class << self
@@ -1,13 +1,17 @@
1
1
  RSpec::Matchers.define :allow_event do |event|
2
2
  match do |obj|
3
3
  @state_machine_name ||= :default
4
- obj.aasm(@state_machine_name).may_fire_event?(event)
4
+ obj.aasm(@state_machine_name).may_fire_event?(event, *@args)
5
5
  end
6
6
 
7
7
  chain :on do |state_machine_name|
8
8
  @state_machine_name = state_machine_name
9
9
  end
10
10
 
11
+ chain :with do |*args|
12
+ @args = args
13
+ end
14
+
11
15
  description do
12
16
  "allow event #{expected} (on :#{@state_machine_name})"
13
17
  end
@@ -1,13 +1,17 @@
1
1
  RSpec::Matchers.define :allow_transition_to do |state|
2
2
  match do |obj|
3
3
  @state_machine_name ||= :default
4
- obj.aasm(@state_machine_name).states(:permitted => true).include?(state)
4
+ obj.aasm(@state_machine_name).states({:permitted => true}, *@args).include?(state)
5
5
  end
6
6
 
7
7
  chain :on do |state_machine_name|
8
8
  @state_machine_name = state_machine_name
9
9
  end
10
10
 
11
+ chain :with do |*args|
12
+ @args = args
13
+ end
14
+
11
15
  description do
12
16
  "allow transition to #{expected} (on :#{@state_machine_name})"
13
17
  end
@@ -2,8 +2,11 @@ RSpec::Matchers.define :transition_from do |from_state|
2
2
  match do |obj|
3
3
  @state_machine_name ||= :default
4
4
  obj.aasm(@state_machine_name).current_state = from_state.to_sym
5
- # expect(obj).to receive(:broadcast_event).with(@event.to_s, obj, from_state, @to_state)
6
- obj.send(@event) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
5
+ begin
6
+ obj.send(@event, *@args) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
7
+ rescue AASM::InvalidTransition
8
+ false
9
+ end
7
10
  end
8
11
 
9
12
  chain :on do |state_machine_name|
@@ -14,12 +17,13 @@ RSpec::Matchers.define :transition_from do |from_state|
14
17
  @to_state = state
15
18
  end
16
19
 
17
- chain :on_event do |event|
20
+ chain :on_event do |event, *args|
18
21
  @event = event
22
+ @args = args
19
23
  end
20
24
 
21
25
  description do
22
- "transition state to :#{@to_state} from :#{expected} on event :#{@event} (on :#{@state_machine_name})"
26
+ "transition state to :#{@to_state} from :#{expected} on event :#{@event}, with params: #{@args} (on :#{@state_machine_name})"
23
27
  end
24
28
 
25
29
  failure_message do |obj|
@@ -1,14 +1,6 @@
1
1
  module AASM
2
2
  class StateMachine
3
-
4
- # the following two methods provide the storage of all state machines
5
- def self.[](klass)
6
- (@machines ||= {})[klass.to_s]
7
- end
8
-
9
- def self.[]=(klass, machine)
10
- (@machines ||= {})[klass.to_s] = machine
11
- end
3
+ # the following four methods provide the storage of all state machines
12
4
 
13
5
  attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks
14
6
 
@@ -24,8 +16,10 @@ module AASM
24
16
  # called internally by Ruby 1.9 after clone()
25
17
  def initialize_copy(orig)
26
18
  super
27
- @states = @states.dup
28
- @events = @events.dup
19
+ @states = orig.states.collect { |state| state.clone }
20
+ @events = {}
21
+ orig.events.each_pair { |name, event| @events[name] = event.clone }
22
+ @global_callbacks = @global_callbacks.dup
29
23
  end
30
24
 
31
25
  def add_state(state_name, klass, options)
@@ -44,7 +38,7 @@ module AASM
44
38
  def add_global_callbacks(name, *callbacks, &block)
45
39
  @global_callbacks[name] ||= []
46
40
  callbacks.each do |callback|
47
- @global_callbacks[name] << callback
41
+ @global_callbacks[name] << callback unless @global_callbacks[name].include? callback
48
42
  end
49
43
  @global_callbacks[name] << block if block
50
44
  end