state_machines-mongoid 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +20 -0
  4. data/Appraisals +8 -0
  5. data/Gemfile +0 -2
  6. data/LICENSE.txt +1 -0
  7. data/README.md +43 -4
  8. data/Rakefile +8 -1
  9. data/gemfiles/active_model_4.1.gemfile +7 -0
  10. data/gemfiles/active_model_4.2.gemfile +7 -0
  11. data/lib/state_machines-mongoid.rb +1 -0
  12. data/lib/state_machines/integrations/mongoid.rb +401 -0
  13. data/lib/state_machines/integrations/mongoid/locale.rb +11 -0
  14. data/lib/state_machines/integrations/mongoid/version.rb +7 -0
  15. data/lib/state_machines/state_machines-mongoid.rb +1 -0
  16. data/state_machines-mongoid.gemspec +18 -12
  17. data/test/files/en.yml +5 -0
  18. data/test/integration_test.rb +23 -0
  19. data/test/machine_by_default_test.rb +16 -0
  20. data/test/machine_errors_test.rb +20 -0
  21. data/test/machine_multiple_test.rb +17 -0
  22. data/test/machine_nested_action_test.rb +39 -0
  23. data/test/machine_with_aliased_attribute_test.rb +22 -0
  24. data/test/machine_with_aliased_field_test.rb +22 -0
  25. data/test/machine_with_callbacks_test.rb +167 -0
  26. data/test/machine_with_column_state_attribute_test.rb +44 -0
  27. data/test/machine_with_complex_pluralization_scopes_test.rb +16 -0
  28. data/test/machine_with_conflicting_predicate_test.rb +27 -0
  29. data/test/machine_with_conflicting_state_name_test.rb +29 -0
  30. data/test/machine_with_custom_attribute_test.rb +21 -0
  31. data/test/machine_with_default_scope_test.rb +14 -0
  32. data/test/machine_with_different_column_default_test.rb +26 -0
  33. data/test/machine_with_different_integer_column_default_test.rb +27 -0
  34. data/test/machine_with_different_same_default_test.rb +26 -0
  35. data/test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb +24 -0
  36. data/test/machine_with_dirty_attribute_and_state_events_test.rb +20 -0
  37. data/test/machine_with_dirty_attributes_and_custom_attribute_test.rb +32 -0
  38. data/test/machine_with_dirty_attributes_during_loopback_test.rb +22 -0
  39. data/test/machine_with_dirty_attributes_test.rb +35 -0
  40. data/test/machine_with_dynamic_initial_state_test.rb +99 -0
  41. data/test/machine_with_event_attributes_on_autosave_test.rb +50 -0
  42. data/test/machine_with_event_attributes_on_custom_action_test.rb +40 -0
  43. data/test/machine_with_event_attributes_on_save_bang_test.rb +82 -0
  44. data/test/machine_with_event_attributes_on_save_test.rb +146 -0
  45. data/test/machine_with_event_attributes_on_validation_test.rb +115 -0
  46. data/test/machine_with_events_test.rb +13 -0
  47. data/test/machine_with_failed_action_test.rb +39 -0
  48. data/test/machine_with_failed_after_callbacks_test.rb +35 -0
  49. data/test/machine_with_failed_before_callbacks_test.rb +36 -0
  50. data/test/machine_with_field_test.rb +16 -0
  51. data/test/machine_with_initialized_state_test.rb +35 -0
  52. data/test/machine_with_internationalization_test.rb +175 -0
  53. data/test/machine_with_loopback_test.rb +26 -0
  54. data/test/machine_with_non_column_state_attribute_defined_test.rb +35 -0
  55. data/test/machine_with_non_column_state_attribute_undefined_test.rb +33 -0
  56. data/test/machine_with_scopes_and_owner_subclass_test.rb +42 -0
  57. data/test/machine_with_scopes_test.rb +69 -0
  58. data/test/machine_with_state_driven_validations_test.rb +33 -0
  59. data/test/machine_with_state_test.rb +13 -0
  60. data/test/machine_with_static_initial_state_test.rb +162 -0
  61. data/test/machine_with_validations_and_custom_attribute_test.rb +21 -0
  62. data/test/machine_with_validations_test.rb +46 -0
  63. data/test/machine_without_field_test.rb +14 -0
  64. data/test/test_helper.rb +90 -0
  65. metadata +170 -9
  66. data/lib/state_machines/mongoid.rb +0 -7
  67. data/lib/state_machines/mongoid/version.rb +0 -5
@@ -0,0 +1,16 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithComplexPluralizationScopesTest < BaseTestCase
4
+ def setup
5
+ @model = new_model
6
+ @machine = StateMachines::Machine.new(@model, :status)
7
+ end
8
+
9
+ def test_should_create_singular_with_scope
10
+ assert @model.respond_to?(:with_status)
11
+ end
12
+
13
+ def test_should_create_plural_with_scope
14
+ assert @model.respond_to?(:with_statuses)
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDifferentIntegerColumnDefaultTest < BaseTestCase
4
+ def setup
5
+ @original_stderr, $stderr = $stderr, StringIO.new
6
+
7
+ @model = new_model do
8
+ field :status, :type => Integer, :default => 0
9
+ end
10
+ @machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
11
+ @machine.state :parked, :value => 1
12
+ @record = @model.new
13
+ end
14
+
15
+ def test_should_use_machine_default
16
+ assert_equal 1, @record.status
17
+ end
18
+
19
+ def test_should_generate_a_warning
20
+ assert_match(/Both MongoidTest::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)
21
+ end
22
+
23
+ def teardown
24
+ $stderr = @original_stderr
25
+ super
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithConflictingStateNameTest < BaseTestCase
4
+ def setup
5
+ require 'stringio'
6
+ @original_stderr, $stderr = $stderr, StringIO.new
7
+
8
+ @model = new_model
9
+ end
10
+
11
+ def test_should_output_warning_with_same_machine_name
12
+ @machine = StateMachines::Machine.new(@model)
13
+ @machine.state :state
14
+
15
+ assert_match(/^Instance method "state\?" is already defined in .*, use generic helper instead.*\n$/, $stderr.string)
16
+ end
17
+
18
+ def test_should_output_warning_with_same_machine_attribute
19
+ @machine = StateMachines::Machine.new(@model, :public_state, :attribute => :state)
20
+ @machine.state :state
21
+
22
+ assert_match(/^Instance method "state\?" is already defined in .*, use generic helper instead.*\n$/, $stderr.string)
23
+ end
24
+
25
+ def teardown
26
+ $stderr = @original_stderr
27
+ super
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithCustomAttributeTest < BaseTestCase
4
+ def setup
5
+ require 'stringio'
6
+ @original_stderr, $stderr = $stderr, StringIO.new
7
+
8
+ @model = new_model
9
+ @machine = StateMachines::Machine.new(@model, :public_state, :attribute => :state)
10
+ @record = @model.new
11
+ end
12
+
13
+ def test_should_not_delegate_attribute_predicate_with_different_attribute
14
+ assert_raises(ArgumentError) { @record.public_state? }
15
+ end
16
+
17
+ def teardown
18
+ $stderr = @original_stderr
19
+ super
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDefaultScopeTest < BaseTestCase
4
+ def setup
5
+ @model = new_model
6
+ @machine = StateMachines::Machine.new(@model, :initial => :parked)
7
+ @machine.state :idling
8
+ end
9
+
10
+ def test_should_set_initial_state_on_created_object
11
+ object = @model.new
12
+ assert_equal 'parked', object.state
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDifferentColumnDefaultTest < BaseTestCase
4
+ def setup
5
+ @original_stderr, $stderr = $stderr, StringIO.new
6
+
7
+ @model = new_model do
8
+ field :status, :type => String, :default => 'idling'
9
+ end
10
+ @machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
11
+ @record = @model.new
12
+ end
13
+
14
+ def test_should_use_machine_default
15
+ assert_equal 'parked', @record.status
16
+ end
17
+
18
+ def test_should_generate_a_warning
19
+ assert_match(/Both MongoidTest::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)
20
+ end
21
+
22
+ def teardown
23
+ $stderr = @original_stderr
24
+ super
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDifferentIntegerColumnDefaultTest < BaseTestCase
4
+ def setup
5
+ @original_stderr, $stderr = $stderr, StringIO.new
6
+
7
+ @model = new_model do
8
+ field :status, :type => Integer, :default => 0
9
+ end
10
+ @machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
11
+ @machine.state :parked, :value => 1
12
+ @record = @model.new
13
+ end
14
+
15
+ def test_should_use_machine_default
16
+ assert_equal 1, @record.status
17
+ end
18
+
19
+ def test_should_generate_a_warning
20
+ assert_match(/Both MongoidTest::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)
21
+ end
22
+
23
+ def teardown
24
+ $stderr = @original_stderr
25
+ super
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDifferentSameDefaultTest < BaseTestCase
4
+ def setup
5
+ @original_stderr, $stderr = $stderr, StringIO.new
6
+
7
+ @model = new_model do
8
+ field :status, :type => String, :default => 'parked'
9
+ end
10
+ @machine = StateMachines::Machine.new(@model, :status, :initial => :parked)
11
+ @record = @model.new
12
+ end
13
+
14
+ def test_should_use_machine_default
15
+ assert_equal 'parked', @record.status
16
+ end
17
+
18
+ def test_should_not_generate_a_warning
19
+ refute_match(/have defined a different default/, $stderr.string)
20
+ end
21
+
22
+ def teardown
23
+ $stderr = @original_stderr
24
+ super
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDirtyAttributeAndCustomAttributesDuringLoopbackTest < BaseTestCase
4
+ def setup
5
+ @model = new_model do
6
+ field :status, :type => String
7
+ end
8
+ @machine = StateMachines::Machine.new(@model, :status, :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_include_state_in_changed_attributes
18
+ assert_equal [], @record.changed
19
+ end
20
+
21
+ def test_should_track_attribute_changes
22
+ assert_equal nil, @record.send(:attribute_change, 'status')
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDirtyAttributeAndStateEventsTest < BaseTestCase
4
+ def setup
5
+ @model = new_model
6
+ @machine = StateMachines::Machine.new(@model, :initial => :parked)
7
+ @machine.event :ignite
8
+
9
+ @record = @model.create
10
+ @record.state_event = 'ignite'
11
+ end
12
+
13
+ def test_should_not_include_state_in_changed_attributes
14
+ assert_equal [], @record.changed
15
+ end
16
+
17
+ def test_should_not_track_attribute_change
18
+ assert_equal nil, @record.send(:attribute_change, 'state')
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDirtyAttributesAndCustomAttributeTest < BaseTestCase
4
+ def setup
5
+ @model = new_model do
6
+ field :status, :type => String
7
+ end
8
+ @machine = StateMachines::Machine.new(@model, :status, :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(status), @record.changed
20
+ end
21
+
22
+ def test_should_track_attribute_change
23
+ assert_equal %w(parked idling), @record.changes['status']
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['status']
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDirtyAttributesDuringLoopbackTest < BaseTestCase
4
+ def setup
5
+ @model = new_model
6
+ @machine = StateMachines::Machine.new(@model, :initial => :parked)
7
+ @machine.event :park
8
+
9
+ @record = @model.create
10
+
11
+ @transition = StateMachines::Transition.new(@record, @machine, :park, :parked, :parked)
12
+ @transition.perform(false)
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_changes
20
+ assert_equal nil, @record.send(:attribute_change, 'state')
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDirtyAttributesTest < BaseTestCase
4
+ def setup
5
+ @model = new_model
6
+ @machine = StateMachines::Machine.new(@model, :initial => :parked)
7
+ @machine.event :ignite
8
+ @machine.state :idling
9
+
10
+ @record = @model.create
11
+
12
+ @transition = StateMachines::Transition.new(@record, @machine, :ignite, :parked, :idling)
13
+ @transition.perform(false)
14
+ end
15
+
16
+ def test_should_include_state_in_changed_attributes
17
+ assert_equal %w(state), @record.changed
18
+ end
19
+
20
+ def test_should_track_attribute_change
21
+ assert_equal %w(parked idling), @record.changes['state']
22
+ end
23
+
24
+ def test_should_not_reset_changes_on_multiple_transitions
25
+ transition = StateMachines::Transition.new(@record, @machine, :ignite, :idling, :idling)
26
+ transition.perform(false)
27
+
28
+ assert_equal %w(parked idling), @record.changes['state']
29
+ end
30
+
31
+ def test_should_not_have_changes_when_loaded_from_database
32
+ record = @model.find(@record.id)
33
+ assert !record.changed?
34
+ end
35
+ end
@@ -0,0 +1,99 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithDynamicInitialStateTest < BaseTestCase
4
+ def setup
5
+ @model = new_model do
6
+ attr_accessor :value
7
+ end
8
+ @machine = StateMachines::Machine.new(@model, :initial => lambda {|object| :parked})
9
+ @machine.state :parked
10
+ end
11
+
12
+ def test_should_set_initial_state_on_created_object
13
+ record = @model.new
14
+ assert_equal 'parked', record.state
15
+ end
16
+
17
+ def test_should_still_set_attributes
18
+ record = @model.new(:value => 1)
19
+ assert_equal 1, record.value
20
+ end
21
+
22
+ def test_should_still_allow_initialize_blocks
23
+ block_args = nil
24
+ record = @model.new do |*args|
25
+ block_args = args
26
+ end
27
+
28
+ assert_equal [record], block_args
29
+ end
30
+
31
+ def test_should_set_attributes_prior_to_initialize_block
32
+ state = nil
33
+ @model.new do |record|
34
+ state = record.state
35
+ end
36
+
37
+ assert_equal 'parked', state
38
+ end
39
+
40
+ def test_should_set_attributes_prior_to_after_initialize_hook
41
+ state = nil
42
+ @model.after_initialize do |record|
43
+ state = record.state
44
+ end
45
+ @model.new
46
+ assert_equal 'parked', state
47
+ end
48
+
49
+ def test_should_set_initial_state_after_setting_attributes
50
+ @model.class_eval do
51
+ attr_accessor :state_during_setter
52
+
53
+ remove_method :value=
54
+ define_method(:value=) do |value|
55
+ self.state_during_setter = state || 'nil'
56
+ end
57
+ end
58
+
59
+ record = @model.new(:value => 1)
60
+ assert_equal 'nil', record.state_during_setter
61
+ end
62
+
63
+ def test_should_not_set_initial_state_after_already_initialized
64
+ record = @model.new(:value => 1)
65
+ assert_equal 'parked', record.state
66
+
67
+ record.state = 'idling'
68
+ record.attributes = {}
69
+ assert_equal 'idling', record.state
70
+ end
71
+
72
+ def test_should_persist_initial_state
73
+ record = @model.new
74
+ record.save
75
+ record.reload
76
+ assert_equal 'parked', record.state
77
+ end
78
+
79
+ def test_should_persist_initial_state_on_dup
80
+ record = @model.create.dup
81
+ record.save
82
+ record.reload
83
+ assert_equal 'parked', record.state
84
+ end
85
+
86
+ def test_should_use_stored_values_when_loading_from_database
87
+ @machine.state :idling
88
+
89
+ record = @model.find(@model.create(:state => 'idling').id)
90
+ assert_equal 'idling', record.state
91
+ end
92
+
93
+ def test_should_use_stored_values_when_loading_from_database_with_nil_state
94
+ @machine.state nil
95
+
96
+ record = @model.find(@model.create(:state => nil).id)
97
+ assert_nil record.state
98
+ end
99
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'test_helper'
2
+
3
+ class MachineWithEventAttributesOnAutosaveTest < BaseTestCase
4
+ def setup
5
+ @vehicle_model = new_model(:vehicle) do
6
+ belongs_to :owner, :class_name => 'MongoidTest::Owner'
7
+ end
8
+ MongoidTest.const_set('Vehicle', @vehicle_model)
9
+
10
+ @owner_model = new_model(:owner)
11
+ MongoidTest.const_set('Owner', @owner_model)
12
+
13
+ machine = StateMachines::Machine.new(@vehicle_model)
14
+ machine.event :ignite do
15
+ transition :parked => :idling
16
+ end
17
+
18
+ @owner = @owner_model.create
19
+ end
20
+
21
+ def test_should_persist_many_association
22
+ @owner_model.has_many :vehicles, :class_name => 'MongoidTest::Vehicle', :autosave => true
23
+ @vehicle = @vehicle_model.create(:state => 'parked', :owner_id => @owner.id)
24
+
25
+ @owner.vehicles[0].state_event = 'ignite'
26
+ @owner.save
27
+
28
+ @vehicle.reload
29
+ assert_equal 'idling', @vehicle.state
30
+ end
31
+
32
+ def test_should_persist_one_association
33
+ @owner_model.has_one :vehicle, :class_name => 'MongoidTest::Vehicle', :autosave => true
34
+ @vehicle = @vehicle_model.create(:state => 'parked', :owner_id => @owner.id)
35
+
36
+ @owner.vehicle.state_event = 'ignite'
37
+ @owner.save
38
+
39
+ @vehicle.reload
40
+ assert_equal 'idling', @vehicle.state
41
+ end
42
+
43
+ def teardown
44
+ MongoidTest.class_eval do
45
+ remove_const('Vehicle')
46
+ remove_const('Owner')
47
+ end
48
+ super
49
+ end
50
+ end