Plugin: selection_options_for

Many selection lists in an application do not change and so the quickest way to code them is to put the list in the code itself. For example:

  • Child, Adult, or Senior.
  • Male or Female.
  • Yes or No.

selection_options_for manages static selection lists. If you selection lists are dynamically generated this is not the answer.

selection_options_for allows you to

  • keep the display labels in the model
  • store a 1 character flag in the DB
  • test the value with methods in the code
class Article < ActiveRecord::Base
  selection_options_for :file_type_option,
    [ :pdf, 'PDF'],
    [ :hmlt, 'HTML'],
    [ :text, 'X', 'Textfile']
end # class

Then you can do stuff like ....

  assert   file_type_option_pdf?

OR

   <%= select :article, :file_type_option, Article.file_type_options %>

More examples are in the README file. Code available here ... or use ...

svn export --username anonymous http://railsstudio.com/svn/pub/plugins/selection_options_for/trunk selection_options_for

Thanks to Scott Barron for code review and tips on tightening up the code.

Related post:

Multi-select lists boxes in Rails


Created at: Fri Mar 16 16:04:53 +0000 2007 Updated at: Wed Jan 16 12:16:12 +0000 2008