New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ActiveSupport::TimeZone.all independent of previous lookups #31176

Merged
merged 5 commits into from Nov 22, 2017

Conversation

Projects
None yet
4 participants
@cjlarose
Contributor

cjlarose commented Nov 17, 2017

Summary

If you call ActiveSupport::TimeZone.all before you look up any ActiveSupport::TimeZones by name, then all calls to ActiveSupport::TimeZone.all for the lifetime of the program will return a collection of ActiveSupport::TimeZones corresponding to the 151 "meaningful" time zone identifiers described by ActiveSupport::TimeZone::MAPPING.

If, however, you look up a time zone by a name that is not a key of ActiveSupport::TimeZone::MAPPING before making a call to ActiveSupport::TimeZone.all, then the collection returned from all calls to that method will contain the time zone you looked up.

Abridged Timeline

  • August 2012: The issue is first described in #7245. It is proposed that the issue is not necessarily that ActiveSupport::TimeZone.all returns inconsistent output, but instead that ActionView::Helpers::FormOptionsHelper#time_zone_options_for_select should return a <select> element that always contained the selected time zone, regardless of whether or not that time zone was a member of ActiveSupport::TimeZone.all.
  • November 2012: #8377 is opened to fix time_zone_options_for_select. I do not think this solution fixes the actual problem since it does not remove the dependency of ActiveSupport::TimeZone.all from time_zone_options_for_select. As long as the former returns inconsistent output, so will the latter.
  • December 2012: #8660 is opened with changes very similar to the changes proposed by this PR.
  • April 2013: #8660 was closed because it was thought that the issue was still in the view code.
  • January 2015: #8377 is closed
  • December 2015: #22651 is opened to remove the cache used for ActiveSupport::TimeZone.all. This has the effect that any time you call ActiveSupport::TimeZone.all, you get all time zones currently known (not just the ones from ActiveSupport::TimeZone::MAPPING). This PR is closed because it would introduce a performance regression. It's proposed to keep the cache around, but to invalidate it when we discover a new time zone.
  • February 2017: #27917 is opened to invalidate some caches when new time zones are discovered. @jeremy suggests that doing this behavior is actually quite strange. The behavior that makes most sense is for ActiveSupport::TimeZone.all to return only time zones that are in ActiveSupport::TimeZone::MAPPING. I think this is absolutely 100% correct.
@rails-bot

This comment has been minimized.

rails-bot commented Nov 17, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@@ -8,6 +8,17 @@
class TimeZoneTest < ActiveSupport::TestCase
include TimeZoneTestHelpers
def setup
ActiveSupport::TimeZone.instance_variable_set(:@lazy_zones_map, Concurrent::Map.new)

This comment has been minimized.

@sgrif

sgrif Nov 17, 2017

Member

This setup is overly coupled to the internal details of the implementation

This comment has been minimized.

@cjlarose

cjlarose Nov 17, 2017

Contributor

Do you have any suggestions for an alternative way to do this? The test I wrote depends on the caches being cleared before making the first call to ActiveSupport::TimeZone.all.

MAPPING.each_key { |place| self[place] } # load all the zones
@lazy_zones_map
end
@zones_map ||= Hash[MAPPING.keys.map { |place| [place, self[place]] }]

This comment has been minimized.

@pixeltrix

pixeltrix Nov 20, 2017

Member

This can be written as:

@zones_map ||= MAPPING.transform_values { |tz| self[tz] }

This comment has been minimized.

@cjlarose

cjlarose Nov 20, 2017

Contributor

Not quite. It seems important to use the keys of MAPPING to perform lookups using #[] because the argument to #[] ends up on the name attribute of the returned ActiveSupport::TimeZone instance.

> Hash[MAPPING.keys.map { |k| [k, self[k]] }]['UTC'].name
=> "UTC"
> MAPPING.transform_values { |v| self[v] }['UTC'].name
=> "Etc/UTC"

If there's a nice way to use Hash#transform_values in a way that I have access to the key, it would work, but I don't think that exists.

> MAPPING.transform_values { |_, k| self[k] }['UTC'].name
=> "UTC"

This comment has been minimized.

@pixeltrix

pixeltrix Nov 21, 2017

Member

Sorry about that - you can use each_with_object to get both the key and the value, e.g.

@zones_map ||= MAPPING.each_with_object({}) { |(name, _), zones| zones[name] = self[name] }

This comment has been minimized.

@cjlarose

cjlarose Nov 22, 2017

Contributor

Done. 69b96af

@@ -8,6 +8,12 @@
class TimeZoneTest < ActiveSupport::TestCase
include TimeZoneTestHelpers
def setup
# Clear memoized time zone data on TimeZone class
ActiveSupport.send(:remove_const, :TimeZone) if ActiveSupport.const_defined?(:TimeZone)

This comment has been minimized.

@pixeltrix

pixeltrix Nov 20, 2017

Member

Still not happy with this - I'd rather we add a nodoc public clear method on AS::TimeZone to explicitly clear the cache and then use that in the test.

Requested changes were made

@pixeltrix pixeltrix merged commit 078421b into rails:master Nov 22, 2017

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@cjlarose

This comment has been minimized.

Contributor

cjlarose commented Nov 22, 2017

Thanks @pixeltrix! Could you please also close #7245?

kamipo added a commit that referenced this pull request Nov 27, 2017

Enable `Style/DefWithParentheses` rubocop rule
The def with blank `()` was newly added in #31176, but we have not used
the blank `()` style in most part of our code base.
So I've enabled `Style/DefWithParentheses` to prevent to newly added the
code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment