github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

nuxlli / activerecord_symbolize forked from zargony/activerecord_symbolize

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 7
    • 4
  • Source
  • Commits
  • Network (4)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Symbolize attribute values in ActiveRecord (e.g. for nicer enums) — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Update for compatibility with Rails 2.2 and 2.3 
nuxlli (author)
Sun Apr 26 13:23:22 -0700 2009
commit  04f470ae7a96f36d0e835d85f0a6305cededc50f
tree    9f83d5956da51669b8a903d6b3cc7d2684f7f6c5
parent  bf7a8bd1d37b3a5961fbbcc2e5fda9850bf7a6d6
activerecord_symbolize /
name age
history
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
file init.rb
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. 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]
    symbolize :so, :in => {
      :windows => "Windows XP",
      :linux   => "Linux",
      :mac     => "Mac OS X"
    }
    symbolize :office, :allow_blank => true, :in => [
      [:kde , 'Koffice'],
      [:ms  , 'Microsoft Office'],
      [:open, 'Open Office']
    ]
  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
  
== 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

I've been using this for quite some time and made it a rails plugin now. More
background iinformation 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
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server