Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Symbolize attribute values in ActiveRecord or Mongoid (nicer enums)
Ruby
branch: master

This branch is 15 commits ahead, 120 commits behind nofxx:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
Gemfile
MIT-LICENSE
README.rdoc
Rakefile
symbolize.gemspec

README.rdoc

Symbolize attribute values

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

Install

Gem

gem install symbolize

Mongoid

gem "symbolize", :require => "symbolize/mongoid"

ActiveRecord

gem "symbolize", :require => "symbolize/active_record"

About

Just use “symbolize :attribute” in your model, and the specified attribute will return symbol values and can be set using symbols (setting string values works, which is important when using forms).

On schema DBs, the attribute should be a string (varchar) column.

Usage

class User < ActiveRecord::Base
  or Mongoid::Document

  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, :js, :c, :erlang], :validate => false

  # Default
  symbolize :kind, :in => [:admin, :manager, :user], :default => :user

end

in/within

The values allowed on the enum field, you can provide a hash like {:value => “Human text”} or an array of keys to run i18n on. 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:
or
mongoid:
  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 :style, :in => [:rock, :punk, :funk, :jazz], :i18n => false

scopes

If you provide the scopes option, some fancy named scopes will be added: In our User example, gender has male/female options, so you can do:

# AR
User.male    # => User.all(:conditions => { :gender => :male })

# Mongoid
User.female  # => User.where({ :gender => :female })

You can chain named scopes as well:

User.female.mac => User.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

As the name suggest, the symbol you choose as default will be set in new objects automatically. Mongoid only for now.

symbolize :mood, :in => [:happy, :sad, :euphoric], :default => (MarvinDay ? :sad : :happy)

User.new.kind # It may print :happy

Rails Form Example

You may call `Class.get_<attribute>_values` anywhere to get a nice array. Works nice with dropdowns. Examples:

class Coffee
  symbolize :genetic, :in => [:arabica, :robusta, :blend]
end

- form_for(@coffee) do |f|
  = f.label :genetic
  = f.select :genetic, Coffee.get_genetic_values

Somewhere on a view:

= select_tag :kind, Coffee.get_genetic_values

View Helpers (DEPRECATED)

<% form_for @user do |f| %>
  <%= f.radio_sym "gender" %>
  <!-- Alphabetic order -->
  <%= f.select_sym "so" %>
  <!-- Fixed order -->
  <%= f.select_sym "office" %>
<%end>

Specs

Run the adapter independently:

$ rspec spec/symbolize/mongoid_spec.rb
$ rspec spec/symbolize/active_record_spec.rb

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

Something went wrong with that request. Please try again.