state_machine 0.9.0: Locked and loaded

by aaron

I‘ve decided the only way to go viral is to get a cat to use state_machine. I’m just having a hard time getting past that whole “cats can’t program” thing… so blogging it is! Last week, state_machine 0.9.0 was tagged after a month’s worth of active development. This is a major milestone that I’m really excited to share with all of you.

If you can dodge a bullet, you can dodge a ball. Wait, what?

If you’ve been following the project for a while, you’ll know that I only bump major versions when there are significant changes in either the public API or the feature set. Fortunately, this release comes loaded with a bunch of new features (and some fixes) that I hope you’ll all enjoy:

  • MongoMapper 0.5.5+ integration support
  • ActiveModel 3.0.0+ integration support (extracted from ActiveRecord)
  • DataMapper 0.10.3+ support
  • around_transition callbacks (supplements before/after callbacks)
  • Rails 3 railtie integration
  • Consolidated rake tasks
  • Unified API for event / attribute transitions
  • …and 7 bug fixes

To me, the most interesting part of this release has been building support for around_transition callbacks. Here’s a basic example of its usage:

class Vehicle < ActiveRecord::Base
  state_machine :initial => :parked do
    around_transition :on => :ignite, :do => :benchmark
    event :ignite do
      transition :parked => :first_gear
    end
  end
 
  def benchmark
    puts "...starting benchmark"
    time = Benchmark.measure { yield }
    puts "...completed in: #{time.real}"
  end
end
 
v = Vehicle.new
v.ignite
# ...starting benchmark
# ...completed in: 0.0642118453979492
v.state # => "idling"
 
v = Vehicle.new(:state_event => 'ignite')
v.valid?
# ...starting benchmark
v.state # => "idling"
sleep 1
v.save
# ...completed in: 1.00799298286438

See the full list of changes here.

I can only show you the door… unfortunately, it’s locked.

Besides addition of the above features, 0.9.0 is also a significant milestone as it marks the beginning of feature lockdown on state_machine. All future changes will be primarily for maintenance means (fixing bugs, compatibility with releases of integrated ORMs, etc.). This is the final push to get to an official 1.0.0 release.

However, I need your help to get to 1.0.0. If you encounter bugs in any part of the code or see any issues with the documentation, now is the time to report them.

Otherwise, enjoy this release :)

Comments

7 Responses to “state_machine 0.9.0: Locked and loaded”

  1. Jay Sanders ~ Mindtonic Media on April 19th, 2010 9:27 am

    Thanks for all your hard work! This plugin is truly beneficial. I use it regularly in my apps and am grateful for both the breadth of features and thoughtfulness of design!

  2. aaron on April 20th, 2010 8:41 am

    Jay, Thanks for the kind words :)

  3. S. M. Sohan on April 20th, 2010 9:03 am

    Your post has been tagged at the drink rails blog.

  4. Robby Colvin on April 21st, 2010 12:23 am

    Any chance for Mongoid support?

  5. aaron on April 21st, 2010 6:50 am

    Robby,

    I think I saw someone build an integration for Mongoid in a fork a month or so ago. I don’t plan on building anymore integrations. If anything, I think what I’d like to see is the integrations become separate projects in the same way that database adapters for ORMs like DataMapper have become separate projects.

    If that were to happen, then getting a Mongoid integration really comes down to the Mongoid community building and maintaining that integration. To be honest, there should be nothing preventing that from happening now (and it could be a good first step).

  6. Roland on June 13th, 2010 8:36 am

    I love your state_machine, it’s imho by far the best one in the Ruby world.

    But I’m still in trouble regarding conditional validations, AR models and state_machine.

    E.g. I’ve a multistep model that requires different validations on different states.

    What’s the best approach to do this? Would you mind to provide a sample?

    thank you!

  7. aaron on June 13th, 2010 9:03 pm

    Roland -

    Glad you like the library! This is a question that’s better off on the mailing list, but perhaps you’re looking for something like this? http://gist.github.com/437137

    Ping me on github or e-mail if you were thinking of something different.