Permalink
Browse files

Fix tzinfo require (it broke test_raises_when_an_invalid_timezone_is_…

…defined_in_the_config in railties)
  • Loading branch information...
1 parent f76842f commit 0d3615f04c79f6e90d8ab33fdfc920b8faac9cb8 @jonleighton jonleighton committed Aug 16, 2011
Showing with 13 additions and 6 deletions.
  1. +13 −6 activesupport/lib/active_support/values/time_zone.rb
View
19 activesupport/lib/active_support/values/time_zone.rb
@@ -195,12 +195,8 @@ def self.seconds_to_utc_offset(seconds, colon = true)
# (GMT). Seconds were chosen as the offset unit because that is the unit that
# Ruby uses to represent time zone offsets (see Time#utc_offset).
def initialize(name, utc_offset = nil, tzinfo = nil)
- begin
- require 'tzinfo'
- rescue LoadError => e
- $stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install"
- raise e
- end
+ self.class.send(:require_tzinfo)
+
@name = name
@utc_offset = utc_offset
@tzinfo = tzinfo || TimeZone.find_tzinfo(name)
@@ -372,13 +368,24 @@ def us_zones
@us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ }
end
+ protected
+
+ def require_tzinfo
+ require 'tzinfo'
+ rescue LoadError
+ $stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install"
+ raise
+ end
+
private
def lookup(name)
(tzinfo = find_tzinfo(name)) && create(tzinfo.name.freeze)
end
def lazy_zones_map
+ require_tzinfo
@toolmantim
toolmantim added a line comment Sep 7, 2011

The performance of some Rails 3.1 apps after this was 500ms or more slower :(

lazy_zones_map gets called for every TimeZone#[] lookup, and if that's doing a require every time things get really slow.

https://gist.github.com/1200301

If we're going to do this for every lazy_zones_map call then we should really change require_tzinfo to include unless defined?(::TZInfo)

@toolmantim
toolmantim added a line comment Sep 7, 2011

Have added a pull request with the change:

#2911

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
@lazy_zones_map ||= Hash.new do |hash, place|
hash[place] = create(place) if MAPPING.has_key?(place)
end

0 comments on commit 0d3615f

Please sign in to comment.