aasm
aasm copied to clipboard
reflect state from rails enum
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
if must, set initial state
It's been some time now but is this a bug?
No, its not a bug. Its a feature request.
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?
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