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,169 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithStaticInitialStateTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model(:vehicle) do
|
8
|
-
attr_accessor :value
|
9
|
-
end
|
10
|
-
@machine = StateMachines::Machine.new(@model, :initial => :parked)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_should_set_initial_state_on_created_object
|
14
|
-
record = @model.new
|
15
|
-
assert_equal 'parked', record.state
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_should_set_initial_state_with_nil_attributes
|
19
|
-
record = @model.new(nil)
|
20
|
-
assert_equal 'parked', record.state
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_should_still_set_attributes
|
24
|
-
record = @model.new(:value => 1)
|
25
|
-
assert_equal 1, record.value
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_should_still_allow_initialize_blocks
|
29
|
-
block_args = nil
|
30
|
-
record = @model.new do |*args|
|
31
|
-
block_args = args
|
32
|
-
end
|
33
|
-
|
34
|
-
assert_equal [record], block_args
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_should_set_attributes_prior_to_initialize_block
|
38
|
-
state = nil
|
39
|
-
@model.new do |record|
|
40
|
-
state = record.state
|
41
|
-
end
|
42
|
-
|
43
|
-
assert_equal 'parked', state
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_should_set_attributes_prior_to_after_initialize_hook
|
47
|
-
state = nil
|
48
|
-
@model.after_initialize do |record|
|
49
|
-
state = record.state
|
50
|
-
end
|
51
|
-
@model.new
|
52
|
-
assert_equal 'parked', state
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_should_set_initial_state_before_setting_attributes
|
56
|
-
@model.class_eval do
|
57
|
-
attr_accessor :state_during_setter
|
58
|
-
|
59
|
-
remove_method :value=
|
60
|
-
define_method(:value=) do |value|
|
61
|
-
self.state_during_setter = state
|
62
|
-
end
|
63
|
-
end
|
64
|
-
record = @model.new
|
65
|
-
record.value = 1
|
66
|
-
assert_equal 'parked', record.state_during_setter
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_should_not_set_initial_state_after_already_initialized
|
70
|
-
record = @model.new(:value => 1)
|
71
|
-
assert_equal 'parked', record.state
|
72
|
-
|
73
|
-
record.state = 'idling'
|
74
|
-
record.attributes = {}
|
75
|
-
assert_equal 'idling', record.state
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_should_persist_initial_state
|
79
|
-
record = @model.new
|
80
|
-
record.save
|
81
|
-
record.reload
|
82
|
-
assert_equal 'parked', record.state
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_should_persist_initial_state_on_dup
|
86
|
-
record = @model.create.dup
|
87
|
-
record.save
|
88
|
-
record.reload
|
89
|
-
assert_equal 'parked', record.state
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_should_use_stored_values_when_loading_from_database
|
93
|
-
@machine.state :idling
|
94
|
-
|
95
|
-
record = @model.find(@model.create(:state => 'idling').id)
|
96
|
-
assert_equal 'idling', record.state
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_should_use_stored_values_when_loading_from_database_with_nil_state
|
100
|
-
@machine.state nil
|
101
|
-
|
102
|
-
record = @model.find(@model.create(:state => nil).id)
|
103
|
-
assert_nil record.state
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_should_use_stored_values_when_loading_for_many_association
|
107
|
-
@machine.state :idling
|
108
|
-
|
109
|
-
@model.connection.add_column @model.table_name, :owner_id, :integer
|
110
|
-
@model.reset_column_information
|
111
|
-
MachineWithStaticInitialStateTest.const_set('Vehicle', @model)
|
112
|
-
|
113
|
-
owner_model = new_model(:owner) do
|
114
|
-
has_many :vehicles, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
|
115
|
-
end
|
116
|
-
MachineWithStaticInitialStateTest.const_set('Owner', owner_model)
|
117
|
-
|
118
|
-
owner = owner_model.create
|
119
|
-
@model.create(:state => 'idling', :owner_id => owner.id)
|
120
|
-
assert_equal 'idling', owner.vehicles[0].state
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_should_use_stored_values_when_loading_for_one_association
|
124
|
-
@machine.state :idling
|
125
|
-
|
126
|
-
@model.connection.add_column @model.table_name, :owner_id, :integer
|
127
|
-
@model.reset_column_information
|
128
|
-
MachineWithStaticInitialStateTest.const_set('Vehicle', @model)
|
129
|
-
|
130
|
-
owner_model = new_model(:owner) do
|
131
|
-
has_one :vehicle, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
|
132
|
-
end
|
133
|
-
MachineWithStaticInitialStateTest.const_set('Owner', owner_model)
|
134
|
-
|
135
|
-
owner = owner_model.create
|
136
|
-
@model.create(:state => 'idling', :owner_id => owner.id)
|
137
|
-
assert_equal 'idling', owner.vehicle.state
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_should_use_stored_values_when_loading_for_belongs_to_association
|
141
|
-
@machine.state :idling
|
142
|
-
|
143
|
-
MachineWithStaticInitialStateTest.const_set('Vehicle', @model)
|
144
|
-
|
145
|
-
driver_model = new_model(:driver) do
|
146
|
-
connection.add_column table_name, :vehicle_id, :integer
|
147
|
-
|
148
|
-
belongs_to :vehicle, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
|
149
|
-
end
|
150
|
-
|
151
|
-
MachineWithStaticInitialStateTest.const_set('Driver', driver_model)
|
152
|
-
|
153
|
-
record = @model.create(:state => 'idling')
|
154
|
-
driver = driver_model.create(:vehicle_id => record.id)
|
155
|
-
assert_equal 'idling', driver.vehicle.state
|
156
|
-
end
|
157
|
-
|
158
|
-
def teardown
|
159
|
-
MachineWithStaticInitialStateTest.class_eval do
|
160
|
-
remove_const('Vehicle') if defined?(MachineWithStaticInitialStateTest::Vehicle)
|
161
|
-
remove_const('Owner') if defined?(MachineWithStaticInitialStateTest::Owner)
|
162
|
-
remove_const('Driver') if defined?(MachineWithStaticInitialStateTest::Driver)
|
163
|
-
end
|
164
|
-
|
165
|
-
clear_active_support_dependencies
|
166
|
-
super
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithTransactionsTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :use_transactions => true)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_should_rollback_transaction_if_false
|
12
|
-
@machine.within_transaction(@model.new) do
|
13
|
-
@model.create
|
14
|
-
false
|
15
|
-
end
|
16
|
-
|
17
|
-
assert_equal 0, @model.count
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_should_not_rollback_transaction_if_true
|
21
|
-
@machine.within_transaction(@model.new) do
|
22
|
-
@model.create
|
23
|
-
true
|
24
|
-
end
|
25
|
-
|
26
|
-
assert_equal 1, @model.count
|
27
|
-
end
|
28
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithValidationsAndCustomAttributeTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :status, :attribute => :state)
|
9
|
-
@machine.state :parked
|
10
|
-
|
11
|
-
@record = @model.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_should_add_validation_errors_to_custom_attribute
|
15
|
-
@record.state = 'invalid'
|
16
|
-
|
17
|
-
refute @record.valid?
|
18
|
-
assert_equal ['State is invalid'], @record.errors.full_messages
|
19
|
-
|
20
|
-
@record.state = 'parked'
|
21
|
-
assert @record.valid?
|
22
|
-
end
|
23
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithValidationsTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model)
|
9
|
-
@machine.state :parked
|
10
|
-
|
11
|
-
@record = @model.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_should_invalidate_using_errors
|
15
|
-
I18n.backend = I18n::Backend::Simple.new
|
16
|
-
@record.state = 'parked'
|
17
|
-
|
18
|
-
@machine.invalidate(@record, :state, :invalid_transition, [[:event, 'park']])
|
19
|
-
assert_equal ['State cannot transition via "park"'], @record.errors.full_messages
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_should_auto_prefix_custom_attributes_on_invalidation
|
23
|
-
@machine.invalidate(@record, :event, :invalid)
|
24
|
-
|
25
|
-
assert_equal ['State event is invalid'], @record.errors.full_messages
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_should_clear_errors_on_reset
|
29
|
-
@record.state = 'parked'
|
30
|
-
@record.errors.add(:state, 'is invalid')
|
31
|
-
|
32
|
-
@machine.reset(@record)
|
33
|
-
assert_equal [], @record.errors.full_messages
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_should_be_valid_if_state_is_known
|
37
|
-
@record.state = 'parked'
|
38
|
-
|
39
|
-
assert @record.valid?
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_should_not_be_valid_if_state_is_unknown
|
43
|
-
@record.state = 'invalid'
|
44
|
-
|
45
|
-
refute @record.valid?
|
46
|
-
assert_equal ['State is invalid'], @record.errors.full_messages
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithoutDatabaseTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model(false) do
|
8
|
-
# Simulate the database not being available entirely
|
9
|
-
def self.connection
|
10
|
-
raise ActiveRecord::ConnectionNotEstablished
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.connected?
|
14
|
-
false
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_allow_machine_creation
|
20
|
-
assert_nothing_raised { StateMachines::Machine.new(@model) }
|
21
|
-
end
|
22
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class MachineWithoutTransactionsTest < BaseTestCase
|
6
|
-
def setup
|
7
|
-
@model = new_model
|
8
|
-
@machine = StateMachines::Machine.new(@model, :use_transactions => false)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_should_not_rollback_transaction_if_false
|
12
|
-
@machine.within_transaction(@model.new) do
|
13
|
-
@model.create
|
14
|
-
false
|
15
|
-
end
|
16
|
-
|
17
|
-
assert_equal 1, @model.count
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_should_not_rollback_transaction_if_true
|
21
|
-
@machine.within_transaction(@model.new) do
|
22
|
-
@model.create
|
23
|
-
true
|
24
|
-
end
|
25
|
-
|
26
|
-
assert_equal 1, @model.count
|
27
|
-
end
|
28
|
-
end
|
data/test/model_test.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
require_relative 'files/models/post'
|
5
|
-
|
6
|
-
class ModelTest < ActiveSupport::TestCase
|
7
|
-
def test_should_have_draft_state_in_defaut_machine
|
8
|
-
assert_equal 'draft', Post.new.state
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_should_have_the_correct_integration
|
12
|
-
assert_equal StateMachines::Integrations::ActiveRecord, StateMachines::Integrations.match(Post)
|
13
|
-
end
|
14
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'debug' if RUBY_ENGINE == 'ruby'
|
4
|
-
require 'minitest/reporters'
|
5
|
-
Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
|
6
|
-
require 'state_machines-activerecord'
|
7
|
-
require 'minitest/autorun'
|
8
|
-
require 'securerandom'
|
9
|
-
|
10
|
-
# Establish database connection
|
11
|
-
ActiveRecord::Base.establish_connection('adapter' => 'sqlite3', 'database' => ':memory:')
|
12
|
-
ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/../log/active_record.log")
|
13
|
-
ActiveSupport.test_order = :random
|
14
|
-
|
15
|
-
class BaseTestCase < ActiveSupport::TestCase
|
16
|
-
protected
|
17
|
-
# Creates a new ActiveRecord model (and the associated table)
|
18
|
-
def new_model(create_table = :foo, &block)
|
19
|
-
name = create_table || :foo
|
20
|
-
table_name = "#{name}_#{SecureRandom.hex(6)}"
|
21
|
-
|
22
|
-
model = Class.new(ActiveRecord::Base) do
|
23
|
-
self.table_name = table_name.to_s
|
24
|
-
connection.create_table(table_name, :force => true) { |t| t.string(:state) } if create_table
|
25
|
-
|
26
|
-
define_method(:abort_from_callback) do
|
27
|
-
throw :abort
|
28
|
-
end
|
29
|
-
|
30
|
-
(
|
31
|
-
class << self;
|
32
|
-
self;
|
33
|
-
end).class_eval do
|
34
|
-
define_method(:name) { "#{name.to_s.capitalize}" }
|
35
|
-
end
|
36
|
-
end
|
37
|
-
model.class_eval(&block) if block_given?
|
38
|
-
model.reset_column_information if create_table
|
39
|
-
model
|
40
|
-
end
|
41
|
-
|
42
|
-
def clear_active_support_dependencies
|
43
|
-
return unless defined?(ActiveSupport::Dependencies)
|
44
|
-
|
45
|
-
if ActiveSupport::Dependencies.respond_to?(:autoloader=)
|
46
|
-
ActiveSupport::Dependencies.autoloader ||= stubbed_autoloader
|
47
|
-
end
|
48
|
-
|
49
|
-
ActiveSupport::Dependencies.clear
|
50
|
-
end
|
51
|
-
|
52
|
-
def stubbed_autoloader
|
53
|
-
Object.new.tap do |obj|
|
54
|
-
obj.define_singleton_method(:reload) {}
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|