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

Illegal state transitions #240

Open
aThorp96 opened this issue Mar 5, 2024 · 0 comments
Open

Illegal state transitions #240

aThorp96 opened this issue Mar 5, 2024 · 0 comments

Comments

@aThorp96
Copy link

aThorp96 commented Mar 5, 2024

When changing a transition using event input as is done in the discard event method, it's possible to get into illegal state transitions when not halting the current transition in the right place. See the below example. This appears to be because when you call an event like keep! it resets @halted to false, so the after exit the halted workflow state transition is persisted. The workaround is to call halt after you call keep!, but it feels like the order there shouldn't affect things, and certainly that it shouldn't be possible to go from :trash to :fridge.

require 'workflow'

class Page
  include Workflow

  def workflow_state
    @workflow_state
  end

  workflow do
    state :new do
      event :draw_on_page, :transitions_to => :drawing
    end

    state :drawing do
      event :discard, :transitions_to => :trash
      event :keep, :transitions_to => :fridge
    end

    state :trash

    state :fridge

    on_transition do |from, to, triggering_event, *event_args|
      puts "#{workflow_state} -> #{to}"
    end
  end

  def discard(good_drawing)
    if good_drawing
      halt
      keep!
    end
  end

  def keep
    puts "so happy this drawing isn't going into the trash"
  end
end

page = Page.new
page.draw_on_page!
page.discard!(true)
puts "Paper's final resting place: #{page.workflow_state}"
athorp$ bundle exec ruby repro.rb
 -> drawing
so happy this drawing isn't going into the trash
drawing -> fridge
fridge -> trash
Paper's final resting place: trash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant