state_machines-activerecord 0.10.0 → 0.200.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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +173 -2
  3. data/lib/state_machines/integrations/active_record/locale.rb +10 -9
  4. data/lib/state_machines/integrations/active_record/type/integer.rb +122 -0
  5. data/lib/state_machines/integrations/active_record/version.rb +1 -1
  6. data/lib/state_machines/integrations/active_record.rb +514 -38
  7. metadata +36 -138
  8. data/test/files/en.yml +0 -5
  9. data/test/files/models/post.rb +0 -13
  10. data/test/integration_test.rb +0 -27
  11. data/test/machine_by_default_test.rb +0 -18
  12. data/test/machine_errors_test.rb +0 -21
  13. data/test/machine_multiple_test.rb +0 -19
  14. data/test/machine_nested_action_test.rb +0 -40
  15. data/test/machine_unmigrated_test.rb +0 -16
  16. data/test/machine_with_aliased_attribute_test.rb +0 -25
  17. data/test/machine_with_callbacks_test.rb +0 -174
  18. data/test/machine_with_column_state_attribute_test.rb +0 -46
  19. data/test/machine_with_complex_pluralization_scopes_test.rb +0 -18
  20. data/test/machine_with_conflicting_predicate_test.rb +0 -20
  21. data/test/machine_with_conflicting_state_name_test.rb +0 -31
  22. data/test/machine_with_custom_attribute_test.rb +0 -23
  23. data/test/machine_with_default_scope_test.rb +0 -20
  24. data/test/machine_with_different_column_default_test.rb +0 -29
  25. data/test/machine_with_different_integer_column_default_test.rb +0 -31
  26. data/test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb +0 -26
  27. data/test/machine_with_dirty_attribute_and_state_events_test.rb +0 -22
  28. data/test/machine_with_dirty_attributes_and_custom_attribute_test.rb +0 -34
  29. data/test/machine_with_dirty_attributes_during_loopback_test.rb +0 -24
  30. data/test/machine_with_dirty_attributes_test.rb +0 -37
  31. data/test/machine_with_dynamic_initial_state_test.rb +0 -101
  32. data/test/machine_with_event_attributes_on_autosave_test.rb +0 -50
  33. data/test/machine_with_event_attributes_on_custom_action_test.rb +0 -43
  34. data/test/machine_with_event_attributes_on_save_bang_test.rb +0 -84
  35. data/test/machine_with_event_attributes_on_save_test.rb +0 -246
  36. data/test/machine_with_event_attributes_on_validation_test.rb +0 -145
  37. data/test/machine_with_events_test.rb +0 -15
  38. data/test/machine_with_failed_action_test.rb +0 -42
  39. data/test/machine_with_failed_after_callbacks_test.rb +0 -37
  40. data/test/machine_with_failed_before_callbacks_test.rb +0 -38
  41. data/test/machine_with_initialized_state_test.rb +0 -43
  42. data/test/machine_with_internationalization_test.rb +0 -182
  43. data/test/machine_with_loopback_test.rb +0 -24
  44. data/test/machine_with_non_column_state_attribute_defined_test.rb +0 -31
  45. data/test/machine_with_same_column_default_test.rb +0 -28
  46. data/test/machine_with_same_integer_column_default_test.rb +0 -32
  47. data/test/machine_with_scopes_and_joins_test.rb +0 -40
  48. data/test/machine_with_scopes_and_owner_subclass_test.rb +0 -29
  49. data/test/machine_with_scopes_test.rb +0 -72
  50. data/test/machine_with_state_driven_validations_test.rb +0 -32
  51. data/test/machine_with_states_test.rb +0 -15
  52. data/test/machine_with_static_initial_state_test.rb +0 -169
  53. data/test/machine_with_transactions_test.rb +0 -28
  54. data/test/machine_with_validations_and_custom_attribute_test.rb +0 -23
  55. data/test/machine_with_validations_test.rb +0 -49
  56. data/test/machine_without_database_test.rb +0 -22
  57. data/test/machine_without_transactions_test.rb +0 -28
  58. data/test/model_test.rb +0 -14
  59. data/test/test_helper.rb +0 -57
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machines-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.200.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -11,103 +11,103 @@ cert_chain: []
11
11
  date: 1980-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: state_machines-activemodel
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.10.0
19
+ version: '7.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.10.0
26
+ version: '7.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activerecord
28
+ name: state_machines-activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '7.1'
33
+ version: 0.200.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '7.1'
40
+ version: 0.200.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: appraisal
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '13.0'
47
+ version: '1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '13.0'
54
+ version: '1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: sqlite3
56
+ name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: '1.3'
61
+ version: 5.27.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: '1.3'
68
+ version: 5.27.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: appraisal
70
+ name: minitest-reporters
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '1'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '1'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: minitest
84
+ name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 5.4.0
89
+ version: '13.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 5.4.0
96
+ version: '13.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: minitest-reporters
98
+ name: sqlite3
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: '2.1'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: '2.1'
111
111
  description: Adds support for creating state machines for attributes on ActiveRecord
112
112
  email:
113
113
  - terminale@gmail.com
@@ -121,63 +121,13 @@ files:
121
121
  - lib/state_machines-activerecord.rb
122
122
  - lib/state_machines/integrations/active_record.rb
123
123
  - lib/state_machines/integrations/active_record/locale.rb
124
+ - lib/state_machines/integrations/active_record/type/integer.rb
124
125
  - lib/state_machines/integrations/active_record/version.rb
125
- - test/files/en.yml
126
- - test/files/models/post.rb
127
- - test/integration_test.rb
128
- - test/machine_by_default_test.rb
129
- - test/machine_errors_test.rb
130
- - test/machine_multiple_test.rb
131
- - test/machine_nested_action_test.rb
132
- - test/machine_unmigrated_test.rb
133
- - test/machine_with_aliased_attribute_test.rb
134
- - test/machine_with_callbacks_test.rb
135
- - test/machine_with_column_state_attribute_test.rb
136
- - test/machine_with_complex_pluralization_scopes_test.rb
137
- - test/machine_with_conflicting_predicate_test.rb
138
- - test/machine_with_conflicting_state_name_test.rb
139
- - test/machine_with_custom_attribute_test.rb
140
- - test/machine_with_default_scope_test.rb
141
- - test/machine_with_different_column_default_test.rb
142
- - test/machine_with_different_integer_column_default_test.rb
143
- - test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb
144
- - test/machine_with_dirty_attribute_and_state_events_test.rb
145
- - test/machine_with_dirty_attributes_and_custom_attribute_test.rb
146
- - test/machine_with_dirty_attributes_during_loopback_test.rb
147
- - test/machine_with_dirty_attributes_test.rb
148
- - test/machine_with_dynamic_initial_state_test.rb
149
- - test/machine_with_event_attributes_on_autosave_test.rb
150
- - test/machine_with_event_attributes_on_custom_action_test.rb
151
- - test/machine_with_event_attributes_on_save_bang_test.rb
152
- - test/machine_with_event_attributes_on_save_test.rb
153
- - test/machine_with_event_attributes_on_validation_test.rb
154
- - test/machine_with_events_test.rb
155
- - test/machine_with_failed_action_test.rb
156
- - test/machine_with_failed_after_callbacks_test.rb
157
- - test/machine_with_failed_before_callbacks_test.rb
158
- - test/machine_with_initialized_state_test.rb
159
- - test/machine_with_internationalization_test.rb
160
- - test/machine_with_loopback_test.rb
161
- - test/machine_with_non_column_state_attribute_defined_test.rb
162
- - test/machine_with_same_column_default_test.rb
163
- - test/machine_with_same_integer_column_default_test.rb
164
- - test/machine_with_scopes_and_joins_test.rb
165
- - test/machine_with_scopes_and_owner_subclass_test.rb
166
- - test/machine_with_scopes_test.rb
167
- - test/machine_with_state_driven_validations_test.rb
168
- - test/machine_with_states_test.rb
169
- - test/machine_with_static_initial_state_test.rb
170
- - test/machine_with_transactions_test.rb
171
- - test/machine_with_validations_and_custom_attribute_test.rb
172
- - test/machine_with_validations_test.rb
173
- - test/machine_without_database_test.rb
174
- - test/machine_without_transactions_test.rb
175
- - test/model_test.rb
176
- - test/test_helper.rb
177
126
  homepage: https://github.com/state-machines/state_machines-activerecord/
178
127
  licenses:
179
128
  - MIT
180
- metadata: {}
129
+ metadata:
130
+ rubygems_mfa_required: 'true'
181
131
  rdoc_options: []
182
132
  require_paths:
183
133
  - lib
@@ -185,66 +135,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
185
135
  requirements:
186
136
  - - ">="
187
137
  - !ruby/object:Gem::Version
188
- version: '3.1'
138
+ version: '3.2'
189
139
  required_rubygems_version: !ruby/object:Gem::Requirement
190
140
  requirements:
191
141
  - - ">="
192
142
  - !ruby/object:Gem::Version
193
143
  version: '0'
194
144
  requirements: []
195
- rubygems_version: 3.6.7
145
+ rubygems_version: 4.0.10
196
146
  specification_version: 4
197
147
  summary: State machines Active Record Integration
198
- test_files:
199
- - test/files/en.yml
200
- - test/files/models/post.rb
201
- - test/integration_test.rb
202
- - test/machine_by_default_test.rb
203
- - test/machine_errors_test.rb
204
- - test/machine_multiple_test.rb
205
- - test/machine_nested_action_test.rb
206
- - test/machine_unmigrated_test.rb
207
- - test/machine_with_aliased_attribute_test.rb
208
- - test/machine_with_callbacks_test.rb
209
- - test/machine_with_column_state_attribute_test.rb
210
- - test/machine_with_complex_pluralization_scopes_test.rb
211
- - test/machine_with_conflicting_predicate_test.rb
212
- - test/machine_with_conflicting_state_name_test.rb
213
- - test/machine_with_custom_attribute_test.rb
214
- - test/machine_with_default_scope_test.rb
215
- - test/machine_with_different_column_default_test.rb
216
- - test/machine_with_different_integer_column_default_test.rb
217
- - test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb
218
- - test/machine_with_dirty_attribute_and_state_events_test.rb
219
- - test/machine_with_dirty_attributes_and_custom_attribute_test.rb
220
- - test/machine_with_dirty_attributes_during_loopback_test.rb
221
- - test/machine_with_dirty_attributes_test.rb
222
- - test/machine_with_dynamic_initial_state_test.rb
223
- - test/machine_with_event_attributes_on_autosave_test.rb
224
- - test/machine_with_event_attributes_on_custom_action_test.rb
225
- - test/machine_with_event_attributes_on_save_bang_test.rb
226
- - test/machine_with_event_attributes_on_save_test.rb
227
- - test/machine_with_event_attributes_on_validation_test.rb
228
- - test/machine_with_events_test.rb
229
- - test/machine_with_failed_action_test.rb
230
- - test/machine_with_failed_after_callbacks_test.rb
231
- - test/machine_with_failed_before_callbacks_test.rb
232
- - test/machine_with_initialized_state_test.rb
233
- - test/machine_with_internationalization_test.rb
234
- - test/machine_with_loopback_test.rb
235
- - test/machine_with_non_column_state_attribute_defined_test.rb
236
- - test/machine_with_same_column_default_test.rb
237
- - test/machine_with_same_integer_column_default_test.rb
238
- - test/machine_with_scopes_and_joins_test.rb
239
- - test/machine_with_scopes_and_owner_subclass_test.rb
240
- - test/machine_with_scopes_test.rb
241
- - test/machine_with_state_driven_validations_test.rb
242
- - test/machine_with_states_test.rb
243
- - test/machine_with_static_initial_state_test.rb
244
- - test/machine_with_transactions_test.rb
245
- - test/machine_with_validations_and_custom_attribute_test.rb
246
- - test/machine_with_validations_test.rb
247
- - test/machine_without_database_test.rb
248
- - test/machine_without_transactions_test.rb
249
- - test/model_test.rb
250
- - test/test_helper.rb
148
+ test_files: []
data/test/files/en.yml DELETED
@@ -1,5 +0,0 @@
1
- en:
2
- activerecord:
3
- errors:
4
- messages:
5
- invalid_transition: "cannot transition"
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ActiveRecord::Base.connection.create_table(:posts, :force => true) do |t|
4
- t.string :title
5
- t.string :content
6
- t.string :state
7
- end
8
-
9
- class Post < ActiveRecord::Base
10
- state_machine initial: :draft do
11
-
12
- end
13
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class IntegrationTest < BaseTestCase
6
- def test_should_have_an_integration_name
7
- assert_equal :active_record, StateMachines::Integrations::ActiveRecord.integration_name
8
- end
9
-
10
- def test_should_be_before_activemodel
11
- integrations = StateMachines::Integrations.list.to_a
12
- assert StateMachines::Integrations::ActiveRecord, integrations.first
13
- assert StateMachines::Integrations::ActiveModel, integrations.last
14
- end
15
-
16
- def test_should_match_if_class_inherits_from_active_record
17
- assert StateMachines::Integrations::ActiveRecord.matches?(new_model)
18
- end
19
-
20
- def test_should_not_match_if_class_does_not_inherit_from_active_record
21
- refute StateMachines::Integrations::ActiveRecord.matches?(Class.new)
22
- end
23
-
24
- def test_should_have_defaults
25
- assert_equal({action: :save, use_transactions: true}, StateMachines::Integrations::ActiveRecord.defaults)
26
- end
27
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class MachineByDefaultTest < BaseTestCase
6
- def setup
7
- @model = new_model
8
- @machine = StateMachines::Machine.new(@model)
9
- end
10
-
11
- def test_should_use_save_as_action
12
- assert_equal :save, @machine.action
13
- end
14
-
15
- def test_should_use_transactions
16
- assert_equal true, @machine.use_transactions
17
- end
18
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class MachineErrorsTest < BaseTestCase
6
- def setup
7
- @model = new_model
8
- @machine = StateMachines::Machine.new(@model)
9
- @record = @model.new
10
- end
11
-
12
- def test_should_be_able_to_describe_current_errors
13
- @record.errors.add(:id, 'cannot be blank')
14
- @record.errors.add(:state, 'is invalid')
15
- assert_equal ['Id cannot be blank', 'State is invalid'], @machine.errors_for(@record).split(', ').sort
16
- end
17
-
18
- def test_should_describe_as_halted_with_no_errors
19
- assert_equal 'Transition halted', @machine.errors_for(@record)
20
- end
21
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class MachineMultipleTest < BaseTestCase
6
- def setup
7
- @model = new_model do
8
- connection.add_column table_name, :status, :string
9
- end
10
- @state_machine = StateMachines::Machine.new(@model, :initial => :parked)
11
- @status_machine = StateMachines::Machine.new(@model, :status, :initial => :idling)
12
- end
13
-
14
- def test_should_should_initialize_each_state
15
- record = @model.new
16
- assert_equal 'parked', record.state
17
- assert_equal 'idling', record.status
18
- end
19
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class MachineNestedActionTest < BaseTestCase
6
- def setup
7
- @callbacks = []
8
-
9
- @model = new_model
10
- @machine = StateMachines::Machine.new(@model)
11
- @machine.event :ignite do
12
- transition :parked => :idling
13
- end
14
-
15
- @record = @model.new(:state => 'parked')
16
- end
17
-
18
- def test_should_allow_transition_prior_to_creation_if_skipping_action
19
- record = @record
20
- @model.before_create { record.ignite(false) }
21
- result = @record.save
22
-
23
- assert_equal true, result
24
- assert_equal "idling", @record.state
25
- @record.reload
26
- assert_equal "idling", @record.state
27
- end
28
-
29
- def test_should_allow_transition_after_creation
30
- record = @record
31
- @model.after_create { record.ignite }
32
- result = @record.save
33
-
34
- assert_equal true, result
35
- assert_equal "idling", @record.state
36
- @record.reload
37
- assert_equal "idling", @record.state
38
- end
39
- end
40
-
@@ -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