Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

This gem is very useful to handle static lists (like enumerations).

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 test
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 static_list.gemspec
README.rdoc

Static List

This module is very useful to handle static lists (like enumerations).

The problem:

In your application you may want to handle things in your User model like sex (female, male) or other static lists. You want these lists to be handled using 'textual keys' in your application but stored in your database using codes in an integer column. You don't want to join other tables to display these information. You want these lists to be easily ordered, localized and translated using Rails i18n. You want view helpers to display these lists localized and validations helpers to validate the values in the 'receiving' model.

Example :

(I want to store the hair color of the user...)

(hair_color.rb)

class HairColor
  include StaticList::Model
  static_list [[:white, 1], [:blond, 2], [:red, 3], [:light_brown, 4], [:brown, 5], [:black, 6], [:colored, 7], [:bald, 8]]
end

(user.rb)

class User < ActiveRecord::Base
  ...
  include StaticList::Validate

  validates_static_list_value :hair_color, HairColor, :allow_blank => true
  ...
end

(application_helper.rb)

module ApplicationHelper
  ...
  include StaticList::Helpers
  ...
end

(_form.html.erb)

...
<%= f.select :hair_color, static_list_select_options(HairColor) %>
...

(show.html.erb)

...
<%= t_static_list(@user.hair_color, HairColor) %>
...

(en.yml)

...
hair_color:
  white: white
  blond: blond
  red: red
...

(fr.yml)

...
hair_color:
  white: blancs
  blond: blonds
  red: rouges
...

Copyright

Copyright © 2010-2011 Novelys. See LICENSE.txt for details.

Contributors

Something went wrong with that request. Please try again.