Skip to content

Instantly share code, notes, and snippets.

@wndxlori
Created May 29, 2009 17:49
Show Gist options
  • Save wndxlori/120093 to your computer and use it in GitHub Desktop.
Save wndxlori/120093 to your computer and use it in GitHub Desktop.
Monkey patch for "options_for_select", which forces a title attribute on the option
#
# Monkey patch for "options_for_select", which forces a title attribute on the option which is the same as the text
# value which will be displayed in the select box.
#
# For what purpose, you ask? Well, it's like this. Selects have fixed widths and cannot be made to expand to fill
# available space :-p so you must choose a width when creating it, and hope for the best. If the data to be
# displayed in the select is too wide, then you just can't see the whole thing. With this patch, you will at least
# have access to the entire string as as hover-text, using the title attribute.
#
# If you don't like this, then just delete this file from the plugin, and you will be back to normal.
#
module ActionView
module Helpers
module FormOptionsHelper
def options_for_select(container, selected = nil)
container = container.to_a if Hash === container
options_for_select = container.inject([]) do |options, element|
text, value = option_text_and_value(element)
selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
title_attribute = ' title="#{text}"'
options << %(<option value="#{html_escape(value.to_s)}"#{selected_attribute}#{title_attribute}>#{html_escape(text.to_s)}</option>)
end
options_for_select.join("\n")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment