Which State Machine plugin do you recommend for Rails? [closed]

孤人 提交于 2019-12-21 17:25:45

问题


I am looking for a relatively simple state machine plugin for a Rails 3 / Active Record project.

I've done a little research and come up with the following plugins:

  • Transitions: https://github.com/qoobaa/transitions
    • Extracted from old Active Record State Machine Library
  • Acts_As_State_Machine: https://github.com/aasm/aasm
  • Workflow: https://github.com/geekq/workflow
  • State Machine: https://github.com/pluginaweek/state_machine
  • State Flow: https://github.com/ryanza/stateflow

But they all seem very similar, so I am curious to know if anyone has had real-world experience with any of them.

Thanks!


回答1:


state_machine seems to be the one people say to use, at least who I've spoken to. It is environment agnostic, so you don't have to use one state machine in one part of your app, and a completely different one in another part of your app.

Update February 2015

State Machine is unfortunately no longer maintained and there are many issues, which makes it a less favourable choice. However, this fork of the project is actively maintained and appears to be stable.




回答2:


I ended up using stateflow and like it. https://github.com/ryanza/stateflow

It runs with rails 3.0, and it is similar in approach to the state machine code that was in several of the rails 3.0 betas but dropped from the final release. I've not followed along to see what the current thinking about having a state machine inside rails is - but I guess that if a state machine gets reintegrated in a future release it will be a bit like this. Hopefully that will mean minimal code changes if I ever want to drop the gem and use core functionality




回答3:


I would approach Transitions with care. The author/extractor says he doesn't have time to maintain it, and usually favors state_machine on his new projects.

On the other hand, it does work (although check the open issues to see if it will work in your case). I'm using Transitions myself on a project, but for a trivial state machine.

I've used acts as state machine before, on a Rails 2 project, and it did really well (even with very complex state machines)




回答4:


SimpleStateMachine is a simple DSL to decorate existing methods with state transition guards.

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

It works with ActiveModel validations and allows events to be called with arguments:

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

It can rescue exception:

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



回答5:


Even the simplest state machine gems had way more features than I needed, so I just decided to roll my own solution. I tried Transitions and Stateflow, but had minor issues with both.




回答6:


I would recommend Workflow as i feel its the easiest of all the state machines available .



来源:https://stackoverflow.com/questions/6487285/which-state-machine-plugin-do-you-recommend-for-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!