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

enum support #13

Open
renchap opened this issue Mar 25, 2015 · 3 comments
Open

enum support #13

renchap opened this issue Mar 25, 2015 · 3 comments

Comments

@renchap
Copy link

renchap commented Mar 25, 2015

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.

@rosskevin
Copy link
Member

@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?

@korun
Copy link

korun commented Jul 1, 2015

@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

@iangreenleaf
Copy link

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?

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

4 participants