aasm 4.5.1 → 5.5.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,18 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AASM::Core
2
4
  class Transition
3
- include DslHelper
5
+ include AASM::DslHelper
4
6
 
5
- attr_reader :from, :to, :event, :opts
7
+ attr_reader :from, :to, :event, :opts, :failures
6
8
  alias_method :options, :opts
7
9
 
8
10
  def initialize(event, opts, &block)
9
- add_options_from_dsl(opts, [:on_transition, :guard, :after], &block) if block
11
+ add_options_from_dsl(opts, [:on_transition, :guard, :after, :success], &block) if block
10
12
 
11
13
  @event = event
12
14
  @from = opts[:from]
13
15
  @to = opts[:to]
14
16
  @guards = Array(opts[:guards]) + Array(opts[:guard]) + Array(opts[:if])
15
17
  @unless = Array(opts[:unless]) #TODO: This could use a better name
18
+ @failures = []
16
19
 
17
20
  if opts[:on_transition]
18
21
  warn '[DEPRECATION] :on_transition is deprecated, use :after instead'
@@ -21,9 +24,21 @@ module AASM::Core
21
24
  @after = Array(opts[:after])
22
25
  @after = @after[0] if @after.size == 1
23
26
 
27
+ @success = Array(opts[:success])
28
+ @success = @success[0] if @success.size == 1
29
+
24
30
  @opts = opts
25
31
  end
26
32
 
33
+ # called internally by Ruby 1.9 after clone()
34
+ def initialize_copy(orig)
35
+ super
36
+ @guards = @guards.dup
37
+ @unless = @unless.dup
38
+ @opts = {}
39
+ orig.opts.each_pair { |name, setting| @opts[name] = setting.is_a?(Hash) || setting.is_a?(Array) ? setting.dup : setting }
40
+ end
41
+
27
42
  def allowed?(obj, *args)
28
43
  invoke_callbacks_compatible_with_guard(@guards, obj, args, :guard => true) &&
29
44
  invoke_callbacks_compatible_with_guard(@unless, obj, args, :unless => true)
@@ -42,6 +57,10 @@ module AASM::Core
42
57
  @from == value
43
58
  end
44
59
 
60
+ def invoke_success_callbacks(obj, *args)
61
+ _fire_callbacks(@success, obj, args)
62
+ end
63
+
45
64
  private
46
65
 
47
66
  def invoke_callbacks_compatible_with_guard(code, record, args, options={})
@@ -50,26 +69,14 @@ module AASM::Core
50
69
  record.aasm(event.state_machine.name).to_state = @to if record.aasm(event.state_machine.name).respond_to?(:to_state=)
51
70
  end
52
71
 
53
- case code
54
- when Symbol, String
55
- arity = record.send(:method, code.to_sym).arity
56
- arity == 0 ? record.send(code) : record.send(code, *args)
57
- when Proc
58
- code.arity == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code)
59
- when Array
60
- if options[:guard]
61
- # invoke guard callbacks
62
- code.all? {|a| invoke_callbacks_compatible_with_guard(a, record, args)}
63
- elsif options[:unless]
64
- # invoke unless callbacks
65
- code.all? {|a| !invoke_callbacks_compatible_with_guard(a, record, args)}
66
- else
67
- # invoke after callbacks
68
- code.map {|a| invoke_callbacks_compatible_with_guard(a, record, args)}
69
- end
70
- else
71
- true
72
- end
72
+ Invoker.new(code, record, args)
73
+ .with_options(options)
74
+ .with_failures(failures)
75
+ .invoke
76
+ end
77
+
78
+ def _fire_callbacks(code, record, args)
79
+ Invoker.new(code, record, args).invoke
73
80
  end
74
81
 
75
82
  end
@@ -1,30 +1,32 @@
1
- module DslHelper
1
+ module AASM
2
+ module DslHelper
2
3
 
3
- class Proxy
4
- attr_accessor :options
4
+ class Proxy
5
+ attr_accessor :options
5
6
 
6
- def initialize(options, valid_keys, source)
7
- @valid_keys = valid_keys
8
- @source = source
7
+ def initialize(options, valid_keys, source)
8
+ @valid_keys = valid_keys
9
+ @source = source
9
10
 
10
- @options = options
11
- end
11
+ @options = options
12
+ end
12
13
 
13
- def method_missing(name, *args, &block)
14
- if @valid_keys.include?(name)
15
- options[name] = Array(options[name])
16
- options[name] << block if block
17
- options[name] += Array(args)
18
- else
19
- @source.send name, *args, &block
14
+ def method_missing(name, *args, &block)
15
+ if @valid_keys.include?(name)
16
+ options[name] = Array(options[name])
17
+ options[name] << block if block
18
+ options[name] += Array(args)
19
+ else
20
+ @source.send name, *args, &block
21
+ end
20
22
  end
21
23
  end
22
- end
23
24
 
24
- def add_options_from_dsl(options, valid_keys, &block)
25
- proxy = Proxy.new(options, valid_keys, self)
26
- proxy.instance_eval(&block)
27
- proxy.options
28
- end
25
+ def add_options_from_dsl(options, valid_keys, &block)
26
+ proxy = Proxy.new(options, valid_keys, self)
27
+ proxy.instance_eval(&block)
28
+ proxy.options
29
+ end
29
30
 
30
- end
31
+ end
32
+ end
data/lib/aasm/errors.rb CHANGED
@@ -3,17 +3,20 @@ module AASM
3
3
  class UnknownStateMachineError < RuntimeError; end
4
4
 
5
5
  class InvalidTransition < RuntimeError
6
- attr_reader :object, :event_name, :state_machine_name
6
+ attr_reader :object, :event_name, :originating_state, :failures, :state_machine_name
7
7
 
8
- def initialize(object, event_name, state_machine_name)
9
- @object, @event_name, @state_machine_name = object, event_name, state_machine_name
8
+ def initialize(object, event_name, state_machine_name, failures = [])
9
+ @object, @event_name, @originating_state, @failures = object, event_name, object.aasm(state_machine_name).current_state, failures
10
+ @state_machine_name = state_machine_name
11
+ super("Event '#{event_name}' cannot transition from '#{originating_state}'.#{reasoning}")
10
12
  end
11
13
 
12
- def message
13
- "Event '#{event_name}' cannot transition from '#{object.aasm(state_machine_name).current_state}'"
14
+ def reasoning
15
+ " Failed callback(s): #{failures}." unless failures.empty?
14
16
  end
15
17
  end
16
18
 
17
19
  class UndefinedState < RuntimeError; end
20
+ class UndefinedEvent < UndefinedState; end
18
21
  class NoDirectAssignmentError < RuntimeError; end
19
22
  end
@@ -1,6 +1,5 @@
1
1
  module AASM
2
2
  class InstanceBase
3
-
4
3
  attr_accessor :from_state, :to_state, :current_event
5
4
 
6
5
  def initialize(instance, name=:default) # instance of the class including AASM, name of the state machine
@@ -14,7 +13,6 @@ module AASM
14
13
 
15
14
  def current_state=(state)
16
15
  @instance.aasm_write_state_without_persistence(state, @name)
17
- # @current_state = state
18
16
  end
19
17
 
20
18
  def enter_initial_state
@@ -22,7 +20,6 @@ module AASM
22
20
  state_object = state_object_for_name(state_name)
23
21
 
24
22
  state_object.fire_callbacks(:before_enter, @instance)
25
- # state_object.fire_callbacks(:enter, @instance)
26
23
  self.current_state = state_name
27
24
  state_object.fire_callbacks(:after_enter, @instance)
28
25
 
@@ -30,37 +27,67 @@ module AASM
30
27
  end
31
28
 
32
29
  def human_state
33
- AASM::Localizer.new.human_state_name(@instance.class, state_object_for_name(current_state))
30
+ state_object_for_name(current_state).display_name
34
31
  end
35
32
 
36
- def states(options={})
37
- if options[:permitted]
38
- # ugliness level 1000
39
- permitted_event_names = events(:permitted => true).map(&:name)
40
- transitions = @instance.class.aasm(@name).state_machine.events.values_at(*permitted_event_names).compact.map {|e| e.transitions_from_state(current_state) }
41
- tos = transitions.map {|t| t[0] ? t[0].to : nil}.flatten.compact.map(&:to_sym).uniq
42
- @instance.class.aasm(@name).states.select {|s| tos.include?(s.name.to_sym)}
33
+ def states(options={}, *args)
34
+ if options.has_key?(:permitted)
35
+ selected_events = events({:permitted => options[:permitted]}, *args)
36
+ # An array of arrays. Each inner array represents the transitions that
37
+ # transition from the current state for an event
38
+ event_transitions = selected_events.map {|e| e.transitions_from_state(current_state) }
39
+
40
+ # An array of :to transition states
41
+ to_state_names = event_transitions.map do |transitions|
42
+ return nil if transitions.empty?
43
+
44
+ # Return the :to state of the first transition that is allowed (or not) or nil
45
+ if options[:permitted]
46
+ transition = transitions.find { |t| t.allowed?(@instance, *args) }
47
+ else
48
+ transition = transitions.find { |t| !t.allowed?(@instance, *args) }
49
+ end
50
+ transition ? transition.to : nil
51
+ end.flatten.compact.uniq
52
+
53
+ # Select states that are in to_state_names
54
+ @instance.class.aasm(@name).states.select {|s| to_state_names.include?(s.name)}
43
55
  else
44
56
  @instance.class.aasm(@name).states
45
57
  end
46
58
  end
47
59
 
48
- def events(options={})
60
+ def events(options={}, *args)
49
61
  state = options[:state] || current_state
50
62
  events = @instance.class.aasm(@name).events.select {|e| e.transitions_from_state?(state) }
51
63
 
52
64
  options[:reject] = Array(options[:reject])
53
65
  events.reject! { |e| options[:reject].include?(e.name) }
54
66
 
55
- if options[:permitted]
67
+ if options.has_key?(:permitted)
56
68
  # filters the results of events_for_current_state so that only those that
57
69
  # are really currently possible (given transition guards) are shown.
58
- events.select! { |e| @instance.send("may_#{e.name}?") }
70
+ if options[:permitted]
71
+ events.select! { |e| @instance.send("may_#{e.name}?", *args) }
72
+ else
73
+ events.select! { |e| !@instance.send("may_#{e.name}?", *args) }
74
+ end
59
75
  end
60
76
 
61
77
  events
62
78
  end
63
79
 
80
+ def permitted_transitions
81
+ events(permitted: true).flat_map do |event|
82
+ available_transitions = event.transitions_from_state(current_state)
83
+ allowed_transitions = available_transitions.select { |t| t.allowed?(@instance) }
84
+
85
+ allowed_transitions.map do |transition|
86
+ { event: event.name, state: transition.to }
87
+ end
88
+ end
89
+ end
90
+
64
91
  def state_object_for_name(name)
65
92
  obj = @instance.class.aasm(@name).states.find {|s| s.name == name}
66
93
  raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
@@ -74,7 +101,7 @@ module AASM
74
101
  when Proc
75
102
  state.call(@instance)
76
103
  else
77
- raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc."
104
+ raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc."
78
105
  end
79
106
  end
80
107
 
@@ -86,11 +113,32 @@ module AASM
86
113
  end
87
114
  end
88
115
 
116
+ def fire(event_name, *args, &block)
117
+ event_exists?(event_name)
118
+
119
+ @instance.send(event_name, *args, &block)
120
+ end
121
+
122
+ def fire!(event_name, *args, &block)
123
+ event_exists?(event_name, true)
124
+ bang_event_name = "#{event_name}!".to_sym
125
+ @instance.send(bang_event_name, *args, &block)
126
+ end
127
+
89
128
  def set_current_state_with_persistence(state)
90
129
  save_success = @instance.aasm_write_state(state, @name)
91
130
  self.current_state = state if save_success
92
131
  save_success
93
132
  end
94
133
 
134
+ private
135
+
136
+ def event_exists?(event_name, bang = false)
137
+ event = @instance.class.aasm(@name).state_machine.events[event_name.to_sym]
138
+ return true if event
139
+
140
+ event_error = bang ? "#{event_name}!" : event_name
141
+ raise AASM::UndefinedEvent, "Event :#{event_error} doesn't exist" if event.nil?
142
+ end
95
143
  end
96
144
  end
@@ -5,7 +5,7 @@ module AASM
5
5
  list << :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}"
6
6
  list
7
7
  end
8
- translate_queue(checklist) || I18n.translate(checklist.shift, :default => event.to_s.humanize)
8
+ translate_queue(checklist) || I18n.translate(checklist.shift, :default => default_display_name(event))
9
9
  end
10
10
 
11
11
  def human_state_name(klass, state)
@@ -14,7 +14,7 @@ module AASM
14
14
  list << item_for(klass, state, ancestor, :old_style => true)
15
15
  list
16
16
  end
17
- translate_queue(checklist) || I18n.translate(checklist.shift, :default => state.to_s.humanize)
17
+ translate_queue(checklist) || I18n.translate(checklist.shift, :default => default_display_name(state))
18
18
  end
19
19
 
20
20
  private
@@ -46,8 +46,18 @@ module AASM
46
46
  end
47
47
 
48
48
  def ancestors_list(klass)
49
+ has_active_record_base = defined?(::ActiveRecord::Base)
49
50
  klass.ancestors.select do |ancestor|
50
- ancestor.respond_to?(:model_name) unless ancestor.name == 'ActiveRecord::Base'
51
+ not_active_record_base = has_active_record_base ? (ancestor != ::ActiveRecord::Base) : true
52
+ ancestor.respond_to?(:model_name) && not_active_record_base
53
+ end
54
+ end
55
+
56
+ def default_display_name(object) # Can use better arguement name
57
+ if object.respond_to?(:default_display_name)
58
+ object.default_display_name
59
+ else
60
+ object.to_s.gsub(/_/, ' ').capitalize
51
61
  end
52
62
  end
53
63
  end
@@ -0,0 +1,13 @@
1
+ module Minitest::Assertions
2
+ def assert_event_allowed(object, event, options = {})
3
+ state_machine_name = options.fetch(:on, :default)
4
+ assert object.aasm(state_machine_name).may_fire_event?(event),
5
+ "Expected that the event :#{event} would be allowed (on :#{state_machine_name})"
6
+ end
7
+
8
+ def refute_event_allowed(object, event, options = {})
9
+ state_machine_name = options.fetch(:on, :default)
10
+ refute object.aasm(state_machine_name).may_fire_event?(event),
11
+ "Expected that the event :#{event} would not be allowed (on :#{state_machine_name})"
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Minitest::Assertions
2
+ def assert_transition_to_allowed(object, to_state, options = {})
3
+ state_machine_name = options.fetch(:on, :default)
4
+ assert object.aasm(state_machine_name).states(permitted: true).include?(to_state),
5
+ "Expected that the state :#{to_state} would be reachable (on :#{state_machine_name})"
6
+ end
7
+
8
+ def refute_transition_to_allowed(object, to_state, options = {})
9
+ state_machine_name = options.fetch(:on, :default)
10
+ refute object.aasm(state_machine_name).states(permitted: true).include?(to_state),
11
+ "Expected that the state :#{to_state} would be reachable (on :#{state_machine_name})"
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Minitest::Assertions
2
+ def assert_have_state(object, state, options = {})
3
+ state_machine_name = options.fetch(:on, :default)
4
+ assert object.aasm(state_machine_name).current_state == state,
5
+ "Expected that :#{object.aasm(state_machine_name).current_state} would be :#{state} (on :#{state_machine_name})"
6
+ end
7
+
8
+ def refute_have_state(object, state, options = {})
9
+ state_machine_name = options.fetch(:on, :default)
10
+ refute object.aasm(state_machine_name).current_state == state,
11
+ "Expected that :#{object.aasm(state_machine_name).current_state} would be :#{state} (on :#{state_machine_name})"
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module Minitest::Assertions
2
+ def assert_transitions_from(object, from_state, *args)
3
+ options = args.first
4
+ options[:on] ||= :default
5
+ assert _transitions_from?(object, from_state, args, options),
6
+ "Expected transition state to :#{options[:to]} from :#{from_state} on event :#{options[:on_event]}, (on :#{options[:on]})"
7
+ end
8
+
9
+ def refute_transitions_from(object, from_state, *args)
10
+ options = args.first
11
+ options[:on] ||= :default
12
+ refute _transitions_from?(object, from_state, args, options),
13
+ "Expected transition state to :#{options[:to]} from :#{from_state} on event :#{options[:on_event]}, (on :#{options[:on]})"
14
+ end
15
+
16
+ def _transitions_from?(object, from_state, args, options)
17
+ state_machine_name = options[:on]
18
+ object.aasm(state_machine_name).current_state = from_state.to_sym
19
+ object.send(options[:on_event], *args) && options[:to].to_sym == object.aasm(state_machine_name).current_state
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ Minitest = MiniTest unless defined? Minitest
2
+ # relative-require all minitest_spec files
3
+ Dir[File.dirname(__FILE__) + '/minitest/*.rb'].each do |file|
4
+ require 'aasm/minitest/' + File.basename(file, File.extname(file))
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'aasm/minitest'
2
+
3
+ module Minitest::Expectations
4
+ AASM.infect_an_assertion :assert_transitions_from, :must_transition_from, :do_not_flip
5
+ AASM.infect_an_assertion :refute_transitions_from, :wont_transition_from, :do_not_flip
6
+
7
+ AASM.infect_an_assertion :assert_transition_to_allowed, :must_allow_transition_to, :do_not_flip
8
+ AASM.infect_an_assertion :refute_transition_to_allowed, :wont_allow_transition_to, :do_not_flip
9
+
10
+ AASM.infect_an_assertion :assert_have_state, :must_have_state, :do_not_flip
11
+ AASM.infect_an_assertion :refute_have_state, :wont_have_state, :do_not_flip
12
+
13
+ AASM.infect_an_assertion :assert_event_allowed, :must_allow_event, :do_not_flip
14
+ AASM.infect_an_assertion :refute_event_allowed, :wont_allow_event, :do_not_flip
15
+ end