Skip to content

Rails: The missing status/enum field helpers for ActiveRecord.

License

Notifications You must be signed in to change notification settings

grimen/has_status

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HAS_STATUS

The missing status/enum field helpers for ActiveRecord.

Installation

sudo gem install grimen-has_status

Usage

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

More…?

Things that passed my mind:

  • Implement named scopes, or finder methods.
  • Fix the :default option to work on initialize, now disabled.

License

Copyright © 2009 Jonas Grimfelt, released under the MIT-license.

About

Rails: The missing status/enum field helpers for ActiveRecord.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages