Small State_Machine Helper

Some small helpers to

  • access all states
  • access all events
  • define all status? methods

for rails state_machine plugin (for former acts_as_state_machine).

(this will not work if you got multiple status fields).

Usage

movie.online?
movie.banned?
...
f.select(:status,Movie.states)

Install

#lib/status_helper.rb
module StatusHelper
  def self.included(base)
    base.class_eval do
      def self.states; state_machines['status'].states; end
      def self.events; state_machines['status'].events.keys; end
      states.each {|state| define_method("#{state}?"){status == state} }
    end
  end
end

#your model
class Movie
  include StatusHelpers
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s