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::Baseselection_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

   

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

svn export --username anonymous http://agiledna.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


-- Mark Windholtz

Read more at our BLOG.