An open API service indexing awesome lists of open source software.

https://github.com/grimen/has_status

Rails: The missing status/enum field helpers for ActiveRecord.
https://github.com/grimen/has_status

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

        

h1. HAS_STATUS

_The missing status/enum field helpers for ActiveRecord._

h2. Installation

sudo gem install grimen-has_status

h2. 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

h2. More...?

Things that passed my mind:

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

h2. License

Copyright (c) 2009 Jonas Grimfelt, released under the MIT-license.