state_machines-activerecord icon indicating copy to clipboard operation
state_machines-activerecord copied to clipboard

enum support

Open renchap opened this issue 9 years ago • 3 comments

In an existing model, my state column is a Rails enum and using state_machine on this column generates some warnings (and probably does not work) :

Instance method "processing?" is already defined in #<Module:0x00000007615c68>, use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true.
Both Enrollment and its :state machine have defined a different default for "state". Use only one or the other for defining defaults to avoid unexpected behaviors.

I am migrating from aasm and this is a nice feature I miss.

renchap avatar Mar 25 '15 21:03 renchap

@renchap this is probably a problem for the parent project state_machines.

I think you need to simple remove your enum declaration, because state_machines is trying to generate the same methods.

state_machine requires a string state column (you use a different column) and it generates similar methods that you might be expecting from an enum. Perhaps you had to have an enum with aasm and simply don't with state_machine?

rosskevin avatar Jun 19 '15 17:06 rosskevin

@renchap, so, you have integers in your base? You can use something like:

state_machine :status, attribute: :my_integer_status_id, initial: :parked do
  state :parked, value: 0 # from enum
  state :idling, value: 1 # from enum

  event :ignite do
    transition :parked => :idling
  end
  # ...
end

korun avatar Jul 01 '15 06:07 korun

FWIW, I think this adapter could add some nice support for ActiveRecord::Enum that would allow it to coexist. In short, it could detect if there is an existing enum defined for the state machine column, and then:

  • Not complain about method conflicts
  • Either interface as strings through the ActiveRecord::Enum layer, or else detect and use the integer values directly.

This should effectively allow people to have both an enum and state machine definition for the same column. Now the questions are: Is this worth doing? Are there convincing reasons to keep the enum definition even after adding a state machine? Is the behavior of the shared methods consistent between the two?

iangreenleaf avatar Dec 30 '15 20:12 iangreenleaf