geekq / workflow

Ruby finite-state-machine-inspired API for modeling workflow
MIT License
1.75k stars 207 forks source link

Callback on entering initial state #233

Open penguoir opened 2 years ago

penguoir commented 2 years ago

I'd like to run a callback when an object enters its initial state. For example:

class Article
  include Workflow

  workflow do
    state :new
  end

  def on_new_entry
    puts 'entered new'
  end
end

Currently, workflow doesn't run the on_new_entry callback. Is this expected? If so, what's the best practice for running a callback on entering of an initial state?

tlloydthwaites commented 7 months ago

Looking at https://github.com/geekq/workflow/blob/ba6946ba6711e2d255cc2fd5d28e6af3add3df36/lib/workflow.rb#L97, on entry is not called for the initial action, only when transitioning from one action to another.

I'd suggest defining a method with the state name, but that's tricky with an event called 'new'. You could always call it something else, like 'initial', and then you could have an initial method?