state_machine 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +0 -2
  3. data/.yardopts +3 -2
  4. data/Appraisals +48 -0
  5. data/{CHANGELOG.rdoc → CHANGELOG.md} +63 -46
  6. data/README.md +1029 -0
  7. data/gemfiles/active_model-3.0.0.gemfile.lock +1 -3
  8. data/gemfiles/active_model-3.0.5.gemfile.lock +1 -3
  9. data/gemfiles/active_model-3.1.1.gemfile +7 -0
  10. data/gemfiles/active_model-3.1.1.gemfile.lock +32 -0
  11. data/gemfiles/active_record-2.0.0.gemfile.lock +1 -3
  12. data/gemfiles/active_record-2.0.5.gemfile.lock +1 -3
  13. data/gemfiles/active_record-2.1.0.gemfile.lock +1 -3
  14. data/gemfiles/active_record-2.1.2.gemfile.lock +1 -3
  15. data/gemfiles/active_record-2.2.3.gemfile.lock +1 -3
  16. data/gemfiles/active_record-2.3.12.gemfile.lock +1 -3
  17. data/gemfiles/active_record-3.0.0.gemfile.lock +1 -3
  18. data/gemfiles/active_record-3.0.5.gemfile.lock +1 -3
  19. data/gemfiles/active_record-3.1.1.gemfile +8 -0
  20. data/gemfiles/active_record-3.1.1.gemfile.lock +43 -0
  21. data/gemfiles/data_mapper-0.10.2.gemfile.lock +1 -3
  22. data/gemfiles/data_mapper-0.9.11.gemfile.lock +1 -3
  23. data/gemfiles/data_mapper-0.9.4.gemfile.lock +1 -3
  24. data/gemfiles/data_mapper-0.9.7.gemfile.lock +1 -3
  25. data/gemfiles/data_mapper-1.0.0.gemfile.lock +1 -3
  26. data/gemfiles/data_mapper-1.0.1.gemfile.lock +1 -3
  27. data/gemfiles/data_mapper-1.0.2.gemfile.lock +1 -3
  28. data/gemfiles/data_mapper-1.1.0.gemfile.lock +1 -3
  29. data/gemfiles/data_mapper-1.2.0.gemfile +12 -0
  30. data/gemfiles/data_mapper-1.2.0.gemfile.lock +49 -0
  31. data/gemfiles/default.gemfile.lock +1 -3
  32. data/gemfiles/graphviz-0.9.0.gemfile +7 -0
  33. data/gemfiles/graphviz-0.9.0.gemfile.lock +24 -0
  34. data/gemfiles/graphviz-0.9.21.gemfile +7 -0
  35. data/gemfiles/graphviz-0.9.21.gemfile.lock +24 -0
  36. data/gemfiles/graphviz-1.0.0.gemfile +7 -0
  37. data/gemfiles/graphviz-1.0.0.gemfile.lock +24 -0
  38. data/gemfiles/mongo_mapper-0.10.0.gemfile +7 -0
  39. data/gemfiles/mongo_mapper-0.10.0.gemfile.lock +41 -0
  40. data/gemfiles/mongo_mapper-0.5.5.gemfile.lock +1 -3
  41. data/gemfiles/mongo_mapper-0.5.8.gemfile.lock +1 -3
  42. data/gemfiles/mongo_mapper-0.6.0.gemfile.lock +1 -3
  43. data/gemfiles/mongo_mapper-0.6.10.gemfile.lock +1 -3
  44. data/gemfiles/mongo_mapper-0.7.0.gemfile.lock +1 -3
  45. data/gemfiles/mongo_mapper-0.7.5.gemfile.lock +1 -3
  46. data/gemfiles/mongo_mapper-0.8.0.gemfile.lock +1 -3
  47. data/gemfiles/mongo_mapper-0.8.3.gemfile.lock +1 -3
  48. data/gemfiles/mongo_mapper-0.8.4.gemfile.lock +1 -3
  49. data/gemfiles/mongo_mapper-0.8.6.gemfile.lock +1 -3
  50. data/gemfiles/mongo_mapper-0.9.0.gemfile.lock +1 -3
  51. data/gemfiles/mongoid-2.0.0.gemfile.lock +1 -3
  52. data/gemfiles/mongoid-2.1.4.gemfile.lock +1 -3
  53. data/gemfiles/mongoid-2.2.4.gemfile +7 -0
  54. data/gemfiles/mongoid-2.2.4.gemfile.lock +40 -0
  55. data/gemfiles/mongoid-2.3.3.gemfile +7 -0
  56. data/gemfiles/mongoid-2.3.3.gemfile.lock +40 -0
  57. data/gemfiles/sequel-2.11.0.gemfile.lock +1 -3
  58. data/gemfiles/sequel-2.12.0.gemfile.lock +1 -3
  59. data/gemfiles/sequel-2.8.0.gemfile.lock +1 -3
  60. data/gemfiles/sequel-3.0.0.gemfile.lock +1 -3
  61. data/gemfiles/sequel-3.13.0.gemfile.lock +1 -3
  62. data/gemfiles/sequel-3.14.0.gemfile.lock +1 -3
  63. data/gemfiles/sequel-3.23.0.gemfile.lock +1 -3
  64. data/gemfiles/sequel-3.24.0.gemfile.lock +1 -3
  65. data/gemfiles/sequel-3.29.0.gemfile +8 -0
  66. data/gemfiles/sequel-3.29.0.gemfile.lock +26 -0
  67. data/lib/state_machine.rb +45 -0
  68. data/lib/state_machine/event.rb +18 -3
  69. data/lib/state_machine/event_collection.rb +1 -1
  70. data/lib/state_machine/integrations/active_model.rb +59 -16
  71. data/lib/state_machine/integrations/active_model/observer.rb +3 -15
  72. data/lib/state_machine/integrations/active_record.rb +46 -9
  73. data/lib/state_machine/integrations/data_mapper.rb +42 -2
  74. data/lib/state_machine/integrations/data_mapper/versions.rb +22 -10
  75. data/lib/state_machine/integrations/mongo_mapper.rb +55 -0
  76. data/lib/state_machine/integrations/mongo_mapper/versions.rb +3 -3
  77. data/lib/state_machine/integrations/mongoid.rb +57 -12
  78. data/lib/state_machine/integrations/mongoid/versions.rb +22 -4
  79. data/lib/state_machine/integrations/sequel.rb +45 -0
  80. data/lib/state_machine/integrations/sequel/versions.rb +3 -0
  81. data/lib/state_machine/machine.rb +148 -34
  82. data/lib/state_machine/node_collection.rb +36 -3
  83. data/lib/state_machine/state.rb +6 -3
  84. data/lib/state_machine/state_collection.rb +1 -1
  85. data/lib/state_machine/version.rb +1 -1
  86. data/lib/tasks/state_machine.rb +11 -9
  87. data/state_machine.gemspec +2 -3
  88. data/test/functional/state_machine_test.rb +54 -1
  89. data/test/unit/event_collection_test.rb +4 -0
  90. data/test/unit/event_test.rb +34 -1
  91. data/test/unit/integrations/active_model_test.rb +80 -0
  92. data/test/unit/integrations/active_record_test.rb +105 -2
  93. data/test/unit/integrations/data_mapper_test.rb +27 -25
  94. data/test/unit/integrations/mongo_mapper_test.rb +80 -25
  95. data/test/unit/integrations/mongoid_test.rb +61 -6
  96. data/test/unit/integrations/sequel_test.rb +8 -2
  97. data/test/unit/machine_test.rb +87 -9
  98. data/test/unit/node_collection_test.rb +129 -12
  99. data/test/unit/state_collection_test.rb +4 -0
  100. data/test/unit/state_test.rb +2 -2
  101. metadata +30 -24
  102. data/README.rdoc +0 -844
@@ -366,14 +366,14 @@ module ActiveRecordTest
366
366
  @machine = StateMachine::Machine.new(@model)
367
367
  @machine.state :state
368
368
 
369
- assert_equal "Instance method \"state?\" is already defined in ActiveRecordTest::Foo, use generic helper instead.\n", $stderr.string
369
+ assert_match /^Instance method "state\?" is already defined in ActiveRecordTest::Foo, use generic helper instead.*\n$/, $stderr.string
370
370
  end
371
371
 
372
372
  def test_should_output_warning_with_same_machine_attribute
373
373
  @machine = StateMachine::Machine.new(@model, :public_state, :attribute => :state)
374
374
  @machine.state :state
375
375
 
376
- assert_equal "Instance method \"state?\" is already defined in ActiveRecordTest::Foo, use generic helper instead.\n", $stderr.string
376
+ assert_match /^Instance method "state\?" is already defined in ActiveRecordTest::Foo, use generic helper instead.*\n$/, $stderr.string
377
377
  end
378
378
 
379
379
  def teardown
@@ -879,6 +879,22 @@ module ActiveRecordTest
879
879
  assert_equal self, context
880
880
  end
881
881
 
882
+ def test_should_run_after_callbacks_if_model_callback_added_prior_to_state_machine_definition
883
+ model = new_model do
884
+ after_save { nil }
885
+ end
886
+ machine = StateMachine::Machine.new(model, :initial => :parked)
887
+ machine.other_states :idling
888
+ machine.event :ignite
889
+ after_called = false
890
+ machine.after_transition {after_called = true}
891
+
892
+ record = model.new(:state => 'parked')
893
+ transition = StateMachine::Transition.new(record, machine, :ignite, :parked, :idling)
894
+ transition.perform
895
+ assert_equal true, after_called
896
+ end
897
+
882
898
  def test_should_run_around_callbacks
883
899
  before_called = false
884
900
  after_called = false
@@ -1592,6 +1608,54 @@ module ActiveRecordTest
1592
1608
  @transition.perform
1593
1609
  assert_equal [:before_ignite, [:before_save, @object]], instance.notifications
1594
1610
  end
1611
+
1612
+ def test_should_support_nil_from_states
1613
+ callbacks = [
1614
+ :before_ignite_from_nil_to_idling,
1615
+ :before_ignite_from_nil,
1616
+ :before_transition_state_from_nil_to_idling,
1617
+ :before_transition_state_from_nil
1618
+ ]
1619
+
1620
+ notified = false
1621
+ observer = new_observer(@model) do
1622
+ callbacks.each do |callback|
1623
+ define_method(callback) do |*args|
1624
+ notifications << callback
1625
+ end
1626
+ end
1627
+ end
1628
+
1629
+ instance = observer.instance
1630
+
1631
+ transition = StateMachine::Transition.new(@record, @machine, :ignite, nil, :idling)
1632
+ transition.perform
1633
+ assert_equal callbacks, instance.notifications
1634
+ end
1635
+
1636
+ def test_should_support_nil_to_states
1637
+ callbacks = [
1638
+ :before_ignite_from_parked_to_nil,
1639
+ :before_ignite_to_nil,
1640
+ :before_transition_state_from_parked_to_nil,
1641
+ :before_transition_state_to_nil
1642
+ ]
1643
+
1644
+ notified = false
1645
+ observer = new_observer(@model) do
1646
+ callbacks.each do |callback|
1647
+ define_method(callback) do |*args|
1648
+ notifications << callback
1649
+ end
1650
+ end
1651
+ end
1652
+
1653
+ instance = observer.instance
1654
+
1655
+ transition = StateMachine::Transition.new(@record, @machine, :ignite, :parked, nil)
1656
+ transition.perform
1657
+ assert_equal callbacks, instance.notifications
1658
+ end
1595
1659
  end
1596
1660
 
1597
1661
  class MachineWithNamespacedObserversTest < BaseTestCase
@@ -1762,6 +1826,13 @@ module ActiveRecordTest
1762
1826
  assert_equal [parked, idling], @model.with_states(:parked, :idling).find(:all)
1763
1827
  end
1764
1828
 
1829
+ def test_should_allow_lookup_by_string_name
1830
+ parked = @model.create :state => 'parked'
1831
+ idling = @model.create :state => 'idling'
1832
+
1833
+ assert_equal [parked, idling], @model.with_states('parked', 'idling').find(:all)
1834
+ end
1835
+
1765
1836
  def test_should_create_singular_without_scope
1766
1837
  assert @model.respond_to?(:without_state)
1767
1838
  end
@@ -1955,6 +2026,17 @@ module ActiveRecordTest
1955
2026
  assert_equal 'shutdown', machine.state(:parked).human_name
1956
2027
  end
1957
2028
 
2029
+ def test_should_allow_customized_state_key_scoped_to_class
2030
+ I18n.backend.store_translations(:en, {
2031
+ :activerecord => {:state_machines => {:'active_record_test/foo' => {:states => {:parked => 'shutdown'}}}}
2032
+ })
2033
+
2034
+ machine = StateMachine::Machine.new(@model)
2035
+ machine.state :parked
2036
+
2037
+ assert_equal 'shutdown', machine.state(:parked).human_name
2038
+ end
2039
+
1958
2040
  def test_should_allow_customized_state_key_scoped_to_machine
1959
2041
  I18n.backend.store_translations(:en, {
1960
2042
  :activerecord => {:state_machines => {:state => {:states => {:parked => 'shutdown'}}}}
@@ -1977,6 +2059,16 @@ module ActiveRecordTest
1977
2059
  assert_equal 'shutdown', machine.state(:parked).human_name
1978
2060
  end
1979
2061
 
2062
+ def test_should_support_nil_state_key
2063
+ I18n.backend.store_translations(:en, {
2064
+ :activerecord => {:state_machines => {:states => {:nil => 'empty'}}}
2065
+ })
2066
+
2067
+ machine = StateMachine::Machine.new(@model)
2068
+
2069
+ assert_equal 'empty', machine.state(nil).human_name
2070
+ end
2071
+
1980
2072
  def test_should_allow_customized_event_key_scoped_to_class_and_machine
1981
2073
  I18n.backend.store_translations(:en, {
1982
2074
  :activerecord => {:state_machines => {:'active_record_test/foo' => {:state => {:events => {:park => 'stop'}}}}}
@@ -1988,6 +2080,17 @@ module ActiveRecordTest
1988
2080
  assert_equal 'stop', machine.event(:park).human_name
1989
2081
  end
1990
2082
 
2083
+ def test_should_allow_customized_event_key_scoped_to_class
2084
+ I18n.backend.store_translations(:en, {
2085
+ :activerecord => {:state_machines => {:'active_record_test/foo' => {:events => {:park => 'stop'}}}}
2086
+ })
2087
+
2088
+ machine = StateMachine::Machine.new(@model)
2089
+ machine.event :park
2090
+
2091
+ assert_equal 'stop', machine.event(:park).human_name
2092
+ end
2093
+
1991
2094
  def test_should_allow_customized_event_key_scoped_to_machine
1992
2095
  I18n.backend.store_translations(:en, {
1993
2096
  :activerecord => {:state_machines => {:state => {:events => {:park => 'stop'}}}}
@@ -17,16 +17,20 @@ module DataMapperTest
17
17
  def default_test
18
18
  end
19
19
 
20
+ def teardown
21
+ @resources.uniq.each {|resource| DataMapperTest.send(:remove_const, resource)} if @resources
22
+ end
23
+
20
24
  protected
21
25
  # Creates a new DataMapper resource (and the associated table)
22
26
  def new_resource(create_table = :foo, &block)
23
27
  table_name = create_table || :foo
28
+ name = table_name.to_s.capitalize
24
29
 
25
- resource = Class.new do
26
- (class << self; self; end).class_eval do
27
- define_method(:name) { "DataMapperTest::#{table_name.to_s.capitalize}" }
28
- end
29
- end
30
+ resource = Class.new
31
+ DataMapperTest.send(:remove_const, name) if DataMapperTest.const_defined?(name)
32
+ DataMapperTest.const_set(name, resource)
33
+ (@resources ||= []) << name
30
34
 
31
35
  resource.class_eval do
32
36
  include DataMapper::Resource
@@ -35,10 +39,9 @@ module DataMapperTest
35
39
 
36
40
  property :id, resource.class_eval('Serial')
37
41
  property :state, String
38
-
39
- auto_migrate! if create_table
40
42
  end
41
43
  resource.class_eval(&block) if block_given?
44
+ resource.auto_migrate! if create_table
42
45
  resource
43
46
  end
44
47
 
@@ -295,7 +298,6 @@ module DataMapperTest
295
298
  def setup
296
299
  @resource = new_resource do
297
300
  property :status, String, :default => 'idling'
298
- auto_migrate!
299
301
  end
300
302
  @machine = StateMachine::Machine.new(@resource, :status, :initial => :parked)
301
303
  @record = @resource.new
@@ -335,7 +337,7 @@ module DataMapperTest
335
337
  @machine = StateMachine::Machine.new(@resource)
336
338
  @machine.state :state
337
339
 
338
- assert_equal "Instance method \"state?\" is already defined in DataMapperTest::Foo :state instance helpers, use generic helper instead.\n", $stderr.string
340
+ assert_match /^Instance method "state\?" is already defined in DataMapperTest::Foo :state instance helpers, use generic helper instead.*\n$/, $stderr.string
339
341
  end
340
342
 
341
343
  def test_should_not_output_warning_with_same_machine_name
@@ -347,6 +349,7 @@ module DataMapperTest
347
349
 
348
350
  def teardown
349
351
  $stderr = @original_stderr
352
+ super
350
353
  end
351
354
  end
352
355
 
@@ -485,11 +488,14 @@ module DataMapperTest
485
488
 
486
489
  if Gem::Version.new(::DataMapper::VERSION) >= Gem::Version.new('0.9.8')
487
490
  def test_should_raise_exception_if_protected
488
- @resource.class_eval do
491
+ resource = new_resource do
489
492
  protected :state=
490
493
  end
491
494
 
492
- assert_raise(ArgumentError) { @resource.new(:state => 'idling') }
495
+ machine = StateMachine::Machine.new(resource, :initial => :parked)
496
+ machine.state :idling
497
+
498
+ assert_raise(ArgumentError) { resource.new(:state => 'idling') }
493
499
  end
494
500
  end
495
501
  end
@@ -498,7 +504,6 @@ module DataMapperTest
498
504
  def setup
499
505
  @resource = new_resource do
500
506
  property :status, String
501
- auto_migrate!
502
507
  end
503
508
  @state_machine = StateMachine::Machine.new(@resource, :initial => :parked)
504
509
  @status_machine = StateMachine::Machine.new(@resource, :status, :initial => :idling)
@@ -515,7 +520,6 @@ module DataMapperTest
515
520
  def setup
516
521
  @resource = new_resource do
517
522
  property :updated_at, DateTime
518
- auto_migrate!
519
523
 
520
524
  # Simulate dm-timestamps
521
525
  before :update do
@@ -612,7 +616,6 @@ module DataMapperTest
612
616
  def setup
613
617
  @resource = new_resource do
614
618
  property :status, String, :default => 'idling'
615
- auto_migrate!
616
619
  end
617
620
  @machine = StateMachine::Machine.new(@resource, :status, :initial => :parked)
618
621
  @machine.event :ignite
@@ -652,7 +655,6 @@ module DataMapperTest
652
655
  def setup
653
656
  @resource = new_resource do
654
657
  property :status, String, :default => 'idling'
655
- auto_migrate!
656
658
  end
657
659
  @machine = StateMachine::Machine.new(@resource, :status, :initial => :parked)
658
660
  @machine.event :park
@@ -1812,6 +1814,13 @@ module DataMapperTest
1812
1814
  assert_equal [parked, idling], @resource.with_states(:parked, :idling)
1813
1815
  end
1814
1816
 
1817
+ def test_should_allow_lookup_by_string_name
1818
+ parked = @resource.create :state => 'parked'
1819
+ idling = @resource.create :state => 'idling'
1820
+
1821
+ assert_equal [parked, idling], @resource.with_states('parked', 'idling')
1822
+ end
1823
+
1815
1824
  def test_should_create_singular_without_scope
1816
1825
  assert @resource.respond_to?(:without_state)
1817
1826
  end
@@ -1849,6 +1858,9 @@ module DataMapperTest
1849
1858
  @machine = StateMachine::Machine.new(@resource, :state)
1850
1859
 
1851
1860
  @subclass = Class.new(@resource)
1861
+ DataMapperTest.const_set('Bar', @subclass)
1862
+ @resources << 'Bar'
1863
+ @subclass.auto_migrate!
1852
1864
  @subclass_machine = @subclass.state_machine(:state) {}
1853
1865
  @subclass_machine.state :parked, :idling, :first_gear
1854
1866
  end
@@ -1887,15 +1899,12 @@ module DataMapperTest
1887
1899
  class MachineWithScopesAndJoinsTest < BaseTestCase
1888
1900
  def setup
1889
1901
  @company = new_resource(:company)
1890
- DataMapperTest.const_set('Company', @company)
1891
1902
 
1892
1903
  @vehicle = new_resource(:vehicle) do
1893
1904
  property :company_id, Integer
1894
- auto_migrate!
1895
1905
 
1896
1906
  belongs_to :company
1897
1907
  end
1898
- DataMapperTest.const_set('Vehicle', @vehicle)
1899
1908
 
1900
1909
  @company_machine = StateMachine::Machine.new(@company, :initial => :active)
1901
1910
  @vehicle_machine = StateMachine::Machine.new(@vehicle, :initial => :parked)
@@ -1912,12 +1921,5 @@ module DataMapperTest
1912
1921
  def test_should_find_records_in_without_scope
1913
1922
  assert_equal [@mustang], @vehicle.without_states(:idling).all(Vehicle.company.state => 'active')
1914
1923
  end
1915
-
1916
- def teardown
1917
- DataMapperTest.class_eval do
1918
- remove_const('Vehicle')
1919
- remove_const('Company')
1920
- end
1921
- end
1922
1924
  end
1923
1925
  end
@@ -295,14 +295,14 @@ module MongoMapperTest
295
295
  @machine = StateMachine::Machine.new(@model)
296
296
  @machine.state :state
297
297
 
298
- assert_match /^Instance method "state\?" is already defined in .*, use generic helper instead\.\n$/, $stderr.string
298
+ assert_match /^Instance method "state\?" is already defined in .*, use generic helper instead.*\n$/, $stderr.string
299
299
  end
300
300
 
301
301
  def test_should_output_warning_with_same_machine_attribute
302
302
  @machine = StateMachine::Machine.new(@model, :public_state, :attribute => :state)
303
303
  @machine.state :state
304
304
 
305
- assert_match /^Instance method "state\?" is already defined in .*, use generic helper instead\.\n$/, $stderr.string
305
+ assert_match /^Instance method "state\?" is already defined in .*, use generic helper instead.*\n$/, $stderr.string
306
306
  end
307
307
 
308
308
  def teardown
@@ -754,6 +754,22 @@ module MongoMapperTest
754
754
  assert_equal self, context
755
755
  end
756
756
 
757
+ def test_should_run_after_callbacks_if_model_callback_added_prior_to_state_machine_definition
758
+ model = new_model do
759
+ after_save { nil }
760
+ end
761
+ machine = StateMachine::Machine.new(model, :initial => :parked)
762
+ machine.other_states :idling
763
+ machine.event :ignite
764
+ after_called = false
765
+ machine.after_transition {after_called = true}
766
+
767
+ record = model.new(:state => 'parked')
768
+ transition = StateMachine::Transition.new(record, machine, :ignite, :parked, :idling)
769
+ transition.perform
770
+ assert_equal true, after_called
771
+ end
772
+
757
773
  def test_should_run_around_callbacks
758
774
  before_called = false
759
775
  after_called = false
@@ -814,23 +830,7 @@ module MongoMapperTest
814
830
  @result = @transition.perform
815
831
  end
816
832
 
817
- if defined?(MongoMapper::Version) && MongoMapper::Version >= '0.9.0'
818
- def test_should_not_be_successful
819
- assert !@result
820
- end
821
-
822
- def test_should_not_change_current_state
823
- assert_equal 'parked', @record.state
824
- end
825
-
826
- def test_should_not_run_action
827
- assert @record.new_record?
828
- end
829
-
830
- def test_should_not_run_further_callbacks
831
- assert_equal [:before_1], @callbacks
832
- end
833
- else
833
+ if !defined?(MongoMapper::Version) || MongoMapper::Version =~ /^0\.[5-8]\./
834
834
  def test_should_be_successful
835
835
  assert @result
836
836
  end
@@ -846,6 +846,22 @@ module MongoMapperTest
846
846
  def test_should_run_further_callbacks
847
847
  assert_equal [:before_1, :before_2, :around_before, :around_after, :after], @callbacks
848
848
  end
849
+ else
850
+ def test_should_not_be_successful
851
+ assert !@result
852
+ end
853
+
854
+ def test_should_not_change_current_state
855
+ assert_equal 'parked', @record.state
856
+ end
857
+
858
+ def test_should_not_run_action
859
+ assert @record.new_record?
860
+ end
861
+
862
+ def test_should_not_run_further_callbacks
863
+ assert_equal [:before_1], @callbacks
864
+ end
849
865
  end
850
866
  end
851
867
 
@@ -916,14 +932,14 @@ module MongoMapperTest
916
932
  assert !@record.new_record?
917
933
  end
918
934
 
919
- if defined?(MongoMapper::Version) && MongoMapper::Version >= '0.9.0'
920
- def test_should_not_run_further_after_callbacks
921
- assert_equal [:around_before, :around_after, :after_1], @callbacks
922
- end
923
- else
935
+ if !defined?(MongoMapper::Version) || MongoMapper::Version =~ /^0\.[5-8]\./
924
936
  def test_should_still_run_further_after_callbacks
925
937
  assert_equal [:around_before, :around_after, :after_1, :after_2], @callbacks
926
938
  end
939
+ else
940
+ def test_should_not_run_further_after_callbacks
941
+ assert_equal [:around_before, :around_after, :after_1], @callbacks
942
+ end
927
943
  end
928
944
  end
929
945
 
@@ -1405,6 +1421,13 @@ module MongoMapperTest
1405
1421
  assert_equal [parked, idling], @model.with_states(:parked, :idling).to_a
1406
1422
  end
1407
1423
 
1424
+ def test_should_allow_lookup_by_string_name
1425
+ parked = @model.create :state => 'parked'
1426
+ idling = @model.create :state => 'idling'
1427
+
1428
+ assert_equal [parked, idling], @model.with_states('parked', 'idling').to_a
1429
+ end
1430
+
1408
1431
  def test_should_create_singular_without_scope
1409
1432
  assert @model.respond_to?(:without_state)
1410
1433
  end
@@ -1428,7 +1451,7 @@ module MongoMapperTest
1428
1451
  assert_equal [parked, idling], @model.without_states(:first_gear).to_a
1429
1452
  end
1430
1453
 
1431
- if defined?(MongoMapper::Version) && MongoMapper::Version >= '0.8.0'
1454
+ if defined?(MongoMapper::Version) && !(MongoMapper::Version =~ /^0\.[5-7]\./)
1432
1455
  def test_should_allow_chaining_scopes
1433
1456
  parked = @model.create :state => 'parked'
1434
1457
  idling = @model.create :state => 'idling'
@@ -1541,6 +1564,17 @@ module MongoMapperTest
1541
1564
  assert_equal 'shutdown', machine.state(:parked).human_name
1542
1565
  end
1543
1566
 
1567
+ def test_should_allow_customized_state_key_scoped_to_class
1568
+ I18n.backend.store_translations(:en, {
1569
+ :mongo_mapper => {:state_machines => {:'mongo_mapper_test/foo' => {:states => {:parked => 'shutdown'}}}}
1570
+ })
1571
+
1572
+ machine = StateMachine::Machine.new(@model)
1573
+ machine.state :parked
1574
+
1575
+ assert_equal 'shutdown', machine.state(:parked).human_name
1576
+ end
1577
+
1544
1578
  def test_should_allow_customized_state_key_scoped_to_machine
1545
1579
  I18n.backend.store_translations(:en, {
1546
1580
  :mongo_mapper => {:state_machines => {:state => {:states => {:parked => 'shutdown'}}}}
@@ -1563,6 +1597,16 @@ module MongoMapperTest
1563
1597
  assert_equal 'shutdown', machine.state(:parked).human_name
1564
1598
  end
1565
1599
 
1600
+ def test_should_support_nil_state_key
1601
+ I18n.backend.store_translations(:en, {
1602
+ :mongo_mapper => {:state_machines => {:states => {:nil => 'empty'}}}
1603
+ })
1604
+
1605
+ machine = StateMachine::Machine.new(@model)
1606
+
1607
+ assert_equal 'empty', machine.state(nil).human_name
1608
+ end
1609
+
1566
1610
  def test_should_allow_customized_event_key_scoped_to_class_and_machine
1567
1611
  I18n.backend.store_translations(:en, {
1568
1612
  :mongo_mapper => {:state_machines => {:'mongo_mapper_test/foo' => {:state => {:events => {:park => 'stop'}}}}}
@@ -1574,6 +1618,17 @@ module MongoMapperTest
1574
1618
  assert_equal 'stop', machine.event(:park).human_name
1575
1619
  end
1576
1620
 
1621
+ def test_should_allow_customized_event_key_scoped_to_class
1622
+ I18n.backend.store_translations(:en, {
1623
+ :mongo_mapper => {:state_machines => {:'mongo_mapper_test/foo' => {:events => {:park => 'stop'}}}}
1624
+ })
1625
+
1626
+ machine = StateMachine::Machine.new(@model)
1627
+ machine.event :park
1628
+
1629
+ assert_equal 'stop', machine.event(:park).human_name
1630
+ end
1631
+
1577
1632
  def test_should_allow_customized_event_key_scoped_to_machine
1578
1633
  I18n.backend.store_translations(:en, {
1579
1634
  :mongo_mapper => {:state_machines => {:state => {:events => {:park => 'stop'}}}}