aasm icon indicating copy to clipboard operation
aasm copied to clipboard

reflect state from rails enum

Open pynixwang opened this issue 8 years ago • 5 comments

when use enum: true option, bind all enum item to state automatically, do not repeat you self.

enum state: {
    sleeping: 5,
    running: 99
  }

  aasm :column => :state, :enum => true do
-    state :sleeping, :initial => true
-    state :running
  end

pynixwang avatar Oct 11 '17 12:10 pynixwang

if must, set initial state

pynixwang avatar Oct 11 '17 12:10 pynixwang

It's been some time now but is this a bug?

drwl avatar Mar 06 '19 21:03 drwl

No, its not a bug. Its a feature request.

anilmaurya avatar Mar 07 '19 03:03 anilmaurya

I was very confused by the docs, and thought this was supported.

In the latter case AASM will try to use pluralized column name to access possible enum states.

This led me to think that AASM will turn each enum entry into a state, automagically. That appears to not be the case.

Here's my snippet that unrolls the enum, and appears (so far) to work:

aaas column: :state do
  Transfer.states.each { |s, _| state s.to_sym }
end

Annoyingly, it does seem to override the Rails-generated functions:

WARN -- : MyModel: overriding method 'sleeping?'!
WARN -- : MyModel: overriding method 'running?'!

Thinking about if I were to dive in and try to make a PR... does AASM need it's version of those functions, or are they functionally identical to the ones Rails makes?

rangerscience avatar Feb 04 '23 03:02 rangerscience

There's a Rails update that should let you resolve the warnings: https://rubyonrails.org/2022/11/25/this-week-in-rails-disable-enum-methods-generation-a-concurrency-fix-and-more-5e5cc7ab

rangerscience avatar Feb 06 '23 22:02 rangerscience