Skip to content

Instantly share code, notes, and snippets.

@astevens
Created March 18, 2011 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astevens/876802 to your computer and use it in GitHub Desktop.
Save astevens/876802 to your computer and use it in GitHub Desktop.
module Merb::Helpers::Form::Builder
class Base
private
def options_for(attrs)
blank, prompt = attrs.delete(:include_blank), attrs.delete(:prompt)
b = blank || prompt ? tag(:option, prompt || "", :value => "") : ""
# yank out the options attrs
collection = attrs.delete(:collection)
selected = attrs.delete(:selected)
text_method = attrs.delete(:text_method)
value_method = attrs.delete(:value_method)
# if the collection is a Hash, optgroups are a-coming
if collection.is_a?(Hash)
([b] + collection.map do |g,col|
tag(:optgroup, options(col, text_method, value_method, selected), :label => g)
end).join
else
options(collection || [], text_method, value_method, selected, b)
end
end # options_for
end # Class
end # Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment