Acts As State Machine for Mongoid

It’s a customized plugin from github.com/omghax/acts_as_state_machine that works smoothly through Mongoid.

I only removed the Class Methods that were using the method with_scope (ActiveRecord) because it’s not supported in Mongoid (not a problem for me)

Installation

Add the gem into your Gemfile

gem "mongoid_state_machine", :require => "mongoid/state_machine"

And run ‘bundle install`

Example

class Order
  include Mongoid::Document

  include Mongoid::StateMachine

  state_machine :initial => :opened

  state :opened
  state :closed, :enter => Proc.new {|o| Mailer.send_notice(o)}
  state :returned

  event :close do
    transitions :to => :closed, :from => :opened
  end

  event :return do
    transitions :to => :returned, :from => :closed
  end
end

o = Order.create
o.close! # notice is sent by mailer
o.return!

Acknowledge

This project was originally developed by Scott Barron

License

Copyright © 2010 Bruno Azisaka Maciel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.