diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8e3ab2c5a..ed06f0769 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -756,51 +756,6 @@ Rails/SkipsModelValidations: - 'spec/mongoid/touchable_spec.rb' - 'spec/support/models/server.rb' -# Offense count: 159 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: strict, flexible -Rails/TimeZone: - Exclude: - - 'lib/mongoid/criteria/queryable/extensions/date.rb' - - 'lib/mongoid/criteria/queryable/extensions/string.rb' - - 'lib/mongoid/extensions/array.rb' - - 'lib/mongoid/extensions/date.rb' - - 'lib/mongoid/extensions/float.rb' - - 'lib/mongoid/extensions/integer.rb' - - 'lib/mongoid/extensions/string.rb' - - 'lib/mongoid/extensions/time.rb' - - 'lib/mongoid/timestamps/created.rb' - - 'lib/mongoid/timestamps/updated.rb' - - 'lib/mongoid/touchable.rb' - - 'spec/integration/criteria/date_field_spec.rb' - - 'spec/integration/criteria/raw_value_spec.rb' - - 'spec/integration/persistence/range_field_spec.rb' - - 'spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb' - - 'spec/mongoid/association/referenced/has_many_spec.rb' - - 'spec/mongoid/attributes/readonly_spec.rb' - - 'spec/mongoid/attributes_spec.rb' - - 'spec/mongoid/copyable_spec.rb' - - 'spec/mongoid/criteria/queryable/extensions/array_spec.rb' - - 'spec/mongoid/criteria/queryable/extensions/date_spec.rb' - - 'spec/mongoid/criteria/queryable/extensions/date_time_spec.rb' - - 'spec/mongoid/criteria/queryable/extensions/range_spec.rb' - - 'spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb' - - 'spec/mongoid/criteria/queryable/selectable_logical_spec.rb' - - 'spec/mongoid/criteria/queryable/selectable_spec.rb' - - 'spec/mongoid/criteria/queryable/selector_spec.rb' - - 'spec/mongoid/extensions/range_spec.rb' - - 'spec/mongoid/extensions/time_spec.rb' - - 'spec/mongoid/extensions/time_with_zone_spec.rb' - - 'spec/mongoid/mongoizable_spec.rb' - - 'spec/mongoid/persistable/savable_spec.rb' - - 'spec/mongoid/persistable/updatable_spec.rb' - - 'spec/mongoid/timestamps/updated/short_spec.rb' - - 'spec/mongoid/timestamps/updated_spec.rb' - - 'spec/mongoid/timestamps_spec.rb' - - 'spec/mongoid/touchable_spec.rb' - - 'spec/support/models/post.rb' - # Offense count: 5 # Configuration parameters: Include. # Include: spec/**/*.rb, test/**/*.rb diff --git a/docs/reference/associations.txt b/docs/reference/associations.txt index 4cdbd63a2..aef99eaf2 100644 --- a/docs/reference/associations.txt +++ b/docs/reference/associations.txt @@ -589,7 +589,7 @@ help of MongoDB projection operation: # {"find"=>"bands", # "filter"=>{"started_on"=>{"$gt"=>2018-07-01 00:00:00 UTC}}, # "projection"=>{"_id"=>1, "label"=>1}} - Band.where(started_on: {'$gt' => Time.now - 1.year}).only(:label).map(&:label).compact.uniq + Band.where(started_on: {'$gt' => Time.zone.now - 1.year}).only(:label).map(&:label).compact.uniq Setting Stale Values on Referenced Associations ``````````````````````````````````````````````` diff --git a/docs/reference/crud.txt b/docs/reference/crud.txt index bdbdcbfc5..bcdfa78c1 100644 --- a/docs/reference/crud.txt +++ b/docs/reference/crud.txt @@ -455,7 +455,7 @@ are: end post = Post.create! - post.set('metadata.published_at' => Time.now) + post.set('metadata.published_at' => Time.zone.now) post.metadata['published_at'] # => Time instance post.set('metadata.approved.today' => true) diff --git a/lib/mongoid/criteria/queryable/extensions/date.rb b/lib/mongoid/criteria/queryable/extensions/date.rb index 449036d3d..b25fd6b38 100644 --- a/lib/mongoid/criteria/queryable/extensions/date.rb +++ b/lib/mongoid/criteria/queryable/extensions/date.rb @@ -25,7 +25,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 diff --git a/lib/mongoid/criteria/queryable/extensions/string.rb b/lib/mongoid/criteria/queryable/extensions/string.rb index 5ab139eac..a5e06bd50 100644 --- a/lib/mongoid/criteria/queryable/extensions/string.rb +++ b/lib/mongoid/criteria/queryable/extensions/string.rb @@ -15,7 +15,7 @@ module String # # @return [ Time ] The time at UTC midnight. def __evolve_date__ - time = ::Time.parse(self) + time = ::Time.zone.parse(self) ::Time.utc(time.year, time.month, time.day, 0, 0, 0, 0) end diff --git a/lib/mongoid/extensions/array.rb b/lib/mongoid/extensions/array.rb index 62855fd9e..a9c1a31b9 100644 --- a/lib/mongoid/extensions/array.rb +++ b/lib/mongoid/extensions/array.rb @@ -50,7 +50,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 # Checks whether conditions given in this array are known to be diff --git a/lib/mongoid/extensions/date.rb b/lib/mongoid/extensions/date.rb index 22f36367e..da3eace87 100644 --- a/lib/mongoid/extensions/date.rb +++ b/lib/mongoid/extensions/date.rb @@ -16,7 +16,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 @@ -71,7 +71,7 @@ def mongoize(object) begin time = if object.is_a?(String) # https://jira.mongodb.org/browse/MONGOID-4460 - ::Time.parse(object) + ::Time.zone.parse(object) else object.__mongoize_time__ end diff --git a/lib/mongoid/extensions/float.rb b/lib/mongoid/extensions/float.rb index 6a6672128..be3ef4b0e 100644 --- a/lib/mongoid/extensions/float.rb +++ b/lib/mongoid/extensions/float.rb @@ -13,7 +13,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? diff --git a/lib/mongoid/extensions/integer.rb b/lib/mongoid/extensions/integer.rb index 9b6f2b2fa..0334135ce 100644 --- a/lib/mongoid/extensions/integer.rb +++ b/lib/mongoid/extensions/integer.rb @@ -13,7 +13,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? diff --git a/lib/mongoid/extensions/string.rb b/lib/mongoid/extensions/string.rb index d35849824..913bfc02b 100644 --- a/lib/mongoid/extensions/string.rb +++ b/lib/mongoid/extensions/string.rb @@ -44,11 +44,11 @@ def __mongoize_time__ # 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.configured == ::Time + parsed = ::Time.zone.parse(self) + if ::Time.zone == ::Time parsed else - ::Time.configured.parse(self) + ::Time.zone.parse(self) end end diff --git a/lib/mongoid/extensions/time.rb b/lib/mongoid/extensions/time.rb index 1fd34bb09..a65c72ec4 100644 --- a/lib/mongoid/extensions/time.rb +++ b/lib/mongoid/extensions/time.rb @@ -33,7 +33,7 @@ module ClassMethods # or the time. # # @example Get the configured time. - # ::Time.configured + # ::Time.zone # # @return [ Time ] The configured time. # @@ -64,7 +64,7 @@ def demongoize(object) nil end elsif object.is_a?(BSON::Timestamp) - ::Time.at(object.seconds) + ::Time.zone.at(object.seconds) end return if time.nil? diff --git a/lib/mongoid/timestamps/created.rb b/lib/mongoid/timestamps/created.rb index 5b198178c..0a7fd7f24 100644 --- a/lib/mongoid/timestamps/created.rb +++ b/lib/mongoid/timestamps/created.rb @@ -23,7 +23,7 @@ module Created # person.set_created_at def set_created_at if !timeless? && !created_at - time = Time.configured.now + time = Time.zone.now self.updated_at = time if is_a?(Updated) && !updated_at_changed? self.created_at = time end diff --git a/lib/mongoid/timestamps/updated.rb b/lib/mongoid/timestamps/updated.rb index cf298defd..f14237de2 100644 --- a/lib/mongoid/timestamps/updated.rb +++ b/lib/mongoid/timestamps/updated.rb @@ -23,7 +23,7 @@ module Updated # @example Set the updated at time. # person.set_updated_at def set_updated_at - self.updated_at = Time.configured.now if able_to_set_updated_at? && !updated_at_changed? + self.updated_at = Time.zone.now if able_to_set_updated_at? && !updated_at_changed? clear_timeless_option end diff --git a/lib/mongoid/touchable.rb b/lib/mongoid/touchable.rb index 433b68430..b6896d11a 100644 --- a/lib/mongoid/touchable.rb +++ b/lib/mongoid/touchable.rb @@ -51,7 +51,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.zone.now, field) _root.send(:persist_atomic_operations, '$set' => touches) if touches.present? _run_touch_callbacks_from_root ensure diff --git a/spec/integration/criteria/date_field_spec.rb b/spec/integration/criteria/date_field_spec.rb index 7a9a1833c..c40038900 100644 --- a/spec/integration/criteria/date_field_spec.rb +++ b/spec/integration/criteria/date_field_spec.rb @@ -17,7 +17,7 @@ context 'using Time' do let(:arg) do - Time.now.freeze + Time.zone.now.freeze end it_behaves_like 'converts to beginning of day in UTC' diff --git a/spec/integration/criteria/raw_value_spec.rb b/spec/integration/criteria/raw_value_spec.rb index be873646b..7283c3ad6 100644 --- a/spec/integration/criteria/raw_value_spec.rb +++ b/spec/integration/criteria/raw_value_spec.rb @@ -3,7 +3,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) } @@ -405,67 +404,67 @@ context 'Mongoid::RawValue