state_machines-mongoid 0.0.0 → 0.1.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.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.travis.yml +20 -0
- data/Appraisals +8 -0
- data/Gemfile +0 -2
- data/LICENSE.txt +1 -0
- data/README.md +43 -4
- data/Rakefile +8 -1
- data/gemfiles/active_model_4.1.gemfile +7 -0
- data/gemfiles/active_model_4.2.gemfile +7 -0
- data/lib/state_machines-mongoid.rb +1 -0
- data/lib/state_machines/integrations/mongoid.rb +401 -0
- data/lib/state_machines/integrations/mongoid/locale.rb +11 -0
- data/lib/state_machines/integrations/mongoid/version.rb +7 -0
- data/lib/state_machines/state_machines-mongoid.rb +1 -0
- data/state_machines-mongoid.gemspec +18 -12
- data/test/files/en.yml +5 -0
- data/test/integration_test.rb +23 -0
- data/test/machine_by_default_test.rb +16 -0
- data/test/machine_errors_test.rb +20 -0
- data/test/machine_multiple_test.rb +17 -0
- data/test/machine_nested_action_test.rb +39 -0
- data/test/machine_with_aliased_attribute_test.rb +22 -0
- data/test/machine_with_aliased_field_test.rb +22 -0
- data/test/machine_with_callbacks_test.rb +167 -0
- data/test/machine_with_column_state_attribute_test.rb +44 -0
- data/test/machine_with_complex_pluralization_scopes_test.rb +16 -0
- data/test/machine_with_conflicting_predicate_test.rb +27 -0
- data/test/machine_with_conflicting_state_name_test.rb +29 -0
- data/test/machine_with_custom_attribute_test.rb +21 -0
- data/test/machine_with_default_scope_test.rb +14 -0
- data/test/machine_with_different_column_default_test.rb +26 -0
- data/test/machine_with_different_integer_column_default_test.rb +27 -0
- data/test/machine_with_different_same_default_test.rb +26 -0
- data/test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb +24 -0
- data/test/machine_with_dirty_attribute_and_state_events_test.rb +20 -0
- data/test/machine_with_dirty_attributes_and_custom_attribute_test.rb +32 -0
- data/test/machine_with_dirty_attributes_during_loopback_test.rb +22 -0
- data/test/machine_with_dirty_attributes_test.rb +35 -0
- data/test/machine_with_dynamic_initial_state_test.rb +99 -0
- data/test/machine_with_event_attributes_on_autosave_test.rb +50 -0
- data/test/machine_with_event_attributes_on_custom_action_test.rb +40 -0
- data/test/machine_with_event_attributes_on_save_bang_test.rb +82 -0
- data/test/machine_with_event_attributes_on_save_test.rb +146 -0
- data/test/machine_with_event_attributes_on_validation_test.rb +115 -0
- data/test/machine_with_events_test.rb +13 -0
- data/test/machine_with_failed_action_test.rb +39 -0
- data/test/machine_with_failed_after_callbacks_test.rb +35 -0
- data/test/machine_with_failed_before_callbacks_test.rb +36 -0
- data/test/machine_with_field_test.rb +16 -0
- data/test/machine_with_initialized_state_test.rb +35 -0
- data/test/machine_with_internationalization_test.rb +175 -0
- data/test/machine_with_loopback_test.rb +26 -0
- data/test/machine_with_non_column_state_attribute_defined_test.rb +35 -0
- data/test/machine_with_non_column_state_attribute_undefined_test.rb +33 -0
- data/test/machine_with_scopes_and_owner_subclass_test.rb +42 -0
- data/test/machine_with_scopes_test.rb +69 -0
- data/test/machine_with_state_driven_validations_test.rb +33 -0
- data/test/machine_with_state_test.rb +13 -0
- data/test/machine_with_static_initial_state_test.rb +162 -0
- data/test/machine_with_validations_and_custom_attribute_test.rb +21 -0
- data/test/machine_with_validations_test.rb +46 -0
- data/test/machine_without_field_test.rb +14 -0
- data/test/test_helper.rb +90 -0
- metadata +170 -9
- data/lib/state_machines/mongoid.rb +0 -7
- data/lib/state_machines/mongoid/version.rb +0 -5
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class MachineWithEventAttributesOnCustomActionTest < BaseTestCase
|
4
|
+
def setup
|
5
|
+
@superclass = new_model do
|
6
|
+
def persist
|
7
|
+
upsert
|
8
|
+
end
|
9
|
+
end
|
10
|
+
@model = Class.new(@superclass)
|
11
|
+
@machine = StateMachines::Machine.new(@model, :action => :persist)
|
12
|
+
@machine.event :ignite do
|
13
|
+
transition :parked => :idling
|
14
|
+
end
|
15
|
+
|
16
|
+
@record = @model.new
|
17
|
+
@record.state = 'parked'
|
18
|
+
@record.state_event = 'ignite'
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_not_transition_on_valid?
|
22
|
+
@record.valid?
|
23
|
+
assert_equal 'parked', @record.state
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_not_transition_on_save
|
27
|
+
@record.save
|
28
|
+
assert_equal 'parked', @record.state
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_not_transition_on_save!
|
32
|
+
@record.save!
|
33
|
+
assert_equal 'parked', @record.state
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_transition_on_custom_action
|
37
|
+
@record.persist
|
38
|
+
assert_equal 'idling', @record.state
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class MachineWithEventAttributesOnSaveBangTest < BaseTestCase
|
4
|
+
def setup
|
5
|
+
@model = new_model
|
6
|
+
@machine = StateMachines::Machine.new(@model)
|
7
|
+
@machine.event :ignite do
|
8
|
+
transition :parked => :idling
|
9
|
+
end
|
10
|
+
|
11
|
+
@record = @model.new
|
12
|
+
@record.state = 'parked'
|
13
|
+
@record.state_event = 'ignite'
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_fail_if_event_is_invalid
|
17
|
+
@record.state_event = 'invalid'
|
18
|
+
assert_raises(Mongoid::Errors::Validations) { @record.save! }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_fail_if_event_has_no_transition
|
22
|
+
@record.state = 'idling'
|
23
|
+
assert_raises(Mongoid::Errors::Validations) { @record.save! }
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_be_successful_if_event_has_transition
|
27
|
+
assert_equal true, @record.save!
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_run_before_callbacks
|
31
|
+
ran_callback = false
|
32
|
+
@machine.before_transition { ran_callback = true }
|
33
|
+
|
34
|
+
@record.save!
|
35
|
+
assert ran_callback
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_should_run_before_callbacks_once
|
39
|
+
before_count = 0
|
40
|
+
@machine.before_transition { before_count += 1 }
|
41
|
+
|
42
|
+
@record.save!
|
43
|
+
assert_equal 1, before_count
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_run_around_callbacks_before_yield
|
47
|
+
ran_callback = false
|
48
|
+
@machine.around_transition {|block| ran_callback = true; block.call }
|
49
|
+
|
50
|
+
@record.save!
|
51
|
+
assert ran_callback
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_run_around_callbacks_before_yield_once
|
55
|
+
around_before_count = 0
|
56
|
+
@machine.around_transition {|block| around_before_count += 1; block.call }
|
57
|
+
|
58
|
+
@record.save!
|
59
|
+
assert_equal 1, around_before_count
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_persist_new_state
|
63
|
+
@record.save!
|
64
|
+
assert_equal 'idling', @record.state
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_should_run_after_callbacks
|
68
|
+
ran_callback = false
|
69
|
+
@machine.after_transition { ran_callback = true }
|
70
|
+
|
71
|
+
@record.save!
|
72
|
+
assert ran_callback
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_should_run_around_callbacks_after_yield
|
76
|
+
ran_callback = false
|
77
|
+
@machine.around_transition {|block| block.call; ran_callback = true }
|
78
|
+
|
79
|
+
@record.save!
|
80
|
+
assert ran_callback
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class MachineWithEventAttributesOnSaveTest < BaseTestCase
|
4
|
+
def setup
|
5
|
+
@model = new_model
|
6
|
+
@machine = StateMachines::Machine.new(@model)
|
7
|
+
@machine.event :ignite do
|
8
|
+
transition :parked => :idling
|
9
|
+
end
|
10
|
+
|
11
|
+
@record = @model.new
|
12
|
+
@record.state = 'parked'
|
13
|
+
@record.state_event = 'ignite'
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_fail_if_event_is_invalid
|
17
|
+
@record.state_event = 'invalid'
|
18
|
+
assert_equal false, @record.save
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_fail_if_event_has_no_transition
|
22
|
+
@record.state = 'idling'
|
23
|
+
assert_equal false, @record.save
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_be_successful_if_event_has_transition
|
27
|
+
assert_equal true, @record.save
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_run_before_callbacks
|
31
|
+
ran_callback = false
|
32
|
+
@machine.before_transition { ran_callback = true }
|
33
|
+
|
34
|
+
@record.save
|
35
|
+
assert ran_callback
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_should_run_before_callbacks_once
|
39
|
+
before_count = 0
|
40
|
+
@machine.before_transition { before_count += 1 }
|
41
|
+
|
42
|
+
@record.save
|
43
|
+
assert_equal 1, before_count
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_run_around_callbacks_before_yield
|
47
|
+
ran_callback = false
|
48
|
+
@machine.around_transition {|block| ran_callback = true; block.call }
|
49
|
+
|
50
|
+
@record.save
|
51
|
+
assert ran_callback
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_run_around_callbacks_before_yield_once
|
55
|
+
around_before_count = 0
|
56
|
+
@machine.around_transition {|block| around_before_count += 1; block.call }
|
57
|
+
|
58
|
+
@record.save
|
59
|
+
assert_equal 1, around_before_count
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_persist_new_state
|
63
|
+
@record.save
|
64
|
+
assert_equal 'idling', @record.state
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_should_run_after_callbacks
|
68
|
+
ran_callback = false
|
69
|
+
@machine.after_transition { ran_callback = true }
|
70
|
+
|
71
|
+
@record.save
|
72
|
+
assert ran_callback
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_should_not_run_after_callbacks_with_failures_disabled_if_fails
|
76
|
+
@model.class_eval do
|
77
|
+
validates_numericality_of :state
|
78
|
+
end
|
79
|
+
|
80
|
+
ran_callback = false
|
81
|
+
@machine.after_transition { ran_callback = true }
|
82
|
+
|
83
|
+
begin; @record.save; rescue; end
|
84
|
+
assert !ran_callback
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_should_run_failure_callbacks__if_fails
|
88
|
+
@model.class_eval do
|
89
|
+
validates_numericality_of :state
|
90
|
+
end
|
91
|
+
|
92
|
+
ran_callback = false
|
93
|
+
@machine.after_failure { ran_callback = true }
|
94
|
+
|
95
|
+
begin; @record.save; rescue; end
|
96
|
+
assert ran_callback
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_should_not_run_around_callbacks_with_failures_disabled_if_fails
|
100
|
+
@model.class_eval do
|
101
|
+
validates_numericality_of :state
|
102
|
+
end
|
103
|
+
|
104
|
+
ran_callback = false
|
105
|
+
@machine.around_transition {|block| block.call; ran_callback = true }
|
106
|
+
|
107
|
+
begin; @record.save; rescue; end
|
108
|
+
assert !ran_callback
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_should_run_around_callbacks_after_yield
|
112
|
+
ran_callback = false
|
113
|
+
@machine.around_transition {|block| block.call; ran_callback = true }
|
114
|
+
|
115
|
+
@record.save
|
116
|
+
assert ran_callback
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_should_allow_additional_transitions_to_new_state_in_after_transitions
|
120
|
+
@machine.event :park do
|
121
|
+
transition :idling => :parked
|
122
|
+
end
|
123
|
+
|
124
|
+
@machine.after_transition(:on => :ignite) { @record.park }
|
125
|
+
|
126
|
+
@record.save
|
127
|
+
assert_equal 'parked', @record.state
|
128
|
+
|
129
|
+
@record.reload
|
130
|
+
assert_equal 'parked', @record.state
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_should_allow_additional_transitions_to_previous_state_in_after_transitions
|
134
|
+
@machine.event :shift_up do
|
135
|
+
transition :idling => :first_gear
|
136
|
+
end
|
137
|
+
|
138
|
+
@machine.after_transition(:on => :ignite) { @record.shift_up }
|
139
|
+
|
140
|
+
@record.save
|
141
|
+
assert_equal 'first_gear', @record.state
|
142
|
+
|
143
|
+
@record.reload
|
144
|
+
assert_equal 'first_gear', @record.state
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class MachineWithEventAttributesOnValidationTest < BaseTestCase
|
4
|
+
def setup
|
5
|
+
@model = new_model
|
6
|
+
@machine = StateMachines::Machine.new(@model)
|
7
|
+
@machine.event :ignite do
|
8
|
+
transition :parked => :idling
|
9
|
+
end
|
10
|
+
|
11
|
+
@record = @model.new
|
12
|
+
@record.state = 'parked'
|
13
|
+
@record.state_event = 'ignite'
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_fail_if_event_is_invalid
|
17
|
+
@record.state_event = 'invalid'
|
18
|
+
assert !@record.valid?
|
19
|
+
assert_equal ['State event is invalid'], @record.errors.full_messages
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_fail_if_event_has_no_transition
|
23
|
+
@record.state = 'idling'
|
24
|
+
assert !@record.valid?
|
25
|
+
assert_equal ['State event cannot transition when idling'], @record.errors.full_messages
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_be_successful_if_event_has_transition
|
29
|
+
assert @record.valid?
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_run_before_callbacks
|
33
|
+
ran_callback = false
|
34
|
+
@machine.before_transition { ran_callback = true }
|
35
|
+
|
36
|
+
@record.valid?
|
37
|
+
assert ran_callback
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_run_around_callbacks_before_yield
|
41
|
+
ran_callback = false
|
42
|
+
@machine.around_transition {|block| ran_callback = true; block.call }
|
43
|
+
|
44
|
+
begin
|
45
|
+
@record.valid?
|
46
|
+
rescue ArgumentError
|
47
|
+
raise if StateMachines::Transition.pause_supported?
|
48
|
+
end
|
49
|
+
assert ran_callback
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_should_persist_new_state
|
53
|
+
@record.valid?
|
54
|
+
assert_equal 'idling', @record.state
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_not_run_after_callbacks
|
58
|
+
ran_callback = false
|
59
|
+
@machine.after_transition { ran_callback = true }
|
60
|
+
|
61
|
+
@record.valid?
|
62
|
+
assert !ran_callback
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_should_not_run_after_callbacks_with_failures_disabled_if_validation_fails
|
66
|
+
@model.class_eval do
|
67
|
+
attr_accessor :seatbelt
|
68
|
+
validates_presence_of :seatbelt
|
69
|
+
end
|
70
|
+
|
71
|
+
ran_callback = false
|
72
|
+
@machine.after_transition { ran_callback = true }
|
73
|
+
|
74
|
+
@record.valid?
|
75
|
+
assert !ran_callback
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_should_not_run_around_callbacks_after_yield
|
79
|
+
ran_callback = false
|
80
|
+
@machine.around_transition {|block| block.call; ran_callback = true }
|
81
|
+
|
82
|
+
begin
|
83
|
+
@record.valid?
|
84
|
+
rescue ArgumentError
|
85
|
+
raise if StateMachines::Transition.pause_supported?
|
86
|
+
end
|
87
|
+
assert !ran_callback
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_should_not_run_around_callbacks_after_yield_with_failures_disabled_if_validation_fails
|
91
|
+
@model.class_eval do
|
92
|
+
attr_accessor :seatbelt
|
93
|
+
validates_presence_of :seatbelt
|
94
|
+
end
|
95
|
+
|
96
|
+
ran_callback = false
|
97
|
+
@machine.around_transition {|block| block.call; ran_callback = true }
|
98
|
+
|
99
|
+
@record.valid?
|
100
|
+
assert !ran_callback
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_should_run_failure_callbacks_if_validation_fails
|
104
|
+
@model.class_eval do
|
105
|
+
attr_accessor :seatbelt
|
106
|
+
validates_presence_of :seatbelt
|
107
|
+
end
|
108
|
+
|
109
|
+
ran_callback = false
|
110
|
+
@machine.after_failure { ran_callback = true }
|
111
|
+
|
112
|
+
@record.valid?
|
113
|
+
assert ran_callback
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class MachineWithEventsTest < BaseTestCase
|
4
|
+
def setup
|
5
|
+
@model = new_model
|
6
|
+
@machine = StateMachines::Machine.new(@model)
|
7
|
+
@machine.event :shift_up
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_should_humanize_name
|
11
|
+
assert_equal 'shift up', @machine.event(:shift_up).human_name
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class MachineWithFailedActionTest < BaseTestCase
|
4
|
+
def setup
|
5
|
+
@model = new_model do
|
6
|
+
validates_numericality_of :state
|
7
|
+
end
|
8
|
+
|
9
|
+
@machine = StateMachines::Machine.new(@model)
|
10
|
+
@machine.state :parked, :idling
|
11
|
+
@machine.event :ignite
|
12
|
+
|
13
|
+
@callbacks = []
|
14
|
+
@machine.before_transition {@callbacks << :before}
|
15
|
+
@machine.after_transition {@callbacks << :after}
|
16
|
+
@machine.after_failure {@callbacks << :after_failure}
|
17
|
+
@machine.around_transition {|block| @callbacks << :around_before; block.call; @callbacks << :around_after}
|
18
|
+
|
19
|
+
@record = @model.new(:state => 'parked')
|
20
|
+
@transition = StateMachines::Transition.new(@record, @machine, :ignite, :parked, :idling)
|
21
|
+
@result = @transition.perform
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_not_be_successful
|
25
|
+
assert !@result
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_not_change_current_state
|
29
|
+
assert_equal 'parked', @record.state
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_not_save_record
|
33
|
+
assert @record.new_record?
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_run_before_callbacks_and_after_callbacks_with_failures
|
37
|
+
assert_equal [:before, :around_before, :after_failure], @callbacks
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class MachineWithFailedAfterCallbacksTest < BaseTestCase
|
4
|
+
def setup
|
5
|
+
@callbacks = []
|
6
|
+
|
7
|
+
@model = new_model
|
8
|
+
@machine = StateMachines::Machine.new(@model)
|
9
|
+
@machine.state :parked, :idling
|
10
|
+
@machine.event :ignite
|
11
|
+
@machine.after_transition {@callbacks << :after_1; false}
|
12
|
+
@machine.after_transition {@callbacks << :after_2}
|
13
|
+
@machine.around_transition {|block| @callbacks << :around_before; block.call; @callbacks << :around_after}
|
14
|
+
|
15
|
+
@record = @model.new(:state => 'parked')
|
16
|
+
@transition = StateMachines::Transition.new(@record, @machine, :ignite, :parked, :idling)
|
17
|
+
@result = @transition.perform
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_be_successful
|
21
|
+
assert @result
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_change_current_state
|
25
|
+
assert_equal 'idling', @record.state
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_save_record
|
29
|
+
assert !@record.new_record?
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_not_run_further_after_callbacks
|
33
|
+
assert_equal [:around_before, :around_after, :after_1], @callbacks
|
34
|
+
end
|
35
|
+
end
|