Skip to content

Commit

Permalink
Merge pull request rubocop#485 from koic/fix_false_positive_for_rails…
Browse files Browse the repository at this point in the history
…_time_zone

[Fix rubocop#70] Fix a false positive for `Rails/TimeZone`
  • Loading branch information
koic authored May 14, 2021
2 parents c893de1 + eef9625 commit ad4fb01
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#482](https://github.com/rubocop/rubocop-rails/pull/482): Fix a false positive for `Rails/RelativeDateConstant` when assigning (hashes/arrays/etc)-containing procs to a constant. ([@jdelStrother][])
* [#419](https://github.com/rubocop/rubocop-rails/issues/419): Fix an error for `Rails/UniqueValidationWithoutIndex` when using a unique index and `check_constraint` that has `nil` first argument. ([@koic][])
* [#70](https://github.com/rubocop/rubocop-rails/issues/70): Fix a false positive for `Rails/TimeZone` when setting `EnforcedStyle: strict` and using `Time.current`. ([@koic][])

### Changes

Expand Down
32 changes: 13 additions & 19 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4366,50 +4366,44 @@ This cop checks for the use of Time methods without zone.
Built on top of Ruby on Rails style guide (https://rails.rubystyle.guide#time)
and the article http://danilenko.org/2012/7/6/rails_timezones/

Two styles are supported for this cop. When EnforcedStyle is 'strict'
then only use of Time.zone is allowed.
Two styles are supported for this cop. When `EnforcedStyle` is 'strict'
then only use of `Time.zone` is allowed.

When EnforcedStyle is 'flexible' then it's also allowed
to use Time.in_time_zone.
to use `Time#in_time_zone`.

=== Examples

==== EnforcedStyle: strict

[source,ruby]
----
# `strict` means that `Time` should be used with `zone`.
# bad
Time.now
Time.parse('2015-03-02T19:05:37')
# bad
Time.current
Time.at(timestamp).in_time_zone
# good
Time.current
Time.zone.now
Time.zone.parse('2015-03-02T19:05:37')
Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier.
----

==== EnforcedStyle: flexible (default)
==== EnforcedStyle: strict

[source,ruby]
----
# `flexible` allows usage of `in_time_zone` instead of `zone`.
# `strict` means that `Time` should be used with `zone`.
# bad
Time.now
Time.parse('2015-03-02T19:05:37')
Time.at(timestamp).in_time_zone
----

# good
Time.zone.now
Time.zone.parse('2015-03-02T19:05:37')
==== EnforcedStyle: flexible (default)

[source,ruby]
----
# `flexible` allows usage of `in_time_zone` instead of `zone`.
# good
Time.current
Time.at(timestamp).in_time_zone
----

Expand Down
30 changes: 11 additions & 19 deletions lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,33 @@ module Rails
# Built on top of Ruby on Rails style guide (https://rails.rubystyle.guide#time)
# and the article http://danilenko.org/2012/7/6/rails_timezones/
#
# Two styles are supported for this cop. When EnforcedStyle is 'strict'
# then only use of Time.zone is allowed.
# Two styles are supported for this cop. When `EnforcedStyle` is 'strict'
# then only use of `Time.zone` is allowed.
#
# When EnforcedStyle is 'flexible' then it's also allowed
# to use Time.in_time_zone.
#
# @example EnforcedStyle: strict
# # `strict` means that `Time` should be used with `zone`.
# to use `Time#in_time_zone`.
#
# @example
# # bad
# Time.now
# Time.parse('2015-03-02T19:05:37')
#
# # bad
# Time.current
# Time.at(timestamp).in_time_zone
#
# # good
# Time.current
# Time.zone.now
# Time.zone.parse('2015-03-02T19:05:37')
# Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier.
#
# @example EnforcedStyle: flexible (default)
# # `flexible` allows usage of `in_time_zone` instead of `zone`.
# @example EnforcedStyle: strict
# # `strict` means that `Time` should be used with `zone`.
#
# # bad
# Time.now
# Time.parse('2015-03-02T19:05:37')
# Time.at(timestamp).in_time_zone
#
# # good
# Time.zone.now
# Time.zone.parse('2015-03-02T19:05:37')
# @example EnforcedStyle: flexible (default)
# # `flexible` allows usage of `in_time_zone` instead of `zone`.
#
# # good
# Time.current
# Time.at(timestamp).in_time_zone
class TimeZone < Base
include ConfigurableEnforcedStyle
Expand All @@ -59,7 +51,7 @@ class TimeZone < Base

GOOD_METHODS = %i[zone zone_default find_zone find_zone!].freeze

DANGEROUS_METHODS = %i[now local new parse at current].freeze
DANGEROUS_METHODS = %i[now local new parse at].freeze

ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601
jisx0301 rfc3339 httpdate to_i to_f].freeze
Expand Down
26 changes: 6 additions & 20 deletions spec/rubocop/cop/rails/time_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
RUBY
end

it 'registers an offense for Time.current' do
expect_offense(<<~RUBY)
Time.current
^^^^^^^ Do not use `Time.current` without zone. Use `Time.zone.now` instead.
RUBY
end

it 'registers an offense for Time.new without argument' do
expect_offense(<<~RUBY)
Time.new
Expand Down Expand Up @@ -104,19 +97,6 @@
RUBY
end
end

describe '.current' do
it 'corrects the error' do
expect_offense(<<~RUBY)
Time.current
^^^^^^^ Do not use `Time.current` without zone. Use `Time.zone.now` instead.
RUBY

expect_correction(<<~RUBY)
Time.zone.now
RUBY
end
end
end

it 'registers an offense for Time.parse' do
Expand Down Expand Up @@ -179,6 +159,12 @@
RUBY
end

it 'accepts Time.current' do
expect_no_offenses(<<~RUBY)
Time.current
RUBY
end

it 'accepts Time.zone.now' do
expect_no_offenses('Time.zone.now')
end
Expand Down

0 comments on commit ad4fb01

Please sign in to comment.