GithubHelp home page GithubHelp logo

has_status's Introduction

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.

has_status's People

Contributors

grimen avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.