Merge pull request #5117 from nashby/form-option-refactor

FormOptionsHelper refactor
This commit is contained in:
José Valim 2012-02-25 06:05:23 -08:00
commit 29054ba173
1 changed files with 11 additions and 17 deletions

View File

@ -506,23 +506,24 @@ module ActionView
# NOTE: Only the option tags are returned, you have to wrap this call in # NOTE: Only the option tags are returned, you have to wrap this call in
# a regular HTML select tag. # a regular HTML select tag.
def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone) def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)
zone_options = "" zone_options = "".html_safe
zones = model.all zones = model.all
convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } } convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }
if priority_zones if priority_zones
if priority_zones.is_a?(Regexp) if priority_zones.is_a?(Regexp)
priority_zones = model.all.find_all {|z| z =~ priority_zones} priority_zones = zones.select { |z| z =~ priority_zones }
end
zone_options += options_for_select(convert_zones[priority_zones], selected)
zone_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
zones = zones.reject { |z| priority_zones.include?( z ) }
end end
zone_options += options_for_select(convert_zones[zones], selected) zone_options.safe_concat options_for_select(convert_zones[priority_zones], selected)
zone_options.html_safe zone_options.safe_concat content_tag(:option, '-------------', :value => '', :disabled => 'disabled')
zone_options.safe_concat "\n"
zones.reject! { |z| priority_zones.include?(z) }
end
zone_options.safe_concat options_for_select(convert_zones[zones], selected)
end end
# Returns radio button tags for the collection of existing return values # Returns radio button tags for the collection of existing return values
@ -659,11 +660,8 @@ module ActionView
def option_text_and_value(option) def option_text_and_value(option)
# Options are [text, value] pairs or strings used for both. # Options are [text, value] pairs or strings used for both.
case if !option.is_a?(String) && option.respond_to?(:first) && option.respond_to?(:last)
when Array === option option = option.reject { |e| Hash === e } if Array === option
option = option.reject { |e| Hash === e }
[option.first, option.last]
when !option.is_a?(String) && option.respond_to?(:first) && option.respond_to?(:last)
[option.first, option.last] [option.first, option.last]
else else
[option, option] [option, option]
@ -671,11 +669,7 @@ module ActionView
end end
def option_value_selected?(value, selected) def option_value_selected?(value, selected)
if selected.respond_to?(:include?) && !selected.is_a?(String) Array(selected).include? value
selected.include? value
else
value == selected
end
end end
def extract_selected_and_disabled(selected) def extract_selected_and_disabled(selected)