public
Description: Symbolize attribute values in ActiveRecord (e.g. for nicer enums)
Homepage: http://zargony.com/
Clone URL: git://github.com/zargony/activerecord_symbolize.git
zargony (author)
Sat Sep 27 03:41:26 -0700 2008
commit  d4924dd4f3b2167ca0563247901075ec91ecfe3e
tree    72079a1b5426ee51d8f477a1adf8320cdaf58bf1
parent  7d07cc6ad7395cd9731e882039984eb5f90428c6
name age message
file MIT-LICENSE Tue Apr 15 08:19:00 -0700 2008 Updated copyright and removed unneeded stuff [zargony]
file README Loading commit data...
file Rakefile Tue Apr 15 08:16:04 -0700 2008 Initial commit: rails plugin skeleton [zargony]
file init.rb Wed Apr 16 09:28:13 -0700 2008 Added code from http://zargony.com/2007/09/07/s... [zargony]
directory lib/
directory test/
README
= Symbolize attribute values in ActiveRecord (e.g. for nicer enums)

This plugin introduces an easy way to use symbols for values of ActiveRecord
attributes. Symbolized attributes return a ruby symbol (or nil) as their value
and can be set using symbols.

== About

Since ActiveRecord does not natively support database column types of ENUM or
SET, you'll usually use a string attribute and restrict it to certain values
with validations. Using this plugin, the values of such pseudo-enums are
symbols, which look more ruby-style than strings.

Simply add "symbolize :attr_name" to your model class, and the specified
attribute will return symbol values and can be set using smbols (setting
string values will still work, which is important when using forms).

An attribute to symbolize should be a string (varchar) column in the database.

Blog: http://zargony.com/
Github: http://github.com/zargony/activerecord_symbolize

== Install

  ./script/plugin install git://github.com/zargony/activerecord_symbolize.git

== Usage

Add "symbolize :attr_name" to your model class. An attribute validation
(like validates_inclusion_of) can be added by using the :in option.

  class User < ActiveRecord::Base
    symbolize :gender, :in => [:female, :male]
  end

== Examples

  u = User.find_by_name('Anna')   # => #<User Anna>
  u.gender                        # => :female
  
  u = User.find_by_gender(:male)  # => #<User Bob>
  u.gender                        # => :male
  
  u = User.find(:all, :conditions => { :gender => :female })
  
  u = User.new(:name => 'ET', :gender => :unknown)
  u.save                          # => validation fails

== Notes

I've been using this for quite some time and made it a rails plugin now. More
background information can be found at
http://zargony.com/2007/09/07/symbolize-attribute-values-in-activerecord


Copyright (c) 2007-2008 Andreas Neuhaus, released under the MIT license