Skip to content

Commit 9bf85ea

Browse files
authored
Merge pull request #622 from agrobbin/duration-arithmetic-months-years
Add months and years to `Rails/DurationArithmetic` cop duration method set
2 parents 0a90255 + cb61c72 commit 9bf85ea

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#622](https://github.com/rubocop/rubocop-rails/pull/622): Add `month(s)` and `year(s)` to `Rails/DurationArithmetic` cop. ([@agrobbin][])

lib/rubocop/cop/rails/duration_arithmetic.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class DurationArithmetic < Base
2626
RESTRICT_ON_SEND = %i[+ -].freeze
2727

2828
DURATIONS = Set[:second, :seconds, :minute, :minutes, :hour, :hours,
29-
:day, :days, :week, :weeks, :fortnight, :fortnights]
29+
:day, :days, :week, :weeks, :fortnight, :fortnights,
30+
:month, :months, :year, :years]
3031

3132
# @!method duration_arithmetic_argument?(node)
3233
# Match duration subtraction or addition with current time.

spec/rubocop/cop/rails/duration_arithmetic_spec.rb

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,85 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Rails::DurationArithmetic, :config do
4-
it 'registers an offense and corrects' do
4+
it 'registers an offense and corrects Time.zone.now instances' do
55
expect_offense(<<~RUBY)
6-
Time.zone.now - 1.minute
6+
Time.zone.now - 1.second
77
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
8+
Time.zone.now - 2.seconds
9+
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
10+
Time.zone.now + 1.second
11+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
12+
Time.zone.now + 2.seconds
13+
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
14+
RUBY
15+
16+
expect_correction(<<~RUBY)
17+
1.second.ago
18+
2.seconds.ago
19+
1.second.from_now
20+
2.seconds.from_now
21+
RUBY
22+
end
23+
24+
it 'registers an offense and corrects all duration arithmetic methods' do
25+
expect_offense(<<~RUBY)
26+
Time.current - 1.second
27+
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
28+
Time.current - 2.seconds
29+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
30+
Time.current + 1.second
31+
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
32+
Time.current + 2.seconds
33+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
34+
Time.current + 1.minute
35+
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
36+
Time.current + 2.minutes
37+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
38+
Time.current + 1.hour
39+
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
40+
Time.current + 2.hours
41+
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
42+
Time.current + 1.day
43+
^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
844
Time.current + 2.days
945
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
10-
::Time.current + 1.hour
46+
Time.current + 1.week
47+
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
48+
Time.current + 2.weeks
49+
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
50+
Time.current + 1.fortnight
51+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
52+
Time.current + 2.fortnights
53+
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
54+
Time.current + 1.month
55+
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
56+
Time.current + 2.months
1157
^^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
58+
Time.current + 1.year
59+
^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
60+
Time.current + 2.years
61+
^^^^^^^^^^^^^^^^^^^^^^ Do not add or subtract duration.
1262
RUBY
1363

1464
expect_correction(<<~RUBY)
15-
1.minute.ago
16-
2.days.from_now
65+
1.second.ago
66+
2.seconds.ago
67+
1.second.from_now
68+
2.seconds.from_now
69+
1.minute.from_now
70+
2.minutes.from_now
1771
1.hour.from_now
72+
2.hours.from_now
73+
1.day.from_now
74+
2.days.from_now
75+
1.week.from_now
76+
2.weeks.from_now
77+
1.fortnight.from_now
78+
2.fortnights.from_now
79+
1.month.from_now
80+
2.months.from_now
81+
1.year.from_now
82+
2.years.from_now
1883
RUBY
1984
end
2085

0 commit comments

Comments
 (0)