Skip to content

Commit

Permalink
refactor time_zone_options_for_select
Browse files Browse the repository at this point in the history
BTW, select works quite faster then find_all:
require 'benchmark'

n = [1]*100_000_000
Benchmark.bm do |x|
  x.report { n.select { |a| a > 1 } }
  x.report { n.find_all { |a| a > 1 } }
end

    user     system      total        real
7.590000   0.010000   7.600000 (  7.927171)
9.650000   0.010000   9.660000 (  9.634406)
  • Loading branch information
nashby committed Feb 21, 2012
1 parent 67a5157 commit 9dd64f2
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions actionpack/lib/action_view/helpers/form_options_helper.rb
Expand Up @@ -506,23 +506,24 @@ def grouped_options_for_select(grouped_options, selected_key = nil, prompt = nil
# NOTE: Only the option tags are returned, you have to wrap this call in
# a regular HTML select tag.
def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)
zone_options = ""
zone_options = "".html_safe

zones = model.all
convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }

if priority_zones
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 ) }
zone_options.safe_concat options_for_select(convert_zones[priority_zones], selected)
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 += options_for_select(convert_zones[zones], selected)
zone_options.html_safe
zone_options.safe_concat options_for_select(convert_zones[zones], selected)
end

# Returns radio button tags for the collection of existing return values
Expand Down

0 comments on commit 9dd64f2

Please sign in to comment.