This module is very useful to handle static lists (like enumerations).
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.
(I want to store the hair color of the user...)
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
class User < ActiveRecord::Base ... include StaticList::Validate validates_static_list_value :hair_color, HairColor, :allow_blank => true ... end
module ApplicationHelper ... include StaticList::Helpers ... end
... <%= f.select :hair_color, static_list_select_options(HairColor) %> ...
... <%= t_static_list(@user.hair_color, HairColor) %> ...
... hair_color: white: white blond: blond red: red ...
... hair_color: white: blancs blond: blonds red: rouges ...
Copyright © 2010-2011 Novelys. See LICENSE.txt for details.