state_machine 1.0.3 → 1.1.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.
- data/.travis.yml +23 -7
- data/Appraisals +31 -25
- data/CHANGELOG.md +12 -0
- data/README.md +3 -1
- data/gemfiles/active_model-3.0.0.gemfile.lock +7 -5
- data/gemfiles/active_model-3.0.5.gemfile.lock +7 -5
- data/gemfiles/active_model-3.1.1.gemfile.lock +6 -4
- data/gemfiles/active_record-2.0.0.gemfile +1 -1
- data/gemfiles/active_record-2.0.0.gemfile.lock +7 -5
- data/gemfiles/active_record-2.0.5.gemfile +1 -1
- data/gemfiles/active_record-2.0.5.gemfile.lock +7 -5
- data/gemfiles/active_record-2.1.0.gemfile +1 -1
- data/gemfiles/active_record-2.1.0.gemfile.lock +7 -5
- data/gemfiles/active_record-2.1.2.gemfile +1 -1
- data/gemfiles/active_record-2.1.2.gemfile.lock +7 -5
- data/gemfiles/active_record-2.2.3.gemfile +1 -1
- data/gemfiles/active_record-2.2.3.gemfile.lock +7 -5
- data/gemfiles/active_record-2.3.12.gemfile +1 -1
- data/gemfiles/active_record-2.3.12.gemfile.lock +7 -5
- data/gemfiles/active_record-3.0.0.gemfile +1 -1
- data/gemfiles/active_record-3.0.0.gemfile.lock +8 -6
- data/gemfiles/active_record-3.0.5.gemfile +1 -1
- data/gemfiles/active_record-3.0.5.gemfile.lock +8 -6
- data/gemfiles/active_record-3.1.1.gemfile +1 -1
- data/gemfiles/active_record-3.1.1.gemfile.lock +7 -5
- data/gemfiles/data_mapper-0.10.2.gemfile +3 -3
- data/gemfiles/data_mapper-0.10.2.gemfile.lock +14 -5
- data/gemfiles/data_mapper-0.9.11.gemfile +3 -3
- data/gemfiles/data_mapper-0.9.11.gemfile.lock +7 -5
- data/gemfiles/data_mapper-0.9.4.gemfile +3 -3
- data/gemfiles/data_mapper-0.9.4.gemfile.lock +13 -13
- data/gemfiles/data_mapper-0.9.7.gemfile +3 -3
- data/gemfiles/data_mapper-0.9.7.gemfile.lock +13 -13
- data/gemfiles/data_mapper-1.0.0.gemfile +3 -3
- data/gemfiles/data_mapper-1.0.0.gemfile.lock +17 -8
- data/gemfiles/data_mapper-1.0.1.gemfile +3 -3
- data/gemfiles/data_mapper-1.0.1.gemfile.lock +17 -8
- data/gemfiles/data_mapper-1.0.2.gemfile +3 -3
- data/gemfiles/data_mapper-1.0.2.gemfile.lock +17 -8
- data/gemfiles/data_mapper-1.1.0.gemfile +3 -3
- data/gemfiles/data_mapper-1.1.0.gemfile.lock +17 -8
- data/gemfiles/data_mapper-1.2.0.gemfile +3 -3
- data/gemfiles/data_mapper-1.2.0.gemfile.lock +13 -4
- data/gemfiles/default.gemfile.lock +7 -5
- data/gemfiles/graphviz-0.9.0.gemfile.lock +6 -4
- data/gemfiles/graphviz-0.9.21.gemfile.lock +6 -4
- data/gemfiles/graphviz-1.0.0.gemfile.lock +6 -4
- data/gemfiles/mongo_mapper-0.10.0.gemfile.lock +6 -3
- data/gemfiles/mongo_mapper-0.5.5.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.5.5.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.5.8.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.5.8.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.6.0.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.6.0.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.6.10.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.6.10.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.7.0.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.7.0.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.7.5.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.7.5.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.8.0.gemfile +2 -2
- data/gemfiles/mongo_mapper-0.8.0.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.8.3.gemfile +2 -2
- data/gemfiles/mongo_mapper-0.8.3.gemfile.lock +7 -5
- data/gemfiles/mongo_mapper-0.8.4.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.8.4.gemfile.lock +11 -8
- data/gemfiles/mongo_mapper-0.8.6.gemfile +1 -1
- data/gemfiles/mongo_mapper-0.8.6.gemfile.lock +11 -8
- data/gemfiles/mongo_mapper-0.9.0.gemfile.lock +14 -11
- data/gemfiles/mongoid-2.0.0.gemfile.lock +22 -17
- data/gemfiles/mongoid-2.1.4.gemfile.lock +21 -16
- data/gemfiles/mongoid-2.2.4.gemfile.lock +11 -8
- data/gemfiles/mongoid-2.3.3.gemfile.lock +11 -8
- data/gemfiles/sequel-2.11.0.gemfile.lock +7 -5
- data/gemfiles/sequel-2.12.0.gemfile.lock +7 -5
- data/gemfiles/sequel-2.8.0.gemfile.lock +7 -5
- data/gemfiles/sequel-3.0.0.gemfile.lock +7 -5
- data/gemfiles/sequel-3.13.0.gemfile.lock +7 -5
- data/gemfiles/sequel-3.14.0.gemfile.lock +7 -5
- data/gemfiles/sequel-3.23.0.gemfile.lock +7 -5
- data/gemfiles/sequel-3.24.0.gemfile.lock +7 -5
- data/gemfiles/sequel-3.29.0.gemfile.lock +5 -3
- data/lib/state_machine.rb +7 -1
- data/lib/state_machine/eval_helpers.rb +8 -9
- data/lib/state_machine/event.rb +10 -2
- data/lib/state_machine/integrations.rb +0 -1
- data/lib/state_machine/integrations/active_model.rb +46 -35
- data/lib/state_machine/integrations/active_record.rb +8 -0
- data/lib/state_machine/integrations/active_record/versions.rb +0 -20
- data/lib/state_machine/integrations/data_mapper.rb +22 -21
- data/lib/state_machine/integrations/data_mapper/versions.rb +0 -27
- data/lib/state_machine/integrations/mongo_mapper.rb +8 -0
- data/lib/state_machine/integrations/mongo_mapper/versions.rb +0 -4
- data/lib/state_machine/integrations/mongoid.rb +15 -2
- data/lib/state_machine/integrations/mongoid/versions.rb +0 -7
- data/lib/state_machine/integrations/sequel.rb +14 -0
- data/lib/state_machine/machine.rb +12 -0
- data/lib/state_machine/state.rb +4 -3
- data/lib/state_machine/state_context.rb +10 -9
- data/lib/state_machine/transition.rb +6 -1
- data/lib/state_machine/version.rb +1 -1
- data/state_machine.gemspec +1 -1
- data/test/functional/state_machine_test.rb +21 -1
- data/test/unit/event_test.rb +10 -0
- data/test/unit/integrations/active_model_test.rb +31 -29
- data/test/unit/integrations/active_record_test.rb +56 -26
- data/test/unit/integrations/data_mapper_test.rb +34 -57
- data/test/unit/integrations/mongo_mapper_test.rb +30 -24
- data/test/unit/integrations/mongoid_test.rb +114 -29
- data/test/unit/integrations/sequel_test.rb +36 -0
- data/test/unit/invalid_transition_test.rb +38 -0
- data/test/unit/machine_test.rb +38 -0
- data/test/unit/state_context_test.rb +29 -9
- data/test/unit/state_test.rb +21 -1
- data/test/unit/transition_collection_test.rb +72 -26
- data/test/unit/transition_test.rb +84 -73
- metadata +8 -8
|
@@ -538,8 +538,8 @@ module DataMapperTest
|
|
|
538
538
|
@transition.perform
|
|
539
539
|
end
|
|
540
540
|
|
|
541
|
-
def
|
|
542
|
-
|
|
541
|
+
def test_should_not_update_record
|
|
542
|
+
assert_equal @timestamp, @record.updated_at
|
|
543
543
|
end
|
|
544
544
|
end
|
|
545
545
|
|
|
@@ -579,10 +579,10 @@ module DataMapperTest
|
|
|
579
579
|
end
|
|
580
580
|
end
|
|
581
581
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
582
|
+
def test_should_not_have_changes_when_loaded_from_database
|
|
583
|
+
record = @resource.get(@record.id)
|
|
584
|
+
assert record.dirty_attributes.empty?
|
|
585
|
+
end
|
|
586
586
|
end
|
|
587
587
|
|
|
588
588
|
class MachineWithDirtyAttributesDuringLoopbackTest < BaseTestCase
|
|
@@ -597,18 +597,8 @@ module DataMapperTest
|
|
|
597
597
|
@transition.perform(false)
|
|
598
598
|
end
|
|
599
599
|
|
|
600
|
-
def
|
|
601
|
-
assert_equal e = {
|
|
602
|
-
end
|
|
603
|
-
|
|
604
|
-
def test_should_track_attribute_change
|
|
605
|
-
if Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.3')
|
|
606
|
-
assert_equal e = {@resource.properties[:state] => 'parked'}, @record.original_attributes
|
|
607
|
-
elsif Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.0')
|
|
608
|
-
assert_equal e = {@resource.properties[:state] => 'parked-ignored'}, @record.original_attributes
|
|
609
|
-
else
|
|
610
|
-
assert_equal e = {:state => 'parked-ignored'}, @record.original_values
|
|
611
|
-
end
|
|
600
|
+
def test_should_not_include_state_in_changed_attributes
|
|
601
|
+
assert_equal e = {}, @record.dirty_attributes
|
|
612
602
|
end
|
|
613
603
|
end
|
|
614
604
|
|
|
@@ -665,18 +655,8 @@ module DataMapperTest
|
|
|
665
655
|
@transition.perform(false)
|
|
666
656
|
end
|
|
667
657
|
|
|
668
|
-
def
|
|
669
|
-
assert_equal e = {
|
|
670
|
-
end
|
|
671
|
-
|
|
672
|
-
def test_should_track_attribute_changes
|
|
673
|
-
if Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.3')
|
|
674
|
-
assert_equal e = {@resource.properties[:status] => 'parked'}, @record.original_attributes
|
|
675
|
-
elsif Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.0')
|
|
676
|
-
assert_equal e = {@resource.properties[:status] => 'parked-ignored'}, @record.original_attributes
|
|
677
|
-
else
|
|
678
|
-
assert_equal e = {:status => 'parked-ignored'}, @record.original_values
|
|
679
|
-
end
|
|
658
|
+
def test_should_not_include_state_in_changed_attributes
|
|
659
|
+
assert_equal e = {}, @record.dirty_attributes
|
|
680
660
|
end
|
|
681
661
|
end
|
|
682
662
|
|
|
@@ -690,38 +670,17 @@ module DataMapperTest
|
|
|
690
670
|
@record.state_event = 'ignite'
|
|
691
671
|
end
|
|
692
672
|
|
|
693
|
-
def
|
|
694
|
-
assert_equal e = {
|
|
695
|
-
end
|
|
696
|
-
|
|
697
|
-
def test_should_track_attribute_change
|
|
698
|
-
if Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.3')
|
|
699
|
-
assert_equal e = {@resource.properties[:state] => 'parked'}, @record.original_attributes
|
|
700
|
-
elsif Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.0')
|
|
701
|
-
assert_equal e = {@resource.properties[:state] => 'parked-ignored'}, @record.original_attributes
|
|
702
|
-
else
|
|
703
|
-
assert_equal e = {:state => 'parked-ignored'}, @record.original_values
|
|
704
|
-
end
|
|
673
|
+
def test_should_not_include_state_in_changed_attributes
|
|
674
|
+
assert_equal e = {}, @record.dirty_attributes
|
|
705
675
|
end
|
|
706
676
|
|
|
707
|
-
def
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
if Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.3')
|
|
711
|
-
assert_equal e = {@resource.properties[:state] => 'parked'}, @record.original_attributes
|
|
712
|
-
elsif Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.0')
|
|
713
|
-
assert_equal e = {@resource.properties[:state] => 'parked-ignored'}, @record.original_attributes
|
|
677
|
+
def test_should_not_track_attribute_change
|
|
678
|
+
if Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.10.0')
|
|
679
|
+
assert_equal e = {}, @record.original_attributes
|
|
714
680
|
else
|
|
715
|
-
assert_equal e = {
|
|
681
|
+
assert_equal e = {}, @record.original_values
|
|
716
682
|
end
|
|
717
683
|
end
|
|
718
|
-
|
|
719
|
-
def test_should_not_include_state_in_changed_attributes_if_nil
|
|
720
|
-
@record = @resource.create
|
|
721
|
-
@record.state_event = nil
|
|
722
|
-
|
|
723
|
-
assert_equal e = {}, @record.dirty_attributes
|
|
724
|
-
end
|
|
725
684
|
end
|
|
726
685
|
|
|
727
686
|
class MachineWithoutTransactionsTest < BaseTestCase
|
|
@@ -1073,6 +1032,24 @@ module DataMapperTest
|
|
|
1073
1032
|
end
|
|
1074
1033
|
end
|
|
1075
1034
|
|
|
1035
|
+
class MachineErrorsTest < BaseTestCase
|
|
1036
|
+
def setup
|
|
1037
|
+
@resource = new_resource
|
|
1038
|
+
@machine = StateMachine::Machine.new(@resource)
|
|
1039
|
+
@record = @resource.new
|
|
1040
|
+
end
|
|
1041
|
+
|
|
1042
|
+
def test_should_be_able_to_describe_current_errors
|
|
1043
|
+
@record.errors.add(:id, 'cannot be blank')
|
|
1044
|
+
@record.errors.add(:state, 'is invalid')
|
|
1045
|
+
assert_equal ['id cannot be blank', 'state is invalid'], @machine.errors_for(@record).split(', ').sort
|
|
1046
|
+
end
|
|
1047
|
+
|
|
1048
|
+
def test_should_describe_as_halted_with_no_errors
|
|
1049
|
+
assert_equal 'Transition halted', @machine.errors_for(@record)
|
|
1050
|
+
end
|
|
1051
|
+
end
|
|
1052
|
+
|
|
1076
1053
|
class MachineWithStateDrivenValidationsTest < BaseTestCase
|
|
1077
1054
|
def setup
|
|
1078
1055
|
@resource = resource = new_resource do
|
|
@@ -585,12 +585,12 @@ module MongoMapperTest
|
|
|
585
585
|
@transition.perform(false)
|
|
586
586
|
end
|
|
587
587
|
|
|
588
|
-
def
|
|
589
|
-
assert_equal
|
|
588
|
+
def test_should_not_include_state_in_changed_attributes
|
|
589
|
+
assert_equal [], @record.changed
|
|
590
590
|
end
|
|
591
591
|
|
|
592
|
-
def
|
|
593
|
-
assert_equal
|
|
592
|
+
def test_should_not_track_attribute_changes
|
|
593
|
+
assert_equal nil, @record.changes['state']
|
|
594
594
|
end
|
|
595
595
|
end
|
|
596
596
|
|
|
@@ -639,12 +639,12 @@ module MongoMapperTest
|
|
|
639
639
|
@transition.perform(false)
|
|
640
640
|
end
|
|
641
641
|
|
|
642
|
-
def
|
|
643
|
-
assert_equal
|
|
642
|
+
def test_should_not_include_state_in_changed_attributes
|
|
643
|
+
assert_equal [], @record.changed
|
|
644
644
|
end
|
|
645
645
|
|
|
646
|
-
def
|
|
647
|
-
assert_equal
|
|
646
|
+
def test_should_not_track_attribute_changes
|
|
647
|
+
assert_equal nil, @record.changes['status']
|
|
648
648
|
end
|
|
649
649
|
end
|
|
650
650
|
|
|
@@ -658,24 +658,12 @@ module MongoMapperTest
|
|
|
658
658
|
@record.state_event = 'ignite'
|
|
659
659
|
end
|
|
660
660
|
|
|
661
|
-
def
|
|
662
|
-
assert_equal
|
|
663
|
-
end
|
|
664
|
-
|
|
665
|
-
def test_should_track_attribute_change
|
|
666
|
-
assert_equal %w(parked parked), @record.changes['state']
|
|
667
|
-
end
|
|
668
|
-
|
|
669
|
-
def test_should_not_reset_changes_on_multiple_changes
|
|
670
|
-
@record.state_event = 'ignite'
|
|
671
|
-
assert_equal %w(parked parked), @record.changes['state']
|
|
661
|
+
def test_should_not_include_state_in_changed_attributes
|
|
662
|
+
assert_equal [], @record.changed
|
|
672
663
|
end
|
|
673
664
|
|
|
674
|
-
def
|
|
675
|
-
|
|
676
|
-
@record.state_event = nil
|
|
677
|
-
|
|
678
|
-
assert_equal [], @record.changed
|
|
665
|
+
def test_should_not_track_attribute_change
|
|
666
|
+
assert_equal nil, @record.changes['state']
|
|
679
667
|
end
|
|
680
668
|
end
|
|
681
669
|
|
|
@@ -1010,6 +998,24 @@ module MongoMapperTest
|
|
|
1010
998
|
assert @record.valid?
|
|
1011
999
|
end
|
|
1012
1000
|
end
|
|
1001
|
+
|
|
1002
|
+
class MachineErrorsTest < BaseTestCase
|
|
1003
|
+
def setup
|
|
1004
|
+
@model = new_model
|
|
1005
|
+
@machine = StateMachine::Machine.new(@model)
|
|
1006
|
+
@record = @model.new
|
|
1007
|
+
end
|
|
1008
|
+
|
|
1009
|
+
def test_should_be_able_to_describe_current_errors
|
|
1010
|
+
@record.errors.add(:id, 'cannot be blank')
|
|
1011
|
+
@record.errors.add(:state, 'is invalid')
|
|
1012
|
+
assert_equal ['Id cannot be blank', 'State is invalid'], @machine.errors_for(@record).split(', ').sort
|
|
1013
|
+
end
|
|
1014
|
+
|
|
1015
|
+
def test_should_describe_as_halted_with_no_errors
|
|
1016
|
+
assert_equal 'Transition halted', @machine.errors_for(@record)
|
|
1017
|
+
end
|
|
1018
|
+
end
|
|
1013
1019
|
|
|
1014
1020
|
class MachineWithStateDrivenValidationsTest < BaseTestCase
|
|
1015
1021
|
def setup
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
|
|
2
2
|
|
|
3
3
|
require 'mongoid'
|
|
4
|
+
require 'mongoid/version'
|
|
4
5
|
|
|
5
6
|
# Establish database connection
|
|
6
7
|
Mongoid.configure do |config|
|
|
@@ -124,6 +125,24 @@ module MongoidTest
|
|
|
124
125
|
assert_equal [record], block_args
|
|
125
126
|
end
|
|
126
127
|
|
|
128
|
+
def test_should_set_attributes_prior_to_initialize_block
|
|
129
|
+
state = nil
|
|
130
|
+
record = @model.new do |record|
|
|
131
|
+
state = record.state
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
assert_equal 'parked', state
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def test_should_set_attributes_prior_to_after_initialize_hook
|
|
138
|
+
state = nil
|
|
139
|
+
@model.after_initialize do |record|
|
|
140
|
+
state = record.state
|
|
141
|
+
end
|
|
142
|
+
@model.new
|
|
143
|
+
assert_equal 'parked', state
|
|
144
|
+
end
|
|
145
|
+
|
|
127
146
|
def test_should_set_initial_state_before_setting_attributes
|
|
128
147
|
@model.class_eval do
|
|
129
148
|
attr_accessor :state_during_setter
|
|
@@ -189,6 +208,24 @@ module MongoidTest
|
|
|
189
208
|
assert_equal [record], block_args
|
|
190
209
|
end
|
|
191
210
|
|
|
211
|
+
def test_should_set_attributes_prior_to_initialize_block
|
|
212
|
+
state = nil
|
|
213
|
+
record = @model.new do |record|
|
|
214
|
+
state = record.state
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
assert_equal 'parked', state
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def test_should_set_attributes_prior_to_after_initialize_hook
|
|
221
|
+
state = nil
|
|
222
|
+
@model.after_initialize do |record|
|
|
223
|
+
state = record.state
|
|
224
|
+
end
|
|
225
|
+
@model.new
|
|
226
|
+
assert_equal 'parked', state
|
|
227
|
+
end
|
|
228
|
+
|
|
192
229
|
def test_should_set_initial_state_after_setting_attributes
|
|
193
230
|
@model.class_eval do
|
|
194
231
|
attr_accessor :state_during_setter
|
|
@@ -575,12 +612,22 @@ module MongoidTest
|
|
|
575
612
|
@transition.perform(false)
|
|
576
613
|
end
|
|
577
614
|
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
615
|
+
if ::Mongoid::VERSION =~ /^2\.0\./
|
|
616
|
+
def test_should_include_state_in_changed_attributes
|
|
617
|
+
assert_equal %w(state), @record.changed
|
|
618
|
+
end
|
|
619
|
+
|
|
620
|
+
def test_should_not_track_attribute_changes
|
|
621
|
+
assert_equal %w(parked parked), @record.send(:attribute_change, 'state')
|
|
622
|
+
end
|
|
623
|
+
else
|
|
624
|
+
def test_should_not_include_state_in_changed_attributes
|
|
625
|
+
assert_equal [], @record.changed
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
def test_should_not_track_attribute_changes
|
|
629
|
+
assert_equal nil, @record.send(:attribute_change, 'state')
|
|
630
|
+
end
|
|
584
631
|
end
|
|
585
632
|
end
|
|
586
633
|
|
|
@@ -629,12 +676,22 @@ module MongoidTest
|
|
|
629
676
|
@transition.perform(false)
|
|
630
677
|
end
|
|
631
678
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
679
|
+
if ::Mongoid::VERSION =~ /^2\.0\./
|
|
680
|
+
def test_should_include_state_in_changed_attributes
|
|
681
|
+
assert_equal %w(status), @record.changed
|
|
682
|
+
end
|
|
683
|
+
|
|
684
|
+
def test_should_track_attribute_changes
|
|
685
|
+
assert_equal %w(parked parked), @record.send(:attribute_change, 'status')
|
|
686
|
+
end
|
|
687
|
+
else
|
|
688
|
+
def test_should_include_state_in_changed_attributes
|
|
689
|
+
assert_equal [], @record.changed
|
|
690
|
+
end
|
|
691
|
+
|
|
692
|
+
def test_should_track_attribute_changes
|
|
693
|
+
assert_equal nil, @record.send(:attribute_change, 'status')
|
|
694
|
+
end
|
|
638
695
|
end
|
|
639
696
|
end
|
|
640
697
|
|
|
@@ -648,24 +705,34 @@ module MongoidTest
|
|
|
648
705
|
@record.state_event = 'ignite'
|
|
649
706
|
end
|
|
650
707
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
def test_should_not_include_state_in_changed_attributes_if_nil
|
|
665
|
-
@record = @model.create
|
|
666
|
-
@record.state_event = nil
|
|
708
|
+
if ::Mongoid::VERSION =~ /^2\.0\./
|
|
709
|
+
def test_should_include_state_in_changed_attributes
|
|
710
|
+
assert_equal %w(state), @record.changed
|
|
711
|
+
end
|
|
712
|
+
|
|
713
|
+
def test_should_track_attribute_change
|
|
714
|
+
assert_equal %w(parked parked), @record.send(:attribute_change, 'state')
|
|
715
|
+
end
|
|
716
|
+
|
|
717
|
+
def test_should_not_reset_changes_on_multiple_changes
|
|
718
|
+
@record.state_event = 'ignite'
|
|
719
|
+
assert_equal %w(parked parked), @record.send(:attribute_change, 'state')
|
|
720
|
+
end
|
|
667
721
|
|
|
668
|
-
|
|
722
|
+
def test_should_not_include_state_in_changed_attributes_if_nil
|
|
723
|
+
@record = @model.create
|
|
724
|
+
@record.state_event = nil
|
|
725
|
+
|
|
726
|
+
assert_equal [], @record.changed
|
|
727
|
+
end
|
|
728
|
+
else
|
|
729
|
+
def test_should_not_include_state_in_changed_attributes
|
|
730
|
+
assert_equal [], @record.changed
|
|
731
|
+
end
|
|
732
|
+
|
|
733
|
+
def test_should_not_track_attribute_change
|
|
734
|
+
assert_equal nil, @record.send(:attribute_change, 'state')
|
|
735
|
+
end
|
|
669
736
|
end
|
|
670
737
|
end
|
|
671
738
|
|
|
@@ -976,6 +1043,24 @@ module MongoidTest
|
|
|
976
1043
|
assert @record.valid?
|
|
977
1044
|
end
|
|
978
1045
|
end
|
|
1046
|
+
|
|
1047
|
+
class MachineErrorsTest < BaseTestCase
|
|
1048
|
+
def setup
|
|
1049
|
+
@model = new_model
|
|
1050
|
+
@machine = StateMachine::Machine.new(@model)
|
|
1051
|
+
@record = @model.new
|
|
1052
|
+
end
|
|
1053
|
+
|
|
1054
|
+
def test_should_be_able_to_describe_current_errors
|
|
1055
|
+
@record.errors.add(:id, 'cannot be blank')
|
|
1056
|
+
@record.errors.add(:state, 'is invalid')
|
|
1057
|
+
assert_equal ['Id cannot be blank', 'State is invalid'], @machine.errors_for(@record).split(', ').sort
|
|
1058
|
+
end
|
|
1059
|
+
|
|
1060
|
+
def test_should_describe_as_halted_with_no_errors
|
|
1061
|
+
assert_equal 'Transition halted', @machine.errors_for(@record)
|
|
1062
|
+
end
|
|
1063
|
+
end
|
|
979
1064
|
|
|
980
1065
|
class MachineWithStateDrivenValidationsTest < BaseTestCase
|
|
981
1066
|
def setup
|
|
@@ -158,6 +158,15 @@ module SequelTest
|
|
|
158
158
|
assert_equal [record], block_args
|
|
159
159
|
end
|
|
160
160
|
|
|
161
|
+
def test_should_set_attributes_prior_to_initialize_block
|
|
162
|
+
state = nil
|
|
163
|
+
record = @model.new do |record|
|
|
164
|
+
state = record.state
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
assert_equal 'parked', state
|
|
168
|
+
end
|
|
169
|
+
|
|
161
170
|
def test_should_set_attributes_prior_to_after_initialize_hook
|
|
162
171
|
state = nil
|
|
163
172
|
@model.class_eval do
|
|
@@ -239,6 +248,15 @@ module SequelTest
|
|
|
239
248
|
assert record.changed_columns.empty?
|
|
240
249
|
end
|
|
241
250
|
|
|
251
|
+
def test_should_set_attributes_prior_to_initialize_block
|
|
252
|
+
state = nil
|
|
253
|
+
record = @model.new do |record|
|
|
254
|
+
state = record.state
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
assert_equal 'parked', state
|
|
258
|
+
end
|
|
259
|
+
|
|
242
260
|
def test_should_set_attributes_prior_to_after_initialize_hook
|
|
243
261
|
state = nil
|
|
244
262
|
@model.class_eval do
|
|
@@ -998,6 +1016,24 @@ module SequelTest
|
|
|
998
1016
|
end
|
|
999
1017
|
end
|
|
1000
1018
|
|
|
1019
|
+
class MachineErrorsTest < BaseTestCase
|
|
1020
|
+
def setup
|
|
1021
|
+
@model = new_model
|
|
1022
|
+
@machine = StateMachine::Machine.new(@model)
|
|
1023
|
+
@record = @model.new
|
|
1024
|
+
end
|
|
1025
|
+
|
|
1026
|
+
def test_should_be_able_to_describe_current_errors
|
|
1027
|
+
@record.errors.add(:id, 'cannot be blank')
|
|
1028
|
+
@record.errors.add(:state, 'is invalid')
|
|
1029
|
+
assert_equal ['id cannot be blank', 'state is invalid'], @machine.errors_for(@record).split(', ').sort
|
|
1030
|
+
end
|
|
1031
|
+
|
|
1032
|
+
def test_should_describe_as_halted_with_no_errors
|
|
1033
|
+
assert_equal 'Transition halted', @machine.errors_for(@record)
|
|
1034
|
+
end
|
|
1035
|
+
end
|
|
1036
|
+
|
|
1001
1037
|
class MachineWithStateDrivenValidationsTest < BaseTestCase
|
|
1002
1038
|
def setup
|
|
1003
1039
|
@model = new_model do
|