@@ -270,6 +270,15 @@ def time_zone_select(object, method, priority_zones = nil, options = {}, html_op
270
270
# options_for_select([ "VISA", "MasterCard", "Discover" ], ["VISA", "Discover"])
271
271
# <option selected="selected">VISA</option>\n<option>MasterCard</option>\n<option selected="selected">Discover</option>
272
272
#
273
+ # You can optionally provide html attributes as the last element of the array.
274
+ #
275
+ # Examples:
276
+ # options_for_select([ "Denmark", ["USA", {:class=>'bold'}], "Sweden" ], ["USA", "Sweden"])
277
+ # <option value="Denmark">Denmark</option>\n<option value="USA" class="bold" selected="selected">USA</option>\n<option value="Sweden" selected="selected">Sweden</option>
278
+ #
279
+ # options_for_select([["Dollar", "$", {:class=>"bold"}], ["Kroner", "DKK", {:onclick => "alert('HI');"}]])
280
+ # <option value="$" class="bold">Dollar</option>\n<option value="DKK" onclick="alert('HI');">Kroner</option>
281
+ #
273
282
# If you wish to specify disabled option tags, set +selected+ to be a hash, with <tt>:disabled</tt> being either a value
274
283
# or array of values to be disabled. In this case, you can use <tt>:selected</tt> to specify selected option tags.
275
284
#
@@ -291,10 +300,11 @@ def options_for_select(container, selected = nil)
291
300
selected , disabled = extract_selected_and_disabled ( selected )
292
301
293
302
options_for_select = container . inject ( [ ] ) do |options , element |
303
+ html_attributes = option_html_attributes ( element )
294
304
text , value = option_text_and_value ( element )
295
305
selected_attribute = ' selected="selected"' if option_value_selected? ( value , selected )
296
306
disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected? ( value , disabled )
297
- options << %(<option value="#{ html_escape ( value . to_s ) } "#{ selected_attribute } #{ disabled_attribute } >#{ html_escape ( text . to_s ) } </option>)
307
+ options << %(<option value="#{ html_escape ( value . to_s ) } "#{ selected_attribute } #{ disabled_attribute } #{ html_attributes } >#{ html_escape ( text . to_s ) } </option>)
298
308
end
299
309
300
310
options_for_select . join ( "\n " ) . html_safe
@@ -486,9 +496,22 @@ def time_zone_options_for_select(selected = nil, priority_zones = nil, model = :
486
496
end
487
497
488
498
private
499
+ def option_html_attributes ( element )
500
+ return "" unless Array === element
501
+ html_attributes = [ ]
502
+ element . select { |e | Hash === e } . reduce ( { } , :merge ) . each do |k , v |
503
+ html_attributes << " #{ k } =\" #{ html_escape ( v . to_s ) } \" "
504
+ end
505
+ html_attributes . join
506
+ end
507
+
489
508
def option_text_and_value ( option )
490
509
# Options are [text, value] pairs or strings used for both.
491
- if !option . is_a? ( String ) and option . respond_to? ( :first ) and option . respond_to? ( :last )
510
+ case
511
+ when Array === option
512
+ option = option . reject { |e | Hash === e }
513
+ [ option . first , option . last ]
514
+ when !option . is_a? ( String ) && option . respond_to? ( :first ) && option . respond_to? ( :last )
492
515
[ option . first , option . last ]
493
516
else
494
517
[ option , option ]
0 commit comments