Skip to content

Commit

Permalink
style(Rails/TimeZone): manual
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsavitskiy committed May 4, 2023
1 parent 61ab07e commit dc6fb4a
Show file tree
Hide file tree
Showing 42 changed files with 161 additions and 199 deletions.
45 changes: 0 additions & 45 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/associations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
```````````````````````````````````````````````
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/crud.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/mongoid/criteria/queryable/extensions/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module String
#
# @return [ Time ] The time at UTC midnight.
def __evolve_date__
time = ::Time.parse(self)
time = ::Time.zone.parse(self)
return nil if time.nil?

::Time.utc(time.year, time.month, time.day, 0, 0, 0, 0)
end

Expand All @@ -26,6 +28,8 @@ def __evolve_date__
#
# @return [ Time ] The string as a time.
def __evolve_time__
return nil if __evolve_date__.nil?

__mongoize_time__.utc
end

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 @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/extensions/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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 @@ -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?
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 @@ -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?
Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/extensions/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/extensions/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module ClassMethods
# or the time.
#
# @example Get the configured time.
# ::Time.configured
# ::Time.zone
#
# @return [ Time ] The configured time.
#
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/timestamps/created.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/touchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/criteria/date_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
33 changes: 16 additions & 17 deletions spec/integration/criteria/raw_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -405,67 +404,67 @@
context 'Mongoid::RawValue<Time>' do
let!(:band7) do
id = BSON::ObjectId.new
Band.collection.insert_one(_id: id, name: Time.at(1), likes: Time.at(1), rating: Time.at(3.1), sales: Time.at(310), decibels: Time.at(90))
Band.collection.insert_one(_id: id, name: Time.zone.at(1), likes: Time.zone.at(1), rating: Time.zone.at(3.1), sales: Time.zone.at(310), decibels: Time.zone.at(90))
Band.find(id)
end

context 'Integer field' do
it 'does not match objects with raw value' do
expect(Band.where(likes: Mongoid::RawValue(Time.at(1))).to_a).to eq [band7]
expect(Band.where(likes: Mongoid::RawValue(Time.zone.at(1))).to_a).to eq [band7]
end

it 'matches objects without raw value' do
expect(Band.where(likes: Time.at(1)).to_a).to eq [band2, band3]
expect(Band.where(likes: Time.zone.at(1)).to_a).to eq [band2, band3]
end
end

context 'Float field' do
it 'matches objects with raw value' do
expect(Band.where(rating: Mongoid::RawValue(Time.at(3.1))).to_a).to eq [band7]
expect(Band.where(rating: Mongoid::RawValue(Time.zone.at(3.1))).to_a).to eq [band7]
end

it 'matches objects without raw value' do
expect(Band.where(rating: Time.at(3.1)).to_a).to eq [band4, band5]
expect(Band.where(rating: Time.zone.at(3.1)).to_a).to eq [band4, band5]
end
end

context 'BigDecimal field' do
it 'matches objects with raw value' do
expect(Band.where(sales: Mongoid::RawValue(Time.at(310))).to_a).to eq [band7]
expect(Band.where(sales: Mongoid::RawValue(Time.zone.at(310))).to_a).to eq [band7]
end

it 'matches objects without raw value because Time does not evolve into BigDecimal' do
expect(Band.where(sales: Time.at(310)).to_a).to eq [band7]
expect(Band.where(sales: Time.zone.at(310)).to_a).to eq [band7]
end
end

context 'String field' do
it 'matches objects with raw value' do
expect(Band.where(name: Mongoid::RawValue(Time.at(1))).to_a).to eq [band7]
expect(Band.where(name: Mongoid::RawValue(Time.zone.at(1))).to_a).to eq [band7]
end

it 'does not match objects without raw value' do
expect(Band.where(name: Time.at(1)).to_a).to eq []
expect(Band.where(name: Time.zone.at(1)).to_a).to eq []
end
end

context 'Range field' do
it 'matches objects with raw value' do
expect(Band.where(decibels: Mongoid::RawValue(Time.at(90))).to_a).to eq [band7]
expect(Band.where(decibels: Mongoid::RawValue(Time.zone.at(90))).to_a).to eq [band7]
end

it 'matches objects without raw value because BigDecimal cannot be evolved to Range' do
expect(Band.where(decibels: Time.at(90)).to_a).to eq [band7]
expect(Band.where(decibels: Time.zone.at(90)).to_a).to eq [band7]
end
end

context 'Date field' do
it 'matches objects with raw value when exact' do
expect(Band.where(founded: Mongoid::RawValue(Time.at(1577923200))).to_a).to eq [band3, band4]
expect(Band.where(founded: Mongoid::RawValue(Time.zone.at(1577923200))).to_a).to eq [band3, band4]
end

it 'does not match objects with raw value when non-exact' do
expect(Band.where(founded: Mongoid::RawValue(Time.at(1577923199))).to_a).to eq []
expect(Band.where(founded: Mongoid::RawValue(Time.zone.at(1577923199))).to_a).to eq []
end

it 'matches objects without raw value when exact' do
Expand All @@ -483,17 +482,17 @@

context 'Time field' do
it 'matches objects with raw value' do
expect(Band.where(updated: Mongoid::RawValue(Time.at(1578153600))).to_a).to eq [band4, band5]
expect(Band.where(updated: Mongoid::RawValue(Time.zone.at(1578153600))).to_a).to eq [band4, band5]
end

it 'matches objects without raw value' do
expect(Band.where(updated: Time.at(1578153600)).to_a).to eq [band4, band5]
expect(Band.where(updated: Time.zone.at(1578153600)).to_a).to eq [band4, band5]
end
end
end

context 'Mongoid::RawValue<Date>' do
let!(:band7) { Band.create!(updated: Time.at(1577923200)) }
let!(:band7) { Band.create!(updated: Time.zone.at(1577923200)) }

context 'Date field' do
it 'matches objects with raw value' do
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/persistence/range_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
subject { person.send(field) }

let!(:person) { Person.create!(field => value).reload }
let(:now_utc) { Time.now }
let(:now_utc) { Time.zone.now }
let(:later_utc) { now_utc + 10.minutes }
let(:now_in_zone) { now_utc.in_time_zone('Asia/Tokyo') }
let(:later_in_zone) { later_utc.in_time_zone('Asia/Tokyo') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,12 +1076,12 @@ class OtherHasManyRightObject; end
end

context 'when adding an object to the association' do
let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
let!(:start_time) { Timecop.freeze(Time.zone.at(Time.now.to_i)) }
let!(:school) { HabtmmSchool.create! }
let!(:student) { HabtmmStudent.create! }

let(:update_time) do
Timecop.freeze(Time.at(Time.now.to_i) + 2)
Timecop.freeze(Time.zone.at(Time.now.to_i) + 2)
end

after do
Expand Down
4 changes: 2 additions & 2 deletions spec/mongoid/association/referenced/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1249,12 +1249,12 @@ class OtherBelongingObject; end
end

context 'when adding an object to the association' do
let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
let!(:start_time) { Timecop.freeze(Time.zone.at(Time.now.to_i)) }
let!(:school) { HmmSchool.create! }
let!(:student) { HmmStudent.create! }

let(:update_time) do
Timecop.freeze(Time.at(Time.now.to_i) + 2)
Timecop.freeze(Time.zone.at(Time.now.to_i) + 2)
end

after do
Expand Down
Loading

0 comments on commit dc6fb4a

Please sign in to comment.