Skip to content

Commit

Permalink
MONGOID-5676 [Monkey Patch Removal] Remove Time#configured (#5716)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
johnnyshields and jamis authored Nov 8, 2023
1 parent 3e2dd93 commit f4b29ce
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 48 deletions.
40 changes: 40 additions & 0 deletions docs/release-notes/mongoid-9.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,46 @@ There are also rake tasks available, for convenience:
$ rake mongoid:db:remove_search_indexes


``Time.configured`` has been removed
------------------------------------

``Time.configured`` returned either the time object wrapping the configured
time zone, or the standard Ruby ``Time`` class. This allowed you to query
a time value even if no time zone had been configured.

Mongoid now requires that you set a time zone if you intend to do
anything with time values (including using timestamps in your documents).
Any uses of ``Time.configured`` must be replaced with ``Time.zone``.

... code-block:: ruby

# before:
puts Time.configured.now

# after:
puts Time.zone.now

# or, better for finding the current Time specifically:
puts Time.current

If you do not set a time zone, you will see errors in your code related
to ``nil`` values. If you are using Rails, the default time zone is already
set to UTC. If you are not using Rails, you may set a time zone at the start
of your program like this:

... code-block:: ruby

Time.zone = 'UTC'

This will set the time zone to UTC. You can see all available time zone names
by running the following command:

... code-block:: bash

$ ruby -ractive_support/values/time_zone \
-e 'puts ActiveSupport::TimeZone::MAPPING.keys'


Bug Fixes and Improvements
--------------------------

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/criteria/queryable/extensions/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __evolve_date__
#
# @return [ Time | ActiveSupport::TimeWithZone ] The date as a local time.
def __evolve_time__
::Time.configured.local(year, month, day)
::Time.zone.local(year, month, day)
end

module ClassMethods
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __mongoize_object_id__
# configured default time zone corresponding to date/time components
# in this array.
def __mongoize_time__
::Time.configured.local(*self)
::Time.zone.local(*self)
end

# Is the array a set of multiple arguments in a method?
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Date
# configured default time zone corresponding to local midnight of
# this date.
def __mongoize_time__
::Time.configured.local(year, month, day)
::Time.zone.local(year, month, day)
end

# Turn the object from the ruby type we deal with to a Mongo friendly
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/float.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Float
#
# @return [ Time | ActiveSupport::TimeWithZone ] The time.
def __mongoize_time__
::Time.configured.at(self)
::Time.zone.at(self)
end

# Is the float a number?
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Integer
#
# @return [ Time | ActiveSupport::TimeWithZone ] The time.
def __mongoize_time__
::Time.configured.at(self)
::Time.zone.at(self)
end

# Is the integer a number?
Expand Down
18 changes: 8 additions & 10 deletions lib/mongoid/extensions/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,17 @@ def __mongoize_object_id__
# "2012-01-01".__mongoize_time__
# # => 2012-01-01 00:00:00 -0500
#
# @raise [ ArgumentError ] The string is not a valid time string.
#
# @return [ Time | ActiveSupport::TimeWithZone ] Local time in the
# configured default time zone corresponding to this string.
def __mongoize_time__
# This extra parse from Time is because ActiveSupport::TimeZone
# either returns nil or Time.now if the string is empty or invalid,
# which is a regression from pre-3.0 and also does not agree with
# the core Time API.
parsed = ::Time.parse(self)
if ::Time == ::Time.configured
parsed
else
::Time.configured.parse(self)
end
# This extra Time.parse is required to raise an error if the string
# is not a valid time string. ActiveSupport::TimeZone does not
# perform this check.
::Time.parse(self)

::Time.zone.parse(self)
end

# Convert the string to a collection friendly name.
Expand Down
13 changes: 0 additions & 13 deletions lib/mongoid/extensions/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,6 @@ def mongoize

module ClassMethods

# Get the configured time to use when converting - either the time zone
# or the time.
#
# @example Get the configured time.
# ::Time.configured
#
# @return [ Time ] The configured time.
#
# @deprecated
def configured
::Time.zone || ::Time
end

# Convert the object from its mongo friendly ruby type to this type.
#
# @example Demongoize the object.
Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/timestamps/created.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ module Created
# person.set_created_at
def set_created_at
if !timeless? && !created_at
time = Time.configured.now
self.updated_at = time if is_a?(Updated) && !updated_at_changed?
self.created_at = time
now = Time.current
self.updated_at = now if is_a?(Updated) && !updated_at_changed?
self.created_at = now
end
clear_timeless_option
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/timestamps/updated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Updated
# person.set_updated_at
def set_updated_at
if able_to_set_updated_at?
self.updated_at = Time.configured.now unless updated_at_changed?
self.updated_at = Time.current unless updated_at_changed?
end

clear_timeless_option
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/touchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def touch(field = nil)
return false if _root.new_record?

begin
touches = _gather_touch_updates(Time.configured.now, field)
touches = _gather_touch_updates(Time.current, field)
_root.send(:persist_atomic_operations, '$set' => touches) if touches.present?
_run_touch_callbacks_from_root
ensure
Expand Down
1 change: 0 additions & 1 deletion spec/integration/criteria/raw_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require 'spec_helper'

describe 'Queries with Mongoid::RawValue criteria' do
before { Time.zone = 'UTC'}
let(:now_utc) { Time.utc(2020, 1, 1, 16, 0, 0, 0) }
let(:today) { Date.new(2020, 1, 1) }

Expand Down
2 changes: 1 addition & 1 deletion spec/mongoid/findable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@
time_zone_override "Asia/Kolkata"

let!(:time) do
Time.zone.now.tap do |t|
Time.current.tap do |t|
User.create!(last_login: t, name: 'Tom')
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ class Query
inflect.singular("address_components", "address_component")
end

I18n.config.enforce_available_locales = false
Time.zone = 'UTC'

I18n.config.enforce_available_locales = false

if %w(yes true 1).include?((ENV['TEST_I18N_FALLBACKS'] || '').downcase)
require "i18n/backend/fallbacks"
Expand Down
7 changes: 2 additions & 5 deletions spec/support/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ def persistence_context_override(component, value)
end
end

def time_zone_override(tz)
def time_zone_override(time_zone)
around do |example|
old_tz = Time.zone
Time.zone = tz
example.run
Time.zone = old_tz
Time.use_zone(time_zone) { example.run }
end
end

Expand Down
8 changes: 1 addition & 7 deletions spec/support/shared/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
# rubocop:todo all

shared_context 'setting ActiveSupport time zone' do
before do
Time.zone = "Tokyo"
end

after do
Time.zone = nil
end
time_zone_override 'Tokyo'
end

shared_examples_for 'mongoizes to AS::TimeWithZone' do
Expand Down

0 comments on commit f4b29ce

Please sign in to comment.