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
@@ -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
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class MachineWithEventAttributesOnAutosaveTest < BaseTestCase
6
- def setup
7
- @vehicle_model = new_model(:vehicle) do
8
- connection.add_column table_name, :owner_id, :integer
9
- end
10
- MachineWithEventAttributesOnAutosaveTest.const_set('Vehicle', @vehicle_model)
11
-
12
- @owner_model = new_model(:owner)
13
- MachineWithEventAttributesOnAutosaveTest.const_set('Owner', @owner_model)
14
-
15
- machine = StateMachines::Machine.new(@vehicle_model)
16
- machine.event :ignite do
17
- transition :parked => :idling
18
- end
19
-
20
- @owner = @owner_model.create
21
- @vehicle = @vehicle_model.create(:state => 'parked', :owner_id => @owner.id)
22
- end
23
-
24
- def test_should_persist_has_one_autosave
25
- @owner_model.has_one :vehicle, :class_name => 'MachineWithEventAttributesOnAutosaveTest::Vehicle', :autosave => true
26
- @owner.vehicle.state_event = 'ignite'
27
- @owner.save
28
-
29
- @vehicle.reload
30
- assert_equal 'idling', @vehicle.state
31
- end
32
-
33
- def test_should_persist_has_many_autosave
34
- @owner_model.has_many :vehicles, :class_name => 'MachineWithEventAttributesOnAutosaveTest::Vehicle', :autosave => true
35
- @owner.vehicles[0].state_event = 'ignite'
36
- @owner.save
37
-
38
- @vehicle.reload
39
- assert_equal 'idling', @vehicle.state
40
- end
41
-
42
- def teardown
43
- MachineWithEventAttributesOnAutosaveTest.class_eval do
44
- remove_const('Vehicle')
45
- remove_const('Owner')
46
- end
47
- clear_active_support_dependencies
48
- super
49
- end
50
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class MachineWithEventAttributesOnCustomActionTest < BaseTestCase
6
- def setup
7
- @superclass = new_model do
8
- def persist
9
- create_or_update
10
- end
11
- end
12
- @model = Class.new(@superclass)
13
- @machine = StateMachines::Machine.new(@model, :action => :persist)
14
- @machine.event :ignite do
15
- transition :parked => :idling
16
- end
17
-
18
- @record = @model.new
19
- @record.state = 'parked'
20
- @record.state_event = 'ignite'
21
- end
22
-
23
- def test_should_not_transition_on_valid?
24
- @record.valid?
25
- assert_equal 'parked', @record.state
26
- end
27
-
28
- def test_should_not_transition_on_save
29
- @record.save
30
- assert_equal 'parked', @record.state
31
- end
32
-
33
- def test_should_not_transition_on_save!
34
- @record.save!
35
- assert_equal 'parked', @record.state
36
- end
37
-
38
- def test_should_transition_on_custom_action
39
- @record.persist
40
- assert_equal 'idling', @record.state
41
- end
42
- end
43
-
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- class MachineWithEventAttributesOnSaveBangTest < BaseTestCase
6
- def setup
7
- @model = new_model
8
- @machine = StateMachines::Machine.new(@model)
9
- @machine.event :ignite do
10
- transition :parked => :idling
11
- end
12
-
13
- @record = @model.new
14
- @record.state = 'parked'
15
- @record.state_event = 'ignite'
16
- end
17
-
18
- def test_should_fail_if_event_is_invalid
19
- @record.state_event = 'invalid'
20
- assert_raise(ActiveRecord::RecordInvalid) { @record.save! }
21
- end
22
-
23
- def test_should_fail_if_event_has_no_transition
24
- @record.state = 'idling'
25
- assert_raise(ActiveRecord::RecordInvalid) { @record.save! }
26
- end
27
-
28
- def test_should_be_successful_if_event_has_transition
29
- assert_equal true, @record.save!
30
- end
31
-
32
- def test_should_run_before_callbacks
33
- ran_callback = false
34
- @machine.before_transition { ran_callback = true }
35
-
36
- @record.save!
37
- assert ran_callback
38
- end
39
-
40
- def test_should_run_before_callbacks_once
41
- before_count = 0
42
- @machine.before_transition { before_count += 1 }
43
-
44
- @record.save!
45
- assert_equal 1, before_count
46
- end
47
-
48
- def test_should_run_around_callbacks_before_yield
49
- ran_callback = false
50
- @machine.around_transition { |block| ran_callback = true; block.call }
51
-
52
- @record.save!
53
- assert ran_callback
54
- end
55
-
56
- def test_should_run_around_callbacks_before_yield_once
57
- around_before_count = 0
58
- @machine.around_transition { |block| around_before_count += 1; block.call }
59
-
60
- @record.save!
61
- assert_equal 1, around_before_count
62
- end
63
-
64
- def test_should_persist_new_state
65
- @record.save!
66
- assert_equal 'idling', @record.state
67
- end
68
-
69
- def test_should_run_after_callbacks
70
- ran_callback = false
71
- @machine.after_transition { ran_callback = true }
72
-
73
- @record.save!
74
- assert ran_callback
75
- end
76
-
77
- def test_should_run_around_callbacks_after_yield
78
- ran_callback = false
79
- @machine.around_transition { |block| block.call; ran_callback = true }
80
-
81
- @record.save!
82
- assert ran_callback
83
- end
84
- end