state_machines-activerecord 0.10.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.
- checksums.yaml +4 -4
- data/README.md +9 -0
- 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 +40 -24
- metadata +34 -137
- 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,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
|