Permalink
Browse files

refactor time_zone_options_for_select

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...
1 parent 67a5157 commit 9dd64f2e0c1b3ce85b503b0f81c50c93f89ffbcd @nashby nashby committed Feb 21, 2012
Showing with 8 additions and 7 deletions.
  1. +8 −7 actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -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

0 comments on commit 9dd64f2

Please sign in to comment.