state-machines / state_machines

Adds support for creating state machines for attributes on any Ruby class
https://github.com/state-machines/state_machines
MIT License
814 stars 91 forks source link

Ability to dynamically set state #89

Closed dylan-hoefsloot closed 1 year ago

dylan-hoefsloot commented 1 year ago

I would like to do something like this

class Project
  attr_accessor :field

  state_machine :state do
    event :dynamic do |project|
      transition to: project.calculate_statue
    end
  end

  def calculate_state
    case field do
    when "something"
      :rejected
    when "otherthing"
      :approved
    end
  end
end

where the state being transitioned to can be dynamically calculated via information that is stored on the class containing the state machine, similar to how the do: block works in after_transition.

Is this possible? or is this something that you would consider adding?

seuros commented 1 year ago

Possible, the example is on the readme