state_machines-activerecord 0.10.0 → 0.40.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/README.md +15 -1
- data/lib/state_machines/integrations/active_record/locale.rb +10 -9
- data/lib/state_machines/integrations/active_record/version.rb +1 -1
- data/lib/state_machines/integrations/active_record.rb +52 -31
- metadata +35 -138
- data/test/files/en.yml +0 -5
- data/test/files/models/post.rb +0 -13
- data/test/integration_test.rb +0 -27
- data/test/machine_by_default_test.rb +0 -18
- data/test/machine_errors_test.rb +0 -21
- data/test/machine_multiple_test.rb +0 -19
- data/test/machine_nested_action_test.rb +0 -40
- data/test/machine_unmigrated_test.rb +0 -16
- data/test/machine_with_aliased_attribute_test.rb +0 -25
- data/test/machine_with_callbacks_test.rb +0 -174
- data/test/machine_with_column_state_attribute_test.rb +0 -46
- data/test/machine_with_complex_pluralization_scopes_test.rb +0 -18
- data/test/machine_with_conflicting_predicate_test.rb +0 -20
- data/test/machine_with_conflicting_state_name_test.rb +0 -31
- data/test/machine_with_custom_attribute_test.rb +0 -23
- data/test/machine_with_default_scope_test.rb +0 -20
- data/test/machine_with_different_column_default_test.rb +0 -29
- data/test/machine_with_different_integer_column_default_test.rb +0 -31
- data/test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb +0 -26
- data/test/machine_with_dirty_attribute_and_state_events_test.rb +0 -22
- data/test/machine_with_dirty_attributes_and_custom_attribute_test.rb +0 -34
- data/test/machine_with_dirty_attributes_during_loopback_test.rb +0 -24
- data/test/machine_with_dirty_attributes_test.rb +0 -37
- data/test/machine_with_dynamic_initial_state_test.rb +0 -101
- data/test/machine_with_event_attributes_on_autosave_test.rb +0 -50
- data/test/machine_with_event_attributes_on_custom_action_test.rb +0 -43
- data/test/machine_with_event_attributes_on_save_bang_test.rb +0 -84
- data/test/machine_with_event_attributes_on_save_test.rb +0 -246
- data/test/machine_with_event_attributes_on_validation_test.rb +0 -145
- data/test/machine_with_events_test.rb +0 -15
- data/test/machine_with_failed_action_test.rb +0 -42
- data/test/machine_with_failed_after_callbacks_test.rb +0 -37
- data/test/machine_with_failed_before_callbacks_test.rb +0 -38
- data/test/machine_with_initialized_state_test.rb +0 -43
- data/test/machine_with_internationalization_test.rb +0 -182
- data/test/machine_with_loopback_test.rb +0 -24
- data/test/machine_with_non_column_state_attribute_defined_test.rb +0 -31
- data/test/machine_with_same_column_default_test.rb +0 -28
- data/test/machine_with_same_integer_column_default_test.rb +0 -32
- data/test/machine_with_scopes_and_joins_test.rb +0 -40
- data/test/machine_with_scopes_and_owner_subclass_test.rb +0 -29
- data/test/machine_with_scopes_test.rb +0 -72
- data/test/machine_with_state_driven_validations_test.rb +0 -32
- data/test/machine_with_states_test.rb +0 -15
- data/test/machine_with_static_initial_state_test.rb +0 -169
- data/test/machine_with_transactions_test.rb +0 -28
- data/test/machine_with_validations_and_custom_attribute_test.rb +0 -23
- data/test/machine_with_validations_test.rb +0 -49
- data/test/machine_without_database_test.rb +0 -22
- data/test/machine_without_transactions_test.rb +0 -28
- data/test/model_test.rb +0 -14
- data/test/test_helper.rb +0 -57
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineUnmigratedTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model(false)
|
8
|
-
|
9
|
-
# Drop the table so that it definitely doesn't exist
|
10
|
-
@model.connection.drop_table(@model.table_name) if @model.table_exists?
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_should_allow_machine_creation
|
14
|
-
assert_nothing_raised { StateMachines::Machine.new(@model) }
|
15
|
-
end
|
16
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithAliasedAttributeTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model do
|
8
|
-
alias_attribute :vehicle_status, :state
|
9
|
-
end
|
10
|
-
|
11
|
-
@machine = StateMachines::Machine.new(@model, :status, :attribute => :vehicle_status)
|
12
|
-
@machine.state :parked
|
13
|
-
|
14
|
-
@record = @model.new
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_should_check_custom_attribute_for_predicate
|
18
|
-
@record.vehicle_status = nil
|
19
|
-
refute @record.status?(:parked)
|
20
|
-
|
21
|
-
@record.vehicle_status = 'parked'
|
22
|
-
assert @record.status?(:parked)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
@@ -1,174 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithCallbacksTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :initial => :parked)
|
9
|
-
@machine.other_states :idling
|
10
|
-
@machine.event :ignite
|
11
|
-
|
12
|
-
@record = @model.new(:state => 'parked')
|
13
|
-
@transition = StateMachines::Transition.new(@record, @machine, :ignite, :parked, :idling)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_should_run_before_callbacks
|
17
|
-
called = false
|
18
|
-
@machine.before_transition { called = true }
|
19
|
-
|
20
|
-
@transition.perform
|
21
|
-
assert called
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_should_pass_record_to_before_callbacks_with_one_argument
|
25
|
-
record = nil
|
26
|
-
@machine.before_transition { |arg| record = arg }
|
27
|
-
|
28
|
-
@transition.perform
|
29
|
-
assert_equal @record, record
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_should_pass_record_and_transition_to_before_callbacks_with_multiple_arguments
|
33
|
-
callback_args = nil
|
34
|
-
@machine.before_transition { |*args| callback_args = args }
|
35
|
-
|
36
|
-
@transition.perform
|
37
|
-
assert_equal [@record, @transition], callback_args
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_should_run_before_callbacks_outside_the_context_of_the_record
|
41
|
-
context = nil
|
42
|
-
@machine.before_transition { context = self }
|
43
|
-
|
44
|
-
@transition.perform
|
45
|
-
assert_equal self, context
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_should_run_after_callbacks
|
49
|
-
called = false
|
50
|
-
@machine.after_transition { called = true }
|
51
|
-
|
52
|
-
@transition.perform
|
53
|
-
assert called
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_should_pass_record_to_after_callbacks_with_one_argument
|
57
|
-
record = nil
|
58
|
-
@machine.after_transition { |arg| record = arg }
|
59
|
-
|
60
|
-
@transition.perform
|
61
|
-
assert_equal @record, record
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_should_pass_record_and_transition_to_after_callbacks_with_multiple_arguments
|
65
|
-
callback_args = nil
|
66
|
-
@machine.after_transition { |*args| callback_args = args }
|
67
|
-
|
68
|
-
@transition.perform
|
69
|
-
assert_equal [@record, @transition], callback_args
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_should_run_after_callbacks_outside_the_context_of_the_record
|
73
|
-
context = nil
|
74
|
-
@machine.after_transition { context = self }
|
75
|
-
|
76
|
-
@transition.perform
|
77
|
-
assert_equal self, context
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_should_run_after_callbacks_if_model_callback_added_prior_to_state_machine_definition
|
81
|
-
model = new_model do
|
82
|
-
after_save { nil }
|
83
|
-
end
|
84
|
-
machine = StateMachines::Machine.new(model, :initial => :parked)
|
85
|
-
machine.other_states :idling
|
86
|
-
machine.event :ignite
|
87
|
-
after_called = false
|
88
|
-
machine.after_transition { after_called = true }
|
89
|
-
|
90
|
-
record = model.new(:state => 'parked')
|
91
|
-
transition = StateMachines::Transition.new(record, machine, :ignite, :parked, :idling)
|
92
|
-
transition.perform
|
93
|
-
assert_equal true, after_called
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_should_run_around_callbacks
|
97
|
-
before_called = false
|
98
|
-
after_called = false
|
99
|
-
ensure_called = 0
|
100
|
-
@machine.around_transition do |block|
|
101
|
-
before_called = true
|
102
|
-
begin
|
103
|
-
block.call
|
104
|
-
ensure
|
105
|
-
ensure_called += 1
|
106
|
-
end
|
107
|
-
after_called = true
|
108
|
-
end
|
109
|
-
|
110
|
-
@transition.perform
|
111
|
-
assert before_called
|
112
|
-
assert after_called
|
113
|
-
assert_equal ensure_called, 1
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_should_include_transition_states_in_known_states
|
117
|
-
@machine.before_transition :to => :first_gear, :do => lambda {}
|
118
|
-
|
119
|
-
assert_equal [:parked, :idling, :first_gear], @machine.states.map { |state| state.name }
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_should_allow_symbolic_callbacks
|
123
|
-
callback_args = nil
|
124
|
-
|
125
|
-
klass = class << @record;
|
126
|
-
self;
|
127
|
-
end
|
128
|
-
klass.send(:define_method, :after_ignite) do |*args|
|
129
|
-
callback_args = args
|
130
|
-
end
|
131
|
-
|
132
|
-
@machine.before_transition(:after_ignite)
|
133
|
-
|
134
|
-
@transition.perform
|
135
|
-
assert_equal [@transition], callback_args
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_should_allow_string_callbacks
|
139
|
-
class << @record
|
140
|
-
attr_reader :callback_result
|
141
|
-
end
|
142
|
-
|
143
|
-
@machine.before_transition('@callback_result = [1, 2, 3]')
|
144
|
-
@transition.perform
|
145
|
-
|
146
|
-
assert_equal [1, 2, 3], @record.callback_result
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_should_run_in_expected_order
|
150
|
-
expected = [
|
151
|
-
:before_transition, :before_validation, :after_validation,
|
152
|
-
:before_save, :before_create, :after_create, :after_save,
|
153
|
-
:after_transition
|
154
|
-
]
|
155
|
-
|
156
|
-
callbacks = []
|
157
|
-
@model.before_validation { callbacks << :before_validation }
|
158
|
-
@model.after_validation { callbacks << :after_validation }
|
159
|
-
@model.before_save { callbacks << :before_save }
|
160
|
-
@model.before_create { callbacks << :before_create }
|
161
|
-
@model.after_create { callbacks << :after_create }
|
162
|
-
@model.after_save { callbacks << :after_save }
|
163
|
-
@model.after_commit { callbacks << :after_commit }
|
164
|
-
expected << :after_commit
|
165
|
-
|
166
|
-
@machine.before_transition { callbacks << :before_transition }
|
167
|
-
@machine.after_transition { callbacks << :after_transition }
|
168
|
-
|
169
|
-
@transition.perform
|
170
|
-
|
171
|
-
assert_equal expected, callbacks
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithColumnStateAttributeTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :initial => :parked)
|
9
|
-
@machine.other_states(:idling)
|
10
|
-
|
11
|
-
@record = @model.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_should_not_override_the_column_reader
|
15
|
-
@record[:state] = 'parked'
|
16
|
-
assert_equal 'parked', @record.state
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_not_override_the_column_writer
|
20
|
-
@record.state = 'parked'
|
21
|
-
assert_equal 'parked', @record[:state]
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_should_have_an_attribute_predicate
|
25
|
-
assert @record.respond_to?(:state?)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_should_test_for_existence_on_predicate_without_parameters
|
29
|
-
assert @record.state?
|
30
|
-
|
31
|
-
@record.state = nil
|
32
|
-
refute @record.state?
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_should_return_false_for_predicate_if_does_not_match_current_value
|
36
|
-
refute @record.state?(:idling)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_should_return_true_for_predicate_if_matches_current_value
|
40
|
-
assert @record.state?(:parked)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_should_raise_exception_for_predicate_if_invalid_state_specified
|
44
|
-
assert_raises(IndexError) { @record.state?(:invalid) }
|
45
|
-
end
|
46
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithComplexPluralizationScopesTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :status)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_should_create_singular_with_scope
|
12
|
-
assert @model.respond_to?(:with_status)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_should_create_plural_with_scope
|
16
|
-
assert @model.respond_to?(:with_statuses)
|
17
|
-
end
|
18
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithConflictingPredicateTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model do
|
8
|
-
def state?(*args)
|
9
|
-
true
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
@machine = StateMachines::Machine.new(@model)
|
14
|
-
@record = @model.new
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_should_not_define_attribute_predicate
|
18
|
-
assert @record.state?
|
19
|
-
end
|
20
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
require 'stringio'
|
5
|
-
|
6
|
-
class MachineWithConflictingStateNameTest < BaseTestCase
|
7
|
-
def setup
|
8
|
-
@original_stderr, $stderr = $stderr, StringIO.new
|
9
|
-
|
10
|
-
@model = new_model
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_should_output_warning_with_same_machine_name
|
14
|
-
@machine = StateMachines::Machine.new(@model)
|
15
|
-
@machine.state :state
|
16
|
-
|
17
|
-
assert_match(/^Instance method "state\?" is already defined in .+, use generic helper instead.*\n$/, $stderr.string)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_should_output_warning_with_same_machine_attribute
|
21
|
-
@machine = StateMachines::Machine.new(@model, :public_state, :attribute => :state)
|
22
|
-
@machine.state :state
|
23
|
-
|
24
|
-
assert_match(/^Instance method "state\?" is already defined in .+, use generic helper instead.*\n$/, $stderr.string)
|
25
|
-
end
|
26
|
-
|
27
|
-
def teardown
|
28
|
-
$stderr = @original_stderr
|
29
|
-
super
|
30
|
-
end
|
31
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
require 'stringio'
|
5
|
-
|
6
|
-
class MachineWithCustomAttributeTest < BaseTestCase
|
7
|
-
def setup
|
8
|
-
@original_stderr, $stderr = $stderr, StringIO.new
|
9
|
-
|
10
|
-
@model = new_model
|
11
|
-
@machine = StateMachines::Machine.new(@model, :public_state, :attribute => :state)
|
12
|
-
@record = @model.new
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_should_not_delegate_attribute_predicate_with_different_attribute
|
16
|
-
assert_raise(ArgumentError) { @record.public_state? }
|
17
|
-
end
|
18
|
-
|
19
|
-
def teardown
|
20
|
-
$stderr = @original_stderr
|
21
|
-
super
|
22
|
-
end
|
23
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithDefaultScopeTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :initial => :parked)
|
9
|
-
@machine.state :idling
|
10
|
-
|
11
|
-
@model.class_eval do
|
12
|
-
default_scope { with_state(:parked, :idling) }
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_should_set_initial_state_on_created_object
|
17
|
-
object = @model.new
|
18
|
-
assert_equal 'parked', object.state
|
19
|
-
end
|
20
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
require 'stringio'
|
5
|
-
|
6
|
-
class MachineWithDifferentColumnDefaultTest < BaseTestCase
|
7
|
-
def setup
|
8
|
-
@original_stderr, $stderr = $stderr, StringIO.new
|
9
|
-
|
10
|
-
@model = new_model do
|
11
|
-
connection.add_column table_name, :status, :string, :default => 'idling'
|
12
|
-
end
|
13
|
-
@machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
|
14
|
-
@record = @model.new
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_should_use_machine_default
|
18
|
-
assert_equal 'parked', @record.status
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_should_generate_a_warning
|
22
|
-
assert_match(/Both Foo and its :status machine have defined a different default for "status". Use only one or the other for defining defaults to avoid unexpected behaviors\./, $stderr.string)
|
23
|
-
end
|
24
|
-
|
25
|
-
def teardown
|
26
|
-
$stderr = @original_stderr
|
27
|
-
super
|
28
|
-
end
|
29
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
require 'stringio'
|
5
|
-
|
6
|
-
class MachineWithDifferentIntegerColumnDefaultTest < BaseTestCase
|
7
|
-
def setup
|
8
|
-
@original_stderr, $stderr = $stderr, StringIO.new
|
9
|
-
|
10
|
-
@model = new_model do
|
11
|
-
connection.add_column table_name, :status, :integer, :default => 0
|
12
|
-
end
|
13
|
-
@machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
|
14
|
-
@machine.state :parked, :value => 1
|
15
|
-
@record = @model.new
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_should_use_machine_default
|
19
|
-
assert_equal 1, @record.status
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_should_generate_a_warning
|
23
|
-
assert_match(/Both Foo and its :status machine have defined a different default for "status". Use only one or the other for defining defaults to avoid unexpected behaviors\./, $stderr.string)
|
24
|
-
end
|
25
|
-
|
26
|
-
def teardown
|
27
|
-
$stderr = @original_stderr
|
28
|
-
super
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithDirtyAttributeAndCustomAttributesDuringLoopbackTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model do
|
8
|
-
connection.add_column table_name, :status, :string
|
9
|
-
end
|
10
|
-
@machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
|
11
|
-
@machine.event :park
|
12
|
-
|
13
|
-
@record = @model.create
|
14
|
-
|
15
|
-
@transition = StateMachines::Transition.new(@record, @machine, :park, :parked, :parked)
|
16
|
-
@transition.perform(false)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_not_include_state_in_changed_attributes
|
20
|
-
assert_equal [], @record.changed
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_should_not_track_attribute_changes
|
24
|
-
assert_nil @record.changes['status']
|
25
|
-
end
|
26
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithDirtyAttributeAndStateEventsTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :initial => :parked)
|
9
|
-
@machine.event :ignite
|
10
|
-
|
11
|
-
@record = @model.create
|
12
|
-
@record.state_event = 'ignite'
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_should_not_include_state_in_changed_attributes
|
16
|
-
assert_equal [], @record.changed
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_not_track_attribute_change
|
20
|
-
assert_nil @record.changes['state']
|
21
|
-
end
|
22
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithDirtyAttributesAndCustomAttributeTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model do
|
8
|
-
connection.add_column table_name, :status, :string
|
9
|
-
end
|
10
|
-
@machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
|
11
|
-
@machine.event :ignite
|
12
|
-
@machine.state :idling
|
13
|
-
|
14
|
-
@record = @model.create
|
15
|
-
|
16
|
-
@transition = StateMachines::Transition.new(@record, @machine, :ignite, :parked, :idling)
|
17
|
-
@transition.perform(false)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_should_include_state_in_changed_attributes
|
21
|
-
assert_equal %w(status), @record.changed
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_should_track_attribute_change
|
25
|
-
assert_equal %w(parked idling), @record.changes['status']
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_should_not_reset_changes_on_multiple_transitions
|
29
|
-
transition = StateMachines::Transition.new(@record, @machine, :ignite, :idling, :idling)
|
30
|
-
transition.perform(false)
|
31
|
-
|
32
|
-
assert_equal %w(parked idling), @record.changes['status']
|
33
|
-
end
|
34
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithDirtyAttributesDuringLoopbackTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :initial => :parked)
|
9
|
-
@machine.event :park
|
10
|
-
|
11
|
-
@record = @model.create
|
12
|
-
|
13
|
-
@transition = StateMachines::Transition.new(@record, @machine, :park, :parked, :parked)
|
14
|
-
@transition.perform(false)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_should_not_include_state_in_changed_attributes
|
18
|
-
assert_equal [], @record.changed
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_should_not_track_attribute_changes
|
22
|
-
assert_nil @record.changes['state']
|
23
|
-
end
|
24
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithDirtyAttributesTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :initial => :parked)
|
9
|
-
@machine.event :ignite
|
10
|
-
@machine.state :idling
|
11
|
-
|
12
|
-
@record = @model.create
|
13
|
-
|
14
|
-
@transition = StateMachines::Transition.new(@record, @machine, :ignite, :parked, :idling)
|
15
|
-
@transition.perform(false)
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_should_include_state_in_changed_attributes
|
19
|
-
assert_equal %w(state), @record.changed
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_should_track_attribute_change
|
23
|
-
assert_equal %w(parked idling), @record.changes['state']
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_should_not_reset_changes_on_multiple_transitions
|
27
|
-
transition = StateMachines::Transition.new(@record, @machine, :ignite, :idling, :idling)
|
28
|
-
transition.perform(false)
|
29
|
-
|
30
|
-
assert_equal %w(parked idling), @record.changes['state']
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_should_not_have_changes_when_loaded_from_database
|
34
|
-
record = @model.find(@record.id)
|
35
|
-
refute record.changed?
|
36
|
-
end
|
37
|
-
end
|
@@ -1,101 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithDynamicInitialStateTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model do
|
8
|
-
attr_accessor :value
|
9
|
-
end
|
10
|
-
@machine = StateMachines::Machine.new(@model, :initial => lambda { |object| :parked })
|
11
|
-
@machine.state :parked
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_should_set_initial_state_on_created_object
|
15
|
-
record = @model.new
|
16
|
-
assert_equal 'parked', record.state
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_still_set_attributes
|
20
|
-
record = @model.new(:value => 1)
|
21
|
-
assert_equal 1, record.value
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_should_still_allow_initialize_blocks
|
25
|
-
block_args = nil
|
26
|
-
record = @model.new do |*args|
|
27
|
-
block_args = args
|
28
|
-
end
|
29
|
-
|
30
|
-
assert_equal [record], block_args
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_should_set_attributes_prior_to_initialize_block
|
34
|
-
state = nil
|
35
|
-
@model.new do |record|
|
36
|
-
state = record.state
|
37
|
-
end
|
38
|
-
|
39
|
-
assert_equal 'parked', state
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_should_set_attributes_prior_to_after_initialize_hook
|
43
|
-
state = nil
|
44
|
-
@model.after_initialize do |record|
|
45
|
-
state = record.state
|
46
|
-
end
|
47
|
-
@model.new
|
48
|
-
assert_equal 'parked', state
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_should_set_initial_state_after_setting_attributes
|
52
|
-
@model.class_eval do
|
53
|
-
attr_accessor :state_during_setter
|
54
|
-
|
55
|
-
remove_method :value=
|
56
|
-
define_method(:value=) do |value|
|
57
|
-
self.state_during_setter = state || 'nil'
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
record = @model.new(:value => 1)
|
62
|
-
assert_equal 'nil', record.state_during_setter
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_should_not_set_initial_state_after_already_initialized
|
66
|
-
record = @model.new(:value => 1)
|
67
|
-
assert_equal 'parked', record.state
|
68
|
-
|
69
|
-
record.state = 'idling'
|
70
|
-
record.attributes = {}
|
71
|
-
assert_equal 'idling', record.state
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_should_persist_initial_state
|
75
|
-
record = @model.new
|
76
|
-
record.save
|
77
|
-
record.reload
|
78
|
-
assert_equal 'parked', record.state
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_should_persist_initial_state_on_dup
|
82
|
-
record = @model.create.dup
|
83
|
-
record.save
|
84
|
-
record.reload
|
85
|
-
assert_equal 'parked', record.state
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_should_use_stored_values_when_loading_from_database
|
89
|
-
@machine.state :idling
|
90
|
-
|
91
|
-
record = @model.find(@model.create(:state => 'idling').id)
|
92
|
-
assert_equal 'idling', record.state
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_should_use_stored_values_when_loading_from_database_with_nil_state
|
96
|
-
@machine.state nil
|
97
|
-
|
98
|
-
record = @model.find(@model.create(:state => nil).id)
|
99
|
-
assert_nil record.state
|
100
|
-
end
|
101
|
-
end
|