Zyph Martin Design Studios Search


Zyph Martin Design Studios Recent Entries

TELLING IT LIKE IT IS.

Posted on January 26 2012


COMING SOON TO A BROWSER NEAR YOU!

Posted on December 01 2011


SAY GOODBYE TO FLASH...

Posted on June 09 2011


LOGO PRICES JUST WENT UP.

Posted on June 03 2011


LOUD HOUSE

Posted on April 15 2011


VIEW ARCHIVES -->


RAILS 3, MONGOID, AND STATE MACHINE



Mongoid_statemachine

[UPDATE] Ryan the author of stateflow has updated the library to work with Rails 2.3.x and Rails 3 you no longer need to modify anything to get it working with Rails 3.

 

It is amazing that we have had a blog for about 9 months and I have only posted 1 blog post. I am going to try and make a point to change that and start posting some more about what I am working on, what goodies I am using, and anything that might be useful to someone else or myself later down the road. So Rails 3 is out and it is great. I upgraded our site to a release candidate of it a week or so ago and everything went well with that, but I will talk about that in another post. Along with upgrading our site to Rails 3 I also changed from MongoMapper to MongoID. I think that they are both great libraries, but there were some things that just flowed better for me personally with MongoID. 

 

Right now I am working on a project that I have a need for a state machine library. I googled and found stateflow. Stateflow supports MongoID as a persistence layer. I only needed to modify the persistence file for MongoID to adjust the callback for Rails 3 since the :before_validation_on_create has changed

#[UPDATED] Removed because you know longer need to do modify anything. Ryan has updated the library.

So far in my small initial tests all seems to be working fine for me. I will post back if I run into anything that isn't working for me.

Stateflow.persistence = :mongoid

class Light
  include Mongoid::Document
  include Stateflow
  
  field :state
  
  stateflow do
    state_column :state
    initial :green
    
    state :green
    state :yellow do
      exit :catch_runners
    end
    state :red
    
    event :change_color do
      transitions :from => :green, :to => :yellow
      transitions :from => :yellow, :to => :red
      transitions :from => :red, :to => :green
    end
  end
  
  def catch_runners
    puts "That'll be $250."
  end
  
end

light = Light.new
light.current_state    # :green
light.change_color!    # true
light.current_state    # :yellow
light.change_color!    # true
"That'll be $250."
light.current_state    # :red


Back To Blog - Posted on August 31 2010 by Brandon Martin





Ryan
Posted over 1 year ago
Hey Brandon, thanks for the plug; I wrote stateflow and plan on making a few updates to it. I've been meaning to patch it for Rails 3, and never actually got around to it. Awesome thanks!




Ryan
Posted over 1 year ago
Ok cool, pushed up a new copy that supports Rails 2.3.x and Rails 3 :)




Leave a comment: