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
Conversation
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. |
activesupport/test/time_zone_test.rb
Outdated
@@ -8,6 +8,17 @@ | |||
class TimeZoneTest < ActiveSupport::TestCase | |||
include TimeZoneTestHelpers | |||
|
|||
def setup | |||
ActiveSupport::TimeZone.instance_variable_set(:@lazy_zones_map, Concurrent::Map.new) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setup is overly coupled to the internal details of the implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]] }] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be written as:
@zones_map ||= MAPPING.transform_values { |tz| self[tz] }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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] }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. 69b96af
activesupport/test/time_zone_test.rb
Outdated
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
e7749b0
to
0c8c95c
Compare
0c8c95c
to
69b96af
Compare
Thanks @pixeltrix! Could you please also close #7245? |
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.
Summary
If you call
ActiveSupport::TimeZone.all
before you look up anyActiveSupport::TimeZone
s by name, then all calls toActiveSupport::TimeZone.all
for the lifetime of the program will return a collection ofActiveSupport::TimeZone
s corresponding to the 151 "meaningful" time zone identifiers described byActiveSupport::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 toActiveSupport::TimeZone.all
, then the collection returned from all calls to that method will contain the time zone you looked up.Abridged Timeline
ActiveSupport::TimeZone.all
returns inconsistent output, but instead thatActionView::Helpers::FormOptionsHelper#time_zone_options_for_select
should return a<select>
element that always contained theselected
time zone, regardless of whether or not that time zone was a member ofActiveSupport::TimeZone.all
.time_zone_options_for_select
. I do not think this solution fixes the actual problem since it does not remove the dependency ofActiveSupport::TimeZone.all
fromtime_zone_options_for_select
. As long as the former returns inconsistent output, so will the latter.ActiveSupport::TimeZone.all
. This has the effect that any time you callActiveSupport::TimeZone.all
, you get all time zones currently known (not just the ones fromActiveSupport::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.ActiveSupport::TimeZone.all
to return only time zones that are inActiveSupport::TimeZone::MAPPING
. I think this is absolutely 100% correct.