Skip to content

Commit f4b29ce

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5676 [Monkey Patch Removal] Remove Time#configured (#5716)
* Removes Time#configured monkey patch. * Set UTC timezone by default * More spec cleanup * Re-add error check * add release notes for removal of `Time.configured` --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent 3e2dd93 commit f4b29ce

File tree

16 files changed

+64
-48
lines changed

16 files changed

+64
-48
lines changed

docs/release-notes/mongoid-9.0.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,46 @@ There are also rake tasks available, for convenience:
403403
$ rake mongoid:db:remove_search_indexes
404404

405405

406+
``Time.configured`` has been removed
407+
------------------------------------
408+
409+
``Time.configured`` returned either the time object wrapping the configured
410+
time zone, or the standard Ruby ``Time`` class. This allowed you to query
411+
a time value even if no time zone had been configured.
412+
413+
Mongoid now requires that you set a time zone if you intend to do
414+
anything with time values (including using timestamps in your documents).
415+
Any uses of ``Time.configured`` must be replaced with ``Time.zone``.
416+
417+
... code-block:: ruby
418+
419+
# before:
420+
puts Time.configured.now
421+
422+
# after:
423+
puts Time.zone.now
424+
425+
# or, better for finding the current Time specifically:
426+
puts Time.current
427+
428+
If you do not set a time zone, you will see errors in your code related
429+
to ``nil`` values. If you are using Rails, the default time zone is already
430+
set to UTC. If you are not using Rails, you may set a time zone at the start
431+
of your program like this:
432+
433+
... code-block:: ruby
434+
435+
Time.zone = 'UTC'
436+
437+
This will set the time zone to UTC. You can see all available time zone names
438+
by running the following command:
439+
440+
... code-block:: bash
441+
442+
$ ruby -ractive_support/values/time_zone \
443+
-e 'puts ActiveSupport::TimeZone::MAPPING.keys'
444+
445+
406446
Bug Fixes and Improvements
407447
--------------------------
408448

lib/mongoid/criteria/queryable/extensions/date.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __evolve_date__
2626
#
2727
# @return [ Time | ActiveSupport::TimeWithZone ] The date as a local time.
2828
def __evolve_time__
29-
::Time.configured.local(year, month, day)
29+
::Time.zone.local(year, month, day)
3030
end
3131

3232
module ClassMethods

lib/mongoid/extensions/array.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __mongoize_object_id__
5252
# configured default time zone corresponding to date/time components
5353
# in this array.
5454
def __mongoize_time__
55-
::Time.configured.local(*self)
55+
::Time.zone.local(*self)
5656
end
5757

5858
# Is the array a set of multiple arguments in a method?

lib/mongoid/extensions/date.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Date
1717
# configured default time zone corresponding to local midnight of
1818
# this date.
1919
def __mongoize_time__
20-
::Time.configured.local(year, month, day)
20+
::Time.zone.local(year, month, day)
2121
end
2222

2323
# Turn the object from the ruby type we deal with to a Mongo friendly

lib/mongoid/extensions/float.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Float
1414
#
1515
# @return [ Time | ActiveSupport::TimeWithZone ] The time.
1616
def __mongoize_time__
17-
::Time.configured.at(self)
17+
::Time.zone.at(self)
1818
end
1919

2020
# Is the float a number?

lib/mongoid/extensions/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Integer
1414
#
1515
# @return [ Time | ActiveSupport::TimeWithZone ] The time.
1616
def __mongoize_time__
17-
::Time.configured.at(self)
17+
::Time.zone.at(self)
1818
end
1919

2020
# Is the integer a number?

lib/mongoid/extensions/string.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,17 @@ def __mongoize_object_id__
4040
# "2012-01-01".__mongoize_time__
4141
# # => 2012-01-01 00:00:00 -0500
4242
#
43+
# @raise [ ArgumentError ] The string is not a valid time string.
44+
#
4345
# @return [ Time | ActiveSupport::TimeWithZone ] Local time in the
4446
# configured default time zone corresponding to this string.
4547
def __mongoize_time__
46-
# This extra parse from Time is because ActiveSupport::TimeZone
47-
# either returns nil or Time.now if the string is empty or invalid,
48-
# which is a regression from pre-3.0 and also does not agree with
49-
# the core Time API.
50-
parsed = ::Time.parse(self)
51-
if ::Time == ::Time.configured
52-
parsed
53-
else
54-
::Time.configured.parse(self)
55-
end
48+
# This extra Time.parse is required to raise an error if the string
49+
# is not a valid time string. ActiveSupport::TimeZone does not
50+
# perform this check.
51+
::Time.parse(self)
52+
53+
::Time.zone.parse(self)
5654
end
5755

5856
# Convert the string to a collection friendly name.

lib/mongoid/extensions/time.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,6 @@ def mongoize
3030

3131
module ClassMethods
3232

33-
# Get the configured time to use when converting - either the time zone
34-
# or the time.
35-
#
36-
# @example Get the configured time.
37-
# ::Time.configured
38-
#
39-
# @return [ Time ] The configured time.
40-
#
41-
# @deprecated
42-
def configured
43-
::Time.zone || ::Time
44-
end
45-
4633
# Convert the object from its mongo friendly ruby type to this type.
4734
#
4835
# @example Demongoize the object.

lib/mongoid/timestamps/created.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ module Created
2424
# person.set_created_at
2525
def set_created_at
2626
if !timeless? && !created_at
27-
time = Time.configured.now
28-
self.updated_at = time if is_a?(Updated) && !updated_at_changed?
29-
self.created_at = time
27+
now = Time.current
28+
self.updated_at = now if is_a?(Updated) && !updated_at_changed?
29+
self.created_at = now
3030
end
3131
clear_timeless_option
3232
end

lib/mongoid/timestamps/updated.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Updated
2525
# person.set_updated_at
2626
def set_updated_at
2727
if able_to_set_updated_at?
28-
self.updated_at = Time.configured.now unless updated_at_changed?
28+
self.updated_at = Time.current unless updated_at_changed?
2929
end
3030

3131
clear_timeless_option

0 commit comments

Comments
 (0)