Selection Groups in Rails made less cumbersome
I made this addition today after (once again) struggling with option groups for a nested Rails model.
My solution: create a new tag for FormOptionsHelper: grouped_select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### lib/form_options_helper.rb | |
require 'action_view/helpers/form_helper' | |
module ActionView | |
module Helpers | |
module FormOptionsHelper | |
## | |
def grouped_select(object, method, choices, options = {}, html_options = {}) | |
InstanceTag.new(object, method, self, options.delete(:object)).to_grouped_select_tag(choices, options, html_options) | |
end | |
end | |
class InstanceTag | |
include FormOptionsHelper | |
def to_grouped_select_tag(choices, options, html_options) | |
html_options = html_options.stringify_keys | |
add_default_name_and_id(html_options) | |
value = value(object) | |
selected_value = options.has_key?(:selected) ? options[:selected] : value | |
disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil | |
content_tag("select", add_options(grouped_options_for_select(choices, :selected => selected_value, :disabled => disabled_value), options, selected_value), html_options) | |
end | |
end | |
class FormBuilder | |
def grouped_select(method, choices, options = {}, html_options = {}) | |
@template.grouped_select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options)) | |
end | |
end | |
end | |
end | |
### config/environment.rb | |
require 'form_options_helper' | |
### app/models/expense_type.rb | |
class ExpenseType < ActiveRecord::Base | |
validates_uniqueness_of :name | |
#returns grouped options split in 'popular' and 'all' | |
def self.groups | |
all_types = self.all.map{|t| [t.name, t.id]} | |
[ | |
['popular', all_types[0..2]], | |
['all', all_types] | |
] | |
end | |
end | |
### app/views/expense_report/_expense_item.rb | |
<li class="expense_item"> | |
<%= expense.grouped_select :expense_type_id, ExpenseType.groups, {:include_blank => true}, {:class => 'expense_type'} %> | |
... | |
</li> |