Which State Machine plugin do you recommend for Rails?

I am looking for a relatively simple public computer plugin for the Rails 3 / Active Record project.

I did a little research and came up with the following plugins:

But they all seem very similar, so I'm curious to find out if anyone had experience in the real world with any of them.

Thank!

+5
source share
6 answers

state_machine seems to be the one people are talking to, at least who I talked to. It doesn’t depend on the environment, so you don’t need to use one state machine in one part of the application and completely different in another part of your application.

Update February 2015

The state machine, unfortunately, is no longer supported, and there are many problems, which makes it a less favorable choice. However, this project plug is actively supported and looks stable.

+12
source

stateflow . https://github.com/ryanza/stateflow

3.0, , rails 3.0, . , , - , , . , , -

+4

Transitions. / , state_machine .

, ( , , ). Transitions , .

, Rails 2, ( )

+3

SimpleStateMachine - DSL .

class LampSwitch
   extend SimpleStateMachine

   def initialize
     self.state = 'off'
   end

   event :push_switch, :off => :on
end

lamp = LampSwitch.new
lamp.state          # => 'off'
lamp.off?           # => true
lamp.push_switch    #
lamp.state          # => 'on'
lamp.on?            # => true

ActiveModel :

class User < ActiveRecord::Base
  ...
  def activate_account(activation_code)
    if activation_code_invalid?(activation_code)
      errors.add(:activation_code, 'Invalid')
    end
  end
  event :activate_account, :invited => :activated
end

user = User.new
user.activate_account!('INVALID') # => raises ActiveRecord::RecordInvalid
user.activated?                     # => false
user.activate_account!('VALID')
user.activated?                     # => true

:

def download_data
  raise Service::ConnectionError
end
event :download_data, Service::ConnectionError => :download_failed

user.download_data               # catches Service::ConnectionError
user.state                       # => "download_failed"
user.state_machine.raised_error  # the raised error
+2

Even the simplest jewelry from the state apparatus had more opportunities than I needed, so I just decided to launch my own solution. I tried Transitions and Stateflow, but had minor issues with both.

0
source

I would recommend Workflow , as I consider it the easiest of all available state machines.

0
source

All Articles