
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
selection_options_for
Advanced tools
This code allows you to keep the display labels in the model when the DB holds only a 1 character flag. and when the code requires symbolic references to the value to use in algorithms
Please see also the new 'state_objects' Gem StateObjects also supports behavior changes based on state and reduces conditional logic. https://github.com/mwindholtz/state_objects
Add this line to your application's Gemfile:
gem 'selection_options_for'
And then execute:
$ bundle
Or install it yourself as:
gem install selection_options_for
This code allows you to keep the display labels in the model when the DB holds only a 1 character flag. and when the code requires symbolic references to the value to use in algorithms
Don't use this if you will run reports directly against the DB In that case, the reports will not have access to the display labels
class Article < ActiveRecord::Base
selection_options_for :file_type_option,
[:pdf, 'P', 'PDF'],
[:html, 'H', 'HTML'],
[:msword, 'M', 'MS-Word']
[:text, 'X', 'Textfile']
end
file_type_options returns a array of 2-value arrays suitable to fill a select tag The second example shows how to start the selection on a blank
<%= select :article, :file_type_option, Article.file_type_options %> <%= select :article, :file_type_option, [['','']] + Article.file_type_options %>
assert_equal ["MS-Word", "PDF", "HTML"], Article.file_type_option_hash.values assert_equal "['MS-Word', 'PDF', 'HTML']", Article.file_type_option_js_list
file_type_option_symbols # returns hash of symbols
adds the following INSTANCE METHODS to Article
file_type_option_hash
file_type_option # returns the single character value as in the db
file_type_option_label # returns the current values label
file_type_option_symbol # returns the current values symbol
methods ending in '?' return boolean if the value is set
methods ending in '!' set the value
file_type_option_pdf?
file_type_option_pdf!
file_type_option_html? file_type_option_html!
file_type_option_msword? file_type_option_msword!
article = Article.new
article.file_type_option_pdf!
assert_equal 'P', article.file_type_option
assert_equal :pdf, article.file_type_symbol
assert_equal true, article.file_type_option_pdf?
assert_equal 'PDF', article.file_type_option_label
assert_equal [["MS-Word", "M"], ["PDF", "P"], ["HTML", "H"]],
Article.file_type_options
assert_equal({"M"=>"MS-Word", "P"=>"PDF", "H"=>"HTML"},
Article.file_type_option_hash)
assert_equals({'P'=>:pdf, 'H'=>:html, 'W'=>:msword, 'T'=>:text},
Article.file_type_option_symbols)
By default the value first letter of the label is used as the one character value in the database field. When there are duplicate first letters you can specify a different letter to be stored in the database In the example below 'R' is stored in the database when 'Credit Card Account' is selected.
class Article < ActiveRecord::Base
selection_options_for :payment_method_option,
[:basic, 'B', 'Basic'],
[:cash, 'C', 'Cash Account'],
[:cc, 'R','Credit Card Account']
end
<%= select :article, :payment_method_option, Article.payment_method_options %>
<% Article.payment_method_option_hash.each do | key, value | %>
<%= radio_button :article, :payment_method_option, key %> <%= value %><br />
<% end %>
payment_method_option_js_list
git checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that selection_options_for demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.