Project

enum_attr

0.0
No commit activity in last 3 years
No release in over 3 years
A Rails plugin which brings easy-to-use enum-like functionality to ActiveRecord models (now compatible with rails 3, ruby 1.9 and jruby). .
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

A rails plugin for active_record model¶ ↑

manage mapping column

Usage¶ ↑

In your model,for example in Post,you have a column named status(integer), 1 means normal, 2 means draft, 3 means deleted.

Now,with enum_attr plugin,you just decalre:

enum_attr :status, [ ["正常", 1, "normal"], ["草稿", 2, "draft"], ["已删除", 3, "deleted"] ]

with this declare, you have these code below:

consts:

STATUS_NORMAL = 1; STATUS_DRAFT = 2; STATUS_DELETED = 3
ENUMS_STATUS = [ ["正常", 1], ["草稿", 2], ["已删除", 3] ] # this is for select tag

valitates:

validates_inclusion_of "status", :in => [1,2,3], :allow_nil => true

methods:

# instant_method
def status_name
  # ... return readable status name according to stauts value(integer)
end

See more detail in spec test¶ ↑

更多用法见spec/enum_spec.rb文件

Inspiration¶ ↑

Thanks Quakewang
http://quake.javaeye.com/blog/448235

Copyright¶ ↑

Copyright © 2010 Qichunren. See LICENSE for details.