state_machines-activerecord 0.9.0 → 0.31.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 +10 -2
  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 +59 -102
  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,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
@@ -1,47 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithValidationsTest < BaseTestCase
4
- def setup
5
- @model = new_model
6
- @machine = StateMachines::Machine.new(@model)
7
- @machine.state :parked
8
-
9
- @record = @model.new
10
- end
11
-
12
- def test_should_invalidate_using_errors
13
- I18n.backend = I18n::Backend::Simple.new
14
- @record.state = 'parked'
15
-
16
- @machine.invalidate(@record, :state, :invalid_transition, [[:event, 'park']])
17
- assert_equal ['State cannot transition via "park"'], @record.errors.full_messages
18
- end
19
-
20
- def test_should_auto_prefix_custom_attributes_on_invalidation
21
- @machine.invalidate(@record, :event, :invalid)
22
-
23
- assert_equal ['State event is invalid'], @record.errors.full_messages
24
- end
25
-
26
- def test_should_clear_errors_on_reset
27
- @record.state = 'parked'
28
- @record.errors.add(:state, 'is invalid')
29
-
30
- @machine.reset(@record)
31
- assert_equal [], @record.errors.full_messages
32
- end
33
-
34
- def test_should_be_valid_if_state_is_known
35
- @record.state = 'parked'
36
-
37
- assert @record.valid?
38
- end
39
-
40
- def test_should_not_be_valid_if_state_is_unknown
41
- @record.state = 'invalid'
42
-
43
- refute @record.valid?
44
- assert_equal ['State is invalid'], @record.errors.full_messages
45
- end
46
- end
47
-
@@ -1,20 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithoutDatabaseTest < BaseTestCase
4
- def setup
5
- @model = new_model(false) do
6
- # Simulate the database not being available entirely
7
- def self.connection
8
- raise ActiveRecord::ConnectionNotEstablished
9
- end
10
-
11
- def self.connected?
12
- false
13
- end
14
- end
15
- end
16
-
17
- def test_should_allow_machine_creation
18
- assert_nothing_raised { StateMachines::Machine.new(@model) }
19
- end
20
- end
@@ -1,26 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class MachineWithoutTransactionsTest < BaseTestCase
4
- def setup
5
- @model = new_model
6
- @machine = StateMachines::Machine.new(@model, :use_transactions => false)
7
- end
8
-
9
- def test_should_not_rollback_transaction_if_false
10
- @machine.within_transaction(@model.new) do
11
- @model.create
12
- false
13
- end
14
-
15
- assert_equal 1, @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
data/test/model_test.rb DELETED
@@ -1,12 +0,0 @@
1
- require_relative 'test_helper'
2
- require_relative 'files/models/post'
3
-
4
- class ModelTest < ActiveSupport::TestCase
5
- def test_should_have_draft_state_in_defaut_machine
6
- assert_equal 'draft', Post.new.state
7
- end
8
-
9
- def test_should_have_the_correct_integration
10
- assert_equal StateMachines::Integrations::ActiveRecord, StateMachines::Integrations.match(Post)
11
- end
12
- end
data/test/test_helper.rb DELETED
@@ -1,58 +0,0 @@
1
- begin
2
- require 'pry-byebug'
3
- rescue LoadError
4
- end
5
- require 'minitest/reporters'
6
- Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
7
- require 'state_machines-activerecord'
8
- require 'minitest/autorun'
9
- require 'securerandom'
10
-
11
- # Establish database connection
12
- ActiveRecord::Base.establish_connection('adapter' => 'sqlite3', 'database' => ':memory:')
13
- ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/../log/active_record.log")
14
- ActiveSupport.test_order = :random
15
-
16
- class BaseTestCase < ActiveSupport::TestCase
17
- protected
18
- # Creates a new ActiveRecord model (and the associated table)
19
- def new_model(create_table = :foo, &block)
20
- name = create_table || :foo
21
- table_name = "#{name}_#{SecureRandom.hex(6)}"
22
-
23
- model = Class.new(ActiveRecord::Base) do
24
- self.table_name = table_name.to_s
25
- connection.create_table(table_name, :force => true) { |t| t.string(:state) } if create_table
26
-
27
- define_method(:abort_from_callback) do
28
- throw :abort
29
- end
30
-
31
- (
32
- class << self;
33
- self;
34
- end).class_eval do
35
- define_method(:name) { "#{name.to_s.capitalize}" }
36
- end
37
- end
38
- model.class_eval(&block) if block_given?
39
- model.reset_column_information if create_table
40
- model
41
- end
42
-
43
- def clear_active_support_dependencies
44
- return unless defined?(ActiveSupport::Dependencies)
45
-
46
- if ActiveSupport::Dependencies.respond_to?(:autoloader=)
47
- ActiveSupport::Dependencies.autoloader ||= stubbed_autoloader
48
- end
49
-
50
- ActiveSupport::Dependencies.clear
51
- end
52
-
53
- def stubbed_autoloader
54
- Object.new.tap do |obj|
55
- obj.define_singleton_method(:reload) {}
56
- end
57
- end
58
- end