state_machines-activerecord 0.9.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.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +16 -3
  4. data/lib/state_machines/integrations/active_record/locale.rb +12 -9
  5. data/lib/state_machines/integrations/active_record/version.rb +3 -1
  6. data/lib/state_machines/integrations/active_record.rb +71 -109
  7. data/lib/state_machines-activerecord.rb +2 -0
  8. metadata +36 -142
  9. data/test/files/en.yml +0 -5
  10. data/test/files/models/post.rb +0 -11
  11. data/test/integration_test.rb +0 -25
  12. data/test/machine_by_default_test.rb +0 -16
  13. data/test/machine_errors_test.rb +0 -19
  14. data/test/machine_multiple_test.rb +0 -17
  15. data/test/machine_nested_action_test.rb +0 -38
  16. data/test/machine_unmigrated_test.rb +0 -14
  17. data/test/machine_with_aliased_attribute_test.rb +0 -23
  18. data/test/machine_with_callbacks_test.rb +0 -172
  19. data/test/machine_with_column_state_attribute_test.rb +0 -44
  20. data/test/machine_with_complex_pluralization_scopes_test.rb +0 -16
  21. data/test/machine_with_conflicting_predicate_test.rb +0 -18
  22. data/test/machine_with_conflicting_state_name_test.rb +0 -29
  23. data/test/machine_with_custom_attribute_test.rb +0 -21
  24. data/test/machine_with_default_scope_test.rb +0 -18
  25. data/test/machine_with_different_column_default_test.rb +0 -27
  26. data/test/machine_with_different_integer_column_default_test.rb +0 -29
  27. data/test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb +0 -24
  28. data/test/machine_with_dirty_attribute_and_state_events_test.rb +0 -20
  29. data/test/machine_with_dirty_attributes_and_custom_attribute_test.rb +0 -32
  30. data/test/machine_with_dirty_attributes_during_loopback_test.rb +0 -22
  31. data/test/machine_with_dirty_attributes_test.rb +0 -35
  32. data/test/machine_with_dynamic_initial_state_test.rb +0 -99
  33. data/test/machine_with_event_attributes_on_autosave_test.rb +0 -48
  34. data/test/machine_with_event_attributes_on_custom_action_test.rb +0 -41
  35. data/test/machine_with_event_attributes_on_save_bang_test.rb +0 -82
  36. data/test/machine_with_event_attributes_on_save_test.rb +0 -244
  37. data/test/machine_with_event_attributes_on_validation_test.rb +0 -143
  38. data/test/machine_with_events_test.rb +0 -13
  39. data/test/machine_with_failed_action_test.rb +0 -40
  40. data/test/machine_with_failed_after_callbacks_test.rb +0 -35
  41. data/test/machine_with_failed_before_callbacks_test.rb +0 -36
  42. data/test/machine_with_initialized_state_test.rb +0 -41
  43. data/test/machine_with_internationalization_test.rb +0 -180
  44. data/test/machine_with_loopback_test.rb +0 -22
  45. data/test/machine_with_non_column_state_attribute_defined_test.rb +0 -29
  46. data/test/machine_with_same_column_default_test.rb +0 -26
  47. data/test/machine_with_same_integer_column_default_test.rb +0 -30
  48. data/test/machine_with_scopes_and_joins_test.rb +0 -38
  49. data/test/machine_with_scopes_and_owner_subclass_test.rb +0 -27
  50. data/test/machine_with_scopes_test.rb +0 -70
  51. data/test/machine_with_state_driven_validations_test.rb +0 -30
  52. data/test/machine_with_states_test.rb +0 -13
  53. data/test/machine_with_static_initial_state_test.rb +0 -167
  54. data/test/machine_with_transactions_test.rb +0 -26
  55. data/test/machine_with_validations_and_custom_attribute_test.rb +0 -21
  56. data/test/machine_with_validations_test.rb +0 -47
  57. data/test/machine_without_database_test.rb +0 -20
  58. data/test/machine_without_transactions_test.rb +0 -26
  59. data/test/model_test.rb +0 -12
  60. data/test/test_helper.rb +0 -58
@@ -1,180 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithInternationalizationTest < BaseTestCase
4
- def setup
5
- I18n.backend = I18n::Backend::Simple.new
6
-
7
- # Initialize the backend
8
- StateMachines::Machine.new(new_model)
9
-
10
- @model = new_model
11
- end
12
-
13
- def test_should_use_defaults
14
- I18n.backend.store_translations(:en, {
15
- :activerecord => {:errors => {:messages => {:invalid_transition => "cannot #{interpolation_key('event')}"}}}
16
- })
17
-
18
- machine = StateMachines::Machine.new(@model)
19
- machine.state :parked, :idling
20
- machine.event :ignite
21
-
22
- record = @model.new(:state => 'idling')
23
-
24
- machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
25
- assert_equal ['State cannot transition via "ignite"'], record.errors.full_messages
26
- end
27
-
28
- def test_should_allow_customized_error_key
29
- I18n.backend.store_translations(:en, {
30
- :activerecord => {:errors => {:messages => {:bad_transition => "cannot #{interpolation_key('event')}"}}}
31
- })
32
-
33
- machine = StateMachines::Machine.new(@model, :messages => {:invalid_transition => :bad_transition})
34
- machine.state :parked, :idling
35
-
36
- record = @model.new(:state => 'idling')
37
-
38
- machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
39
- assert_equal ['State cannot ignite'], record.errors.full_messages
40
- end
41
-
42
- def test_should_allow_customized_error_string
43
- machine = StateMachines::Machine.new(@model, :messages => {:invalid_transition => "cannot #{interpolation_key('event')}"})
44
- machine.state :parked, :idling
45
-
46
- record = @model.new(:state => 'idling')
47
-
48
- machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
49
- assert_equal ['State cannot ignite'], record.errors.full_messages
50
- end
51
-
52
- def test_should_allow_customized_state_key_scoped_to_class_and_machine
53
- I18n.backend.store_translations(:en, {
54
- :activerecord => {:state_machines => {:'foo' => {:state => {:states => {:parked => 'shutdown'}}}}}
55
- })
56
-
57
- machine = StateMachines::Machine.new(@model)
58
- machine.state :parked
59
-
60
- assert_equal 'shutdown', machine.state(:parked).human_name
61
- end
62
-
63
- def test_should_allow_customized_state_key_scoped_to_class
64
- I18n.backend.store_translations(:en, {
65
- :activerecord => {:state_machines => {:'foo' => {:states => {:parked => 'shutdown'}}}}
66
- })
67
-
68
- machine = StateMachines::Machine.new(@model)
69
- machine.state :parked
70
-
71
- assert_equal 'shutdown', machine.state(:parked).human_name
72
- end
73
-
74
- def test_should_allow_customized_state_key_scoped_to_machine
75
- I18n.backend.store_translations(:en, {
76
- :activerecord => {:state_machines => {:state => {:states => {:parked => 'shutdown'}}}}
77
- })
78
-
79
- machine = StateMachines::Machine.new(@model)
80
- machine.state :parked
81
-
82
- assert_equal 'shutdown', machine.state(:parked).human_name
83
- end
84
-
85
- def test_should_allow_customized_state_key_unscoped
86
- I18n.backend.store_translations(:en, {
87
- :activerecord => {:state_machines => {:states => {:parked => 'shutdown'}}}
88
- })
89
-
90
- machine = StateMachines::Machine.new(@model)
91
- machine.state :parked
92
-
93
- assert_equal 'shutdown', machine.state(:parked).human_name
94
- end
95
-
96
- def test_should_support_nil_state_key
97
- I18n.backend.store_translations(:en, {
98
- :activerecord => {:state_machines => {:states => {:nil => 'empty'}}}
99
- })
100
-
101
- machine = StateMachines::Machine.new(@model)
102
-
103
- assert_equal 'empty', machine.state(nil).human_name
104
- end
105
-
106
- def test_should_allow_customized_event_key_scoped_to_class_and_machine
107
- I18n.backend.store_translations(:en, {
108
- :activerecord => {:state_machines => {:'foo' => {:state => {:events => {:park => 'stop'}}}}}
109
- })
110
-
111
- machine = StateMachines::Machine.new(@model)
112
- machine.event :park
113
-
114
- assert_equal 'stop', machine.event(:park).human_name
115
- end
116
-
117
- def test_should_allow_customized_event_key_scoped_to_class
118
- I18n.backend.store_translations(:en, {
119
- :activerecord => {:state_machines => {:'foo' => {:events => {:park => 'stop'}}}}
120
- })
121
-
122
- machine = StateMachines::Machine.new(@model)
123
- machine.event :park
124
-
125
- assert_equal 'stop', machine.event(:park).human_name
126
- end
127
-
128
- def test_should_allow_customized_event_key_scoped_to_machine
129
- I18n.backend.store_translations(:en, {
130
- :activerecord => {:state_machines => {:state => {:events => {:park => 'stop'}}}}
131
- })
132
-
133
- machine = StateMachines::Machine.new(@model)
134
- machine.event :park
135
-
136
- assert_equal 'stop', machine.event(:park).human_name
137
- end
138
-
139
- def test_should_allow_customized_event_key_unscoped
140
- I18n.backend.store_translations(:en, {
141
- :activerecord => {:state_machines => {:events => {:park => 'stop'}}}
142
- })
143
-
144
- machine = StateMachines::Machine.new(@model)
145
- machine.event :park
146
-
147
- assert_equal 'stop', machine.event(:park).human_name
148
- end
149
-
150
- def test_should_only_add_locale_once_in_load_path
151
- assert_equal 1, I18n.load_path.select { |path| path =~ %r{active_record/locale\.rb$} }.length
152
-
153
- # Create another ActiveRecord model that will trigger the i18n feature
154
- new_model
155
-
156
- assert_equal 1, I18n.load_path.select { |path| path =~ %r{active_record/locale\.rb$} }.length
157
- end
158
-
159
- def test_should_prefer_other_locales_first
160
- @original_load_path = I18n.load_path
161
- I18n.backend = I18n::Backend::Simple.new
162
- I18n.load_path = [File.dirname(__FILE__) + '/files/en.yml']
163
-
164
- machine = StateMachines::Machine.new(@model)
165
- machine.state :parked, :idling
166
- machine.event :ignite
167
-
168
- record = @model.new(:state => 'idling')
169
-
170
- machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
171
- assert_equal ['State cannot transition'], record.errors.full_messages
172
- ensure
173
- I18n.load_path = @original_load_path
174
- end
175
-
176
- private
177
- def interpolation_key(key)
178
- "%{#{key}}"
179
- end
180
- end
@@ -1,22 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithLoopbackTest < BaseTestCase
4
- def setup
5
- @model = new_model do
6
- connection.add_column table_name, :updated_at, :datetime
7
- end
8
-
9
- @machine = StateMachines::Machine.new(@model, :initial => :parked)
10
- @machine.event :park
11
-
12
- @record = @model.create(:updated_at => Time.now - 1)
13
- @transition = StateMachines::Transition.new(@record, @machine, :park, :parked, :parked)
14
-
15
- @timestamp = @record.updated_at
16
- @transition.perform
17
- end
18
-
19
- def test_should_not_update_record
20
- assert_equal @timestamp, @record.updated_at
21
- end
22
- end
@@ -1,29 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithNonColumnStateAttributeDefinedTest < BaseTestCase
4
- def setup
5
- @model = new_model do
6
- attr_accessor :status
7
- end
8
-
9
- @machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
10
- @machine.other_states(:idling)
11
- @record = @model.new
12
- end
13
-
14
- def test_should_return_false_for_predicate_if_does_not_match_current_value
15
- refute @record.status?(:idling)
16
- end
17
-
18
- def test_should_return_true_for_predicate_if_matches_current_value
19
- assert @record.status?(:parked)
20
- end
21
-
22
- def test_should_raise_exception_for_predicate_if_invalid_state_specified
23
- assert_raise(IndexError) { @record.status?(:invalid) }
24
- end
25
-
26
- def test_should_set_initial_state_on_created_object
27
- assert_equal 'parked', @record.status
28
- end
29
- end
@@ -1,26 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithSameColumnDefaultTest < BaseTestCase
4
- def setup
5
- @original_stderr, $stderr = $stderr, StringIO.new
6
-
7
- @model = new_model do
8
- connection.add_column table_name, :status, :string, :default => 'parked'
9
- end
10
- @machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
11
- @record = @model.new
12
- end
13
-
14
- def test_should_use_machine_default
15
- assert_equal 'parked', @record.status
16
- end
17
-
18
- def test_should_not_generate_a_warning
19
- assert_no_match(/have defined a different default/, $stderr.string)
20
- end
21
-
22
- def teardown
23
- $stderr = @original_stderr
24
- super
25
- end
26
- end
@@ -1,30 +0,0 @@
1
- require_relative 'test_helper'
2
- require 'stringio'
3
-
4
- class MachineWithSameIntegerColumnDefaultTest < BaseTestCase
5
- def setup
6
- @original_stderr, $stderr = $stderr, StringIO.new
7
-
8
- @model = new_model do
9
- connection.add_column table_name, :status, :integer, :default => 1
10
- end
11
- @machine = StateMachines::Machine.new(@model, :status, :initial => :parked) do
12
- state :parked, :value => 1
13
- end
14
- @record = @model.new
15
- end
16
-
17
- def test_should_use_machine_default
18
- assert_equal 1, @record.status
19
- end
20
-
21
- def test_should_not_generate_a_warning
22
- assert_no_match(/have defined a different default/, $stderr.string)
23
- end
24
-
25
- def teardown
26
- $stderr = @original_stderr
27
- super
28
- end
29
- end
30
-
@@ -1,38 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithScopesAndJoinsTest < BaseTestCase
4
- def setup
5
- @company = new_model(:company)
6
- MachineWithScopesAndJoinsTest.const_set('Company', @company)
7
-
8
- @vehicle = new_model(:vehicle) do
9
- connection.add_column table_name, :company_id, :integer
10
- belongs_to :company, :class_name => 'MachineWithScopesAndJoinsTest::Company'
11
- end
12
- MachineWithScopesAndJoinsTest.const_set('Vehicle', @vehicle)
13
-
14
- @company_machine = StateMachines::Machine.new(@company, :initial => :active)
15
- @vehicle_machine = StateMachines::Machine.new(@vehicle, :initial => :parked)
16
- @vehicle_machine.state :idling
17
-
18
- @ford = @company.create
19
- @mustang = @vehicle.create(:company => @ford)
20
- end
21
-
22
- def test_should_find_records_in_with_scope
23
- assert_equal [@mustang], @vehicle.with_states(:parked).joins(:company).where("#{@company.table_name}.state = \"active\"")
24
- end
25
-
26
- def test_should_find_records_in_without_scope
27
- assert_equal [@mustang], @vehicle.without_states(:idling).joins(:company).where("#{@company.table_name}.state = \"active\"")
28
- end
29
-
30
- def teardown
31
- MachineWithScopesAndJoinsTest.class_eval do
32
- remove_const('Vehicle')
33
- remove_const('Company')
34
- end
35
-
36
- clear_active_support_dependencies
37
- end
38
- end
@@ -1,27 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithScopesAndOwnerSubclassTest < BaseTestCase
4
- def setup
5
- @model = new_model
6
- @machine = StateMachines::Machine.new(@model, :state)
7
-
8
- @subclass = Class.new(@model)
9
- @subclass_machine = @subclass.state_machine(:state) {}
10
- @subclass_machine.state :parked, :idling, :first_gear
11
- end
12
-
13
- def test_should_only_include_records_with_subclass_states_in_with_scope
14
- parked = @subclass.create :state => 'parked'
15
- idling = @subclass.create :state => 'idling'
16
-
17
- assert_equal [parked, idling], @subclass.with_states(:parked, :idling).all
18
- end
19
-
20
- def test_should_only_include_records_without_subclass_states_in_without_scope
21
- parked = @subclass.create :state => 'parked'
22
- idling = @subclass.create :state => 'idling'
23
- @subclass.create :state => 'first_gear'
24
-
25
- assert_equal [parked, idling], @subclass.without_states(:first_gear).all
26
- end
27
- end
@@ -1,70 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithScopesTest < BaseTestCase
4
- def setup
5
- @model = new_model
6
- @machine = StateMachines::Machine.new(@model)
7
- @machine.state :parked, :first_gear
8
- @machine.state :idling, :value => -> { 'idling' }
9
- end
10
-
11
- def test_should_create_singular_with_scope
12
- assert @model.respond_to?(:with_state)
13
- end
14
-
15
- def test_should_only_include_records_with_state_in_singular_with_scope
16
- parked = @model.create :state => 'parked'
17
- @model.create :state => 'idling'
18
-
19
- assert_equal [parked], @model.with_state(:parked).all
20
- end
21
-
22
- def test_should_create_plural_with_scope
23
- assert @model.respond_to?(:with_states)
24
- end
25
-
26
- def test_should_only_include_records_with_states_in_plural_with_scope
27
- parked = @model.create :state => 'parked'
28
- idling = @model.create :state => 'idling'
29
-
30
- assert_equal [parked, idling], @model.with_states(:parked, :idling).all
31
- end
32
-
33
- def test_should_allow_lookup_by_string_name
34
- parked = @model.create :state => 'parked'
35
- idling = @model.create :state => 'idling'
36
-
37
- assert_equal [parked, idling], @model.with_states('parked', 'idling').all
38
- end
39
-
40
- def test_should_create_singular_without_scope
41
- assert @model.respond_to?(:without_state)
42
- end
43
-
44
- def test_should_only_include_records_without_state_in_singular_without_scope
45
- parked = @model.create :state => 'parked'
46
- @model.create :state => 'idling'
47
-
48
- assert_equal [parked], @model.without_state(:idling).all
49
- end
50
-
51
- def test_should_create_plural_without_scope
52
- assert @model.respond_to?(:without_states)
53
- end
54
-
55
- def test_should_only_include_records_without_states_in_plural_without_scope
56
- parked = @model.create :state => 'parked'
57
- idling = @model.create :state => 'idling'
58
- @model.create :state => 'first_gear'
59
-
60
- assert_equal [parked, idling], @model.without_states(:first_gear).all
61
- end
62
-
63
- def test_should_allow_chaining_scopes
64
- @model.create :state => 'parked'
65
- idling = @model.create :state => 'idling'
66
-
67
- assert_equal [idling], @model.without_state(:parked).with_state(:idling).all
68
- end
69
- end
70
-
@@ -1,30 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithStateDrivenValidationsTest < BaseTestCase
4
- def setup
5
- @model = new_model do
6
- attr_accessor :seatbelt
7
- end
8
-
9
- @machine = StateMachines::Machine.new(@model)
10
- @machine.state :first_gear, :second_gear do
11
- validates :seatbelt, presence: true
12
- end
13
- @machine.other_states :parked
14
- end
15
-
16
- def test_should_be_valid_if_validation_fails_outside_state_scope
17
- record = @model.new(:state => 'parked', :seatbelt => nil)
18
- assert record.valid?
19
- end
20
-
21
- def test_should_be_invalid_if_validation_fails_within_state_scope
22
- record = @model.new(:state => 'first_gear', :seatbelt => nil)
23
- refute record.valid?
24
- end
25
-
26
- def test_should_be_valid_if_validation_succeeds_within_state_scope
27
- record = @model.new(:state => 'second_gear', :seatbelt => true)
28
- assert record.valid?
29
- end
30
- end
@@ -1,13 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithStatesTest < BaseTestCase
4
- def setup
5
- @model = new_model
6
- @machine = StateMachines::Machine.new(@model)
7
- @machine.state :first_gear
8
- end
9
-
10
- def test_should_humanize_name
11
- assert_equal 'first gear', @machine.state(:first_gear).human_name
12
- end
13
- end
@@ -1,167 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithStaticInitialStateTest < BaseTestCase
4
- def setup
5
- @model = new_model(:vehicle) do
6
- attr_accessor :value
7
- end
8
- @machine = StateMachines::Machine.new(@model, :initial => :parked)
9
- end
10
-
11
- def test_should_set_initial_state_on_created_object
12
- record = @model.new
13
- assert_equal 'parked', record.state
14
- end
15
-
16
- def test_should_set_initial_state_with_nil_attributes
17
- record = @model.new(nil)
18
- assert_equal 'parked', record.state
19
- end
20
-
21
- def test_should_still_set_attributes
22
- record = @model.new(:value => 1)
23
- assert_equal 1, record.value
24
- end
25
-
26
- def test_should_still_allow_initialize_blocks
27
- block_args = nil
28
- record = @model.new do |*args|
29
- block_args = args
30
- end
31
-
32
- assert_equal [record], block_args
33
- end
34
-
35
- def test_should_set_attributes_prior_to_initialize_block
36
- state = nil
37
- @model.new do |record|
38
- state = record.state
39
- end
40
-
41
- assert_equal 'parked', state
42
- end
43
-
44
- def test_should_set_attributes_prior_to_after_initialize_hook
45
- state = nil
46
- @model.after_initialize do |record|
47
- state = record.state
48
- end
49
- @model.new
50
- assert_equal 'parked', state
51
- end
52
-
53
- def test_should_set_initial_state_before_setting_attributes
54
- @model.class_eval do
55
- attr_accessor :state_during_setter
56
-
57
- remove_method :value=
58
- define_method(:value=) do |value|
59
- self.state_during_setter = state
60
- end
61
- end
62
- record = @model.new
63
- record.value = 1
64
- assert_equal 'parked', record.state_during_setter
65
- end
66
-
67
- def test_should_not_set_initial_state_after_already_initialized
68
- record = @model.new(:value => 1)
69
- assert_equal 'parked', record.state
70
-
71
- record.state = 'idling'
72
- record.attributes = {}
73
- assert_equal 'idling', record.state
74
- end
75
-
76
- def test_should_persist_initial_state
77
- record = @model.new
78
- record.save
79
- record.reload
80
- assert_equal 'parked', record.state
81
- end
82
-
83
- def test_should_persist_initial_state_on_dup
84
- record = @model.create.dup
85
- record.save
86
- record.reload
87
- assert_equal 'parked', record.state
88
- end
89
-
90
- def test_should_use_stored_values_when_loading_from_database
91
- @machine.state :idling
92
-
93
- record = @model.find(@model.create(:state => 'idling').id)
94
- assert_equal 'idling', record.state
95
- end
96
-
97
- def test_should_use_stored_values_when_loading_from_database_with_nil_state
98
- @machine.state nil
99
-
100
- record = @model.find(@model.create(:state => nil).id)
101
- assert_nil record.state
102
- end
103
-
104
- def test_should_use_stored_values_when_loading_for_many_association
105
- @machine.state :idling
106
-
107
- @model.connection.add_column @model.table_name, :owner_id, :integer
108
- @model.reset_column_information
109
- MachineWithStaticInitialStateTest.const_set('Vehicle', @model)
110
-
111
- owner_model = new_model(:owner) do
112
- has_many :vehicles, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
113
- end
114
- MachineWithStaticInitialStateTest.const_set('Owner', owner_model)
115
-
116
- owner = owner_model.create
117
- @model.create(:state => 'idling', :owner_id => owner.id)
118
- assert_equal 'idling', owner.vehicles[0].state
119
- end
120
-
121
- def test_should_use_stored_values_when_loading_for_one_association
122
- @machine.state :idling
123
-
124
- @model.connection.add_column @model.table_name, :owner_id, :integer
125
- @model.reset_column_information
126
- MachineWithStaticInitialStateTest.const_set('Vehicle', @model)
127
-
128
- owner_model = new_model(:owner) do
129
- has_one :vehicle, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
130
- end
131
- MachineWithStaticInitialStateTest.const_set('Owner', owner_model)
132
-
133
- owner = owner_model.create
134
- @model.create(:state => 'idling', :owner_id => owner.id)
135
- assert_equal 'idling', owner.vehicle.state
136
- end
137
-
138
- def test_should_use_stored_values_when_loading_for_belongs_to_association
139
- @machine.state :idling
140
-
141
- MachineWithStaticInitialStateTest.const_set('Vehicle', @model)
142
-
143
- driver_model = new_model(:driver) do
144
- connection.add_column table_name, :vehicle_id, :integer
145
-
146
- belongs_to :vehicle, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
147
- end
148
-
149
- MachineWithStaticInitialStateTest.const_set('Driver', driver_model)
150
-
151
- record = @model.create(:state => 'idling')
152
- driver = driver_model.create(:vehicle_id => record.id)
153
- assert_equal 'idling', driver.vehicle.state
154
- end
155
-
156
- def teardown
157
- MachineWithStaticInitialStateTest.class_eval do
158
- remove_const('Vehicle') if defined?(MachineWithStaticInitialStateTest::Vehicle)
159
- remove_const('Owner') if defined?(MachineWithStaticInitialStateTest::Owner)
160
- remove_const('Driver') if defined?(MachineWithStaticInitialStateTest::Driver)
161
- end
162
-
163
- clear_active_support_dependencies
164
- super
165
- end
166
- end
167
-
@@ -1,26 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithTransactionsTest < BaseTestCase
4
- def setup
5
- @model = new_model
6
- @machine = StateMachines::Machine.new(@model, :use_transactions => true)
7
- end
8
-
9
- def test_should_rollback_transaction_if_false
10
- @machine.within_transaction(@model.new) do
11
- @model.create
12
- false
13
- end
14
-
15
- assert_equal 0, @model.count
16
- end
17
-
18
- def test_should_not_rollback_transaction_if_true
19
- @machine.within_transaction(@model.new) do
20
- @model.create
21
- true
22
- end
23
-
24
- assert_equal 1, @model.count
25
- end
26
- end
@@ -1,21 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithValidationsAndCustomAttributeTest < BaseTestCase
4
- def setup
5
- @model = new_model
6
- @machine = StateMachines::Machine.new(@model, :status, :attribute => :state)
7
- @machine.state :parked
8
-
9
- @record = @model.new
10
- end
11
-
12
- def test_should_add_validation_errors_to_custom_attribute
13
- @record.state = 'invalid'
14
-
15
- refute @record.valid?
16
- assert_equal ['State is invalid'], @record.errors.full_messages
17
-
18
- @record.state = 'parked'
19
- assert @record.valid?
20
- end
21
- end