The missing status/enum field helpers for ActiveRecord.
sudo gem install grimen-has_status
In your model:
class Post < ActiveRecord:Base has_status :publish_status, [:draft, :published, :some_status], :default => :published has_status :priority, [:important, :no_priority], :labels => {:important => 'Important!', :no_priority => I18n.t('statuses.priority.low')} end
…gives:
@post.draft? # => @post == :draft, @post == 'draft' @post.draft! # => @post = :draft @post.published? # => @post == :published, @post == 'published' @post.published! # => @post = :published @post.has_publish_status?(:hello) # => @post == :hello, @post == 'hello' @post.set_publish_status!(:hello) # => @post = :hello @post = :draft @post.next_publish_status! # => @post = :published @post = :draft @post.previous_publish_status! # => @post = :some_status @post.reset_published_status! # = @post = :published @post.publish_statuses_for_select # = [['Important!', 'important], ...] Post::PUBLISH_STATUSES # => [:draft, :published, :some_status] Post::DEFAULT_PUBLISH_STATUS # => :published
Things that passed my mind:
- Implement named scopes, or finder methods.
- Fix the :default option to work on initialize, now disabled.
Copyright © 2009 Jonas Grimfelt, released under the MIT-license.