From 4a194fb12bde3d1393ca1b366cf597b2f2c8cc22 Mon Sep 17 00:00:00 2001 From: Aaron Pfeifer Date: Tue, 7 Apr 2009 21:54:37 -0400 Subject: [PATCH] Fix event attribute transitions being publicly accessible --- CHANGELOG.rdoc | 2 + lib/state_machine/machine.rb | 4 +- test/unit/machine_collection_test.rb | 77 +++++++++++----------------- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 0c6e5aae..995d98d5 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,5 +1,7 @@ == master +* Fix event attribute transitions being publicly accessible + == 0.7.1 / 2009-04-05 * Fix machines failing to generate graphs when run from Merb tasks diff --git a/lib/state_machine/machine.rb b/lib/state_machine/machine.rb index f34c4a41..2d3c026e 100644 --- a/lib/state_machine/machine.rb +++ b/lib/state_machine/machine.rb @@ -1224,7 +1224,9 @@ def define_event_helpers # Tracks the event / transition to invoke when the action is called @instance_helper_module.class_eval do attr_writer "#{attribute}_event" - attr_accessor "#{attribute}_event_transition" + + protected + attr_accessor "#{attribute}_event_transition" end # Interpret non-blank events as present diff --git a/test/unit/machine_collection_test.rb b/test/unit/machine_collection_test.rb index f6f061a3..17d0c336 100644 --- a/test/unit/machine_collection_test.rb +++ b/test/unit/machine_collection_test.rb @@ -319,10 +319,6 @@ def test_should_not_transition_state def test_should_not_reset_event_attribute assert_equal :invalid, @object.state_event end - - def test_should_not_have_event_transition - assert_nil @object.state_event_transition - end end class MachineCollectionFireImplicitWithoutTransitionTest < MachineCollectionFireImplicitTest @@ -349,10 +345,6 @@ def test_should_not_transition_state def test_should_not_reset_event_attribute assert_equal :ignite, @object.state_event end - - def test_should_not_have_event_transition - assert_nil @object.state_event_transition - end end class MachineCollectionFireImplicitWithTransitionTest < MachineCollectionFireImplicitTest @@ -360,12 +352,10 @@ def setup super @state_event = nil - @state_event_transition = nil @object.state_event = 'ignite' @result = @machines.fire_attribute_events(@object, :save) do @state_event = @object.state_event - @state_event_transition = @object.state_event_transition @saved = true end end @@ -382,10 +372,6 @@ def test_should_not_have_event_while_running_action assert_nil @state_event end - def test_should_not_have_event_transition_while_running_action - assert_nil @state_event_transition - end - def test_should_transition_state assert_equal 'idling', @object.state end @@ -394,8 +380,9 @@ def test_should_reset_event_attribute assert_nil @object.state_event end - def test_should_reset_event_transition - assert_nil @object.state_event_transition + def test_should_not_be_successful_if_fired_again + @object.state_event = 'ignite' + assert !@machines.fire_attribute_events(@object, :save) { true } end end @@ -418,10 +405,6 @@ def test_should_not_transition_state def test_should_not_reset_event_attribute assert_equal :ignite, @object.state_event end - - def test_should_not_have_event_transition - assert_nil @object.state_event_transition - end end class MachineCollectionFireImplicitWithActionErrorTest < MachineCollectionFireImplicitTest @@ -439,10 +422,6 @@ def test_should_not_transition_state def test_should_not_reset_event_attribute assert_equal :ignite, @object.state_event end - - def test_should_not_have_event_transition - assert_nil @object.state_event_transition - end end class MachineCollectionFireImplicitPartialTest < MachineCollectionFireImplicitTest @@ -455,12 +434,10 @@ def setup @machine.after_transition { @ran_after_callback = true } @state_event = nil - @state_event_transition = nil @object.state_event = 'ignite' @result = @machines.fire_attribute_events(@object, :save, false) do @state_event = @object.state_event - @state_event_transition = @object.state_event_transition true end end @@ -481,10 +458,6 @@ def test_should_not_have_event_while_running_action assert_nil @state_event end - def test_should_not_have_event_transition_while_running_action - assert_nil @state_event_transition - end - def test_should_transition_state assert_equal 'idling', @object.state end @@ -493,29 +466,50 @@ def test_should_not_reset_event_attribute assert_equal :ignite, @object.state_event end - def test_should_have_event_transition - assert_not_nil @object.state_event_transition - end - def test_should_reset_event_attributes_after_next_fire_on_success assert @machines.fire_attribute_events(@object, :save) { true } assert_equal 'idling', @object.state assert_nil @object.state_event - assert_nil @object.state_event_transition + end + + def test_should_guard_transition_after_next_fire_on_success + @machines.fire_attribute_events(@object, :save) { true } + + @object.state = 'idling' + @object.state_event = 'ignite' + assert !@machines.fire_attribute_events(@object, :save) { true } end def test_should_rollback_all_attributes_after_next_fire_on_failure assert !@machines.fire_attribute_events(@object, :save) { false } assert_equal 'parked', @object.state assert_equal :ignite, @object.state_event - assert_nil @object.state_event_transition + + @object.state = 'idling' + assert !@machines.fire_attribute_events(@object, :save) { false } + end + + def test_should_guard_transition_after_next_fire_on_failure + @machines.fire_attribute_events(@object, :save) { false } + + @object.state = 'idling' + assert !@machines.fire_attribute_events(@object, :save) { true } end def test_should_rollback_all_attributes_after_next_fire_on_error assert_raise(ArgumentError) { @machines.fire_attribute_events(@object, :save) { raise ArgumentError } } assert_equal 'parked', @object.state assert_equal :ignite, @object.state_event - assert_nil @object.state_event_transition + end + + def test_should_guard_transition_after_next_fire_on_error + begin + @machines.fire_attribute_events(@object, :save) { raise ArgumentError } + rescue ArgumentError + end + + @object.state = 'idling' + assert !@machines.fire_attribute_events(@object, :save) { true } end end @@ -547,10 +541,6 @@ def test_should_transition_state def test_should_reset_event_attribute assert_nil @object.state_event end - - def test_should_not_have_event_transition - assert_nil @object.state_event_transition - end end class MachineCollectionFireImplicitWithDifferentActionsTest < MachineCollectionFireImplicitTest @@ -613,11 +603,6 @@ def test_should_reset_all_event_attributes_for_action assert_nil @object.state_event assert_nil @object.alarm_state_event end - - def test_should_reset_all_event_transitions_for_action - assert_nil @object.state_event_transition - assert_nil @object.alarm_state_event_transition - end end class MachineCollectionFireImplicitWithValidationsTest < Test::Unit::TestCase