Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix event attribute transitions being publicly accessible
  • Loading branch information
obrie committed Apr 8, 2009
1 parent 883805e commit 4a194fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 47 deletions.
2 changes: 2 additions & 0 deletions 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
Expand Down
4 changes: 3 additions & 1 deletion lib/state_machine/machine.rb
Expand Up @@ -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
Expand Down
77 changes: 31 additions & 46 deletions test/unit/machine_collection_test.rb
Expand Up @@ -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
Expand All @@ -349,23 +345,17 @@ 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
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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4a194fb

Please sign in to comment.