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: zargony.com/ Github: github.com/zargony/activerecord_symbolize
Install
Gem
gem install symbolize
config.gem "symbolize", :source => 'http://gemcutter.org'
Rails 3+ Gemfile
gem "symbolize"
Plugin:
./script/plugin install git://github.com/nofxx/symbolize.git
or in Rails3+
rails plugin install ..
Rails 3 (beta)
Specs pass with rails 3, but a scope :public == fail.
Usage
Add “symbolize :attr_name” to your model class. You may also want to add validates_inclusion_of to restrict the possible values (just like an enum).
class User < ActiveRecord::Base
symbolize :gender, :in => [:female, :male], :scopes => true
symbolize :so, :in => {
:linux => "Linux",
:mac => "Mac OS X"
}, :scopes => true
# Allow blank
symbolize :gui, :in => [:gnome, :kde, :xfce], :allow_blank => true
# Don`t i18n
symbolize :browser, :in => [:firefox, :opera], :i18n => false, :methods => true
# Scopes
symbolize :angry, :in => [true, false], :scopes => true
# Don`t validate
symbolize :lang, :in => [:ruby, :c, :erlang], :validate => false
# Default
symbolize :kind, :in => [:admin, :manager, :user], :default => :admin
end
in/within
The values allowed on the enum field, you can provide a hash with => “Human text” or an array of keys to be i18n eval (or not). Booleans are also supported. See below.
allow_(blank|nil): What you expect.
validate
Set to false to avoid the validation of the input. Useful for a dropdown with an “other” option textfield.
method
If you provide the method option, some fancy boolean methods will be added: In our User example, browser has this option, so you can do:
@user.firefox?
@user.opera?
Booleans
Its possible to use boolean fields also.
symbolize :switch, :in => [true, false]
...
switch:
"true": On
"false": Off
"nil": Unknown
i18n
If you don`t provide a hash with values, it will try i18n:
activerecord:
attributes:
user:
enums:
gui:
gnome: Gnome Desktop Enviroment
kde: K Desktop Enviroment
xfce: XFCE4
gender:
female: Girl
male: Boy
You can skip i18n lookup with :i18n => false
symbolize :gender, :in => [:female, :male], :i18n => false
scopes (BETA)
If you provide the scopes option, some fancy named scopes will be added: In our User example, gender has this option, so you can do:
User.female => User.find(:all, :conditions => { :gender => :female })
You can chain named scopes as well:
User.female.mac => User.find(:all, :conditions => { :gender => :female, :so => :mac })
For boolean colums you can use
User.angry => User.find(:all, :conditions => { :angry => true })
User.not_angry => User.find(:all, :conditions => { :angry => false })
( or with_[attribute] and without_[attribute] )
default (BETA)
As the name suggest, the symbol you choose as default will be set in new objects automatically.
User.new.kind # Will print :admin
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.female
u = User.new(:name => 'ET', :gender => :unknown)
u.save # => validation fails
Examples Helpers
<% form_for @user do |f| %>
<%= f.radio_sym "gender" %>
<!-- Alphabetic order -->
<%= f.select_sym "so" %>
<!-- Fixed order -->
<%= f.select_sym "office" %>
<% end %>
output:
<form action="users/1" method="post">
<div style="margin:0;padding:0">...</div>
<label>Female <input id="user_gender_female" name="user[gender]" type="radio" value="female"></label>
<label>Male <input checked="checked" id="user_gender_male" name="user[gender]" type="radio" value="male" ></label>
<!-- Alphabetic order -->
<select id="user_so" name="post[so]">
<option value="linux" selected="selected">Linux</option>
<option value="mac">Mac OS X</option>
<option value="windows">Windows XP</option>
</select>
<!-- Fixed order -->
<select id="user_office" name="post[office]">
<option value="kde" selected="selected">Koffice</option>
<option value="ms">Microsoft Office</option>
<option value="open">Open Office</option>
</select>
</form>
Notes
This fork: github.com/nofxx/symbolize
Forked from: github.com/nuxlli/activerecord_symbolize
Initial work: I’ve been using this for quite some time and made it a rails plugin now. More background iinformation can be found at zargony.com/2007/09/07/symbolize-attribute-values-in-activerecord
Copyright © 2007-2008 Andreas Neuhaus, released under the MIT license