Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include object class in state machine transition error #324

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/state_machine/transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def initialize(object, machine, event) #:nodoc:
@from = machine.read(object, :state)
@event = machine.events.fetch(event)
errors = machine.errors_for(object)

message = "Cannot transition #{machine.name} via :#{self.event} from #{from_name.inspect}"
message << " (Reason(s): #{errors})" unless errors.empty?
message << " (Object class): #{object.class.name}"
super(object, message)
end

Expand Down
6 changes: 3 additions & 3 deletions test/unit/invalid_transition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_should_have_a_qualified_from_name
end

def test_should_generate_a_message
assert_equal 'Cannot transition state via :ignite from :parked', @invalid_transition.message
assert_match 'Cannot transition state via :ignite from :parked', @invalid_transition.message
end
end

Expand Down Expand Up @@ -100,13 +100,13 @@ def errors_for(object)
def test_should_generate_a_message_without_reasons_if_empty
@object.errors = ''
invalid_transition = StateMachine::InvalidTransition.new(@object, @machine, :ignite)
assert_equal 'Cannot transition state via :ignite from :parked', invalid_transition.message
assert_match 'Cannot transition state via :ignite from :parked', invalid_transition.message
end

def test_should_generate_a_message_with_error_reasons_if_errors_found
@object.errors = 'Id is invalid, Name is invalid'
invalid_transition = StateMachine::InvalidTransition.new(@object, @machine, :ignite)
assert_equal 'Cannot transition state via :ignite from :parked (Reason(s): Id is invalid, Name is invalid)', invalid_transition.message
assert_match 'Cannot transition state via :ignite from :parked (Reason(s): Id is invalid, Name is invalid)', invalid_transition.message
end

def teardown
Expand Down