libraries/view_helper.rb in Pimki-1.3.092 vs libraries/view_helper.rb in Pimki-1.4.092

- old
+ new

@@ -1,33 +1,33 @@ -module ViewHelper - # Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container - # where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and - # the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values - # become lasts. If +selected+ is specified, the matching "last" or element will get the selected option-tag. - # - # Examples (call, result): - # html_options([["Dollar", "$"], ["Kroner", "DKK"]]) - # <option value="$">Dollar</option>\n<option value="DKK">Kroner</option> - # - # html_options([ "VISA", "Mastercard" ], "Mastercard") - # <option>VISA</option>\n<option selected>Mastercard</option> - # - # html_options({ "Basic" => "$20", "Plus" => "$40" }, "$40") - # <option value="$20">Basic</option>\n<option value="$40" selected>Plus</option> - def html_options(container, selected = nil) - container = container.to_a if Hash === container - - html_options = container.inject([]) do |options, element| - if element.respond_to?(:first) && element.respond_to?(:last) - if element.last != selected - options << "<option value=\"#{element.last}\">#{element.first}</option>" - else - options << "<option value=\"#{element.last}\" selected>#{element.first}</option>" - end - else - options << ((element != selected) ? "<option>#{element}</option>" : "<option selected>#{element}</option>") - end - end - - html_options.join("\n") - end +module ViewHelper + # Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container + # where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and + # the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values + # become lasts. If +selected+ is specified, the matching "last" or element will get the selected option-tag. + # + # Examples (call, result): + # html_options([["Dollar", "$"], ["Kroner", "DKK"]]) + # <option value="$">Dollar</option>\n<option value="DKK">Kroner</option> + # + # html_options([ "VISA", "Mastercard" ], "Mastercard") + # <option>VISA</option>\n<option selected>Mastercard</option> + # + # html_options({ "Basic" => "$20", "Plus" => "$40" }, "$40") + # <option value="$20">Basic</option>\n<option value="$40" selected>Plus</option> + def html_options(container, selected = nil) + container = container.to_a if Hash === container + + html_options = container.inject([]) do |options, element| + if element.respond_to?(:first) && element.respond_to?(:last) + if element.last != selected + options << "<option value=\"#{element.last}\">#{element.first}</option>" + else + options << "<option value=\"#{element.last}\" selected>#{element.first}</option>" + end + else + options << ((element != selected) ? "<option>#{element}</option>" : "<option selected>#{element}</option>") + end + end + + html_options.join("\n") + end end \ No newline at end of file