I’d like to ask for an opinion about state machine (aasm gem). My main domain model was full of event and transition definitions along with callbacks that occur when the state changes and guards that check a transition can occur. Would you recommend extracting this into a module or is there an even better way?
I was thinking of extracting a couple of concerns but I got concerned ( ) because these concerns wouldn’t actually include shared behaviour but would be specific to each model. It looks like Module is somewhat closer to what I need. I could still extend the module with ActiveSupport::Concern to take advantage of included, ClassMethods etc.
Here’s an example of what I’m talking about
# app/models/order.rb class Order < ActiveRecord::Base include StateMachine ... end # app/models/order/state_machine.rb module Order::StateMachine extend ActiveSupport::Concern included do include AASM aasm do state :incomplete, initial: true state :placed state :canceled state :completed event :place do transitions from: :incomplete, to: :placed end event :complete, before: :finalize_order do transitions from: :placed, to: :completed, guard: :valid_payment? end event :cancel do transitions from: :placed, to: :canceled end end end def finalize_order ... end def valid_payment? ... end end
What do you think? How would you tackle such problem?