From f47f6efcd029a4a8d0b46c75350b93044a387b02 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Tue, 1 Aug 2023 10:04:46 +0900 Subject: [PATCH] Safely autocorrect Rails/DurationArithmetic Inspecting 1481 files ...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................C.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................C.C.................................................................................................................................................................................................................................................................................C..............C..........................................................C...............................C....C..........CC................................................................ Offenses: app/services/create_order_cycle.rb:41:24: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. orders_close_at: Time.zone.now + 1.month ^^^^^^^^^^^^^^^^^^^^^^^ spec/jobs/order_cycle_closing_job_spec.rb:7:74: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. create(:order_cycle, automatic_notifications: true, orders_close_at: Time.zone.now - 1.minute) ^^^^^^^^^^^^^^^^^^^^^^^^ spec/jobs/order_cycle_closing_job_spec.rb:10:74: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. create(:order_cycle, automatic_notifications: true, orders_close_at: Time.zone.now + 1.minute) ^^^^^^^^^^^^^^^^^^^^^^^^ spec/jobs/order_cycle_closing_job_spec.rb:13:75: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. create(:order_cycle, automatic_notifications: false, orders_close_at: Time.zone.now - 1.minute) ^^^^^^^^^^^^^^^^^^^^^^^^ spec/jobs/order_cycle_opened_job_spec.rb:7:42: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. create(:order_cycle, orders_open_at: Time.zone.now - 1.hour) ^^^^^^^^^^^^^^^^^^^^^^ spec/jobs/order_cycle_opened_job_spec.rb:13:42: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. create(:order_cycle, orders_open_at: Time.zone.now + 1.minute) ^^^^^^^^^^^^^^^^^^^^^^^^ spec/services/permissions/order_spec.rb:26:58: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. completed_at: Time.zone.now - 1.year) ^^^^^^^^^^^^^^^^^^^^^^ spec/services/terms_of_service_spec.rb:21:71: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. allow(customer).to receive(:terms_and_conditions_accepted_at) { Time.zone.now - 1.week } ^^^^^^^^^^^^^^^^^^^^^^ spec/services/terms_of_service_spec.rb:22:59: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. allow(TermsOfServiceFile).to receive(:updated_at) { Time.zone.now - 2.weeks } ^^^^^^^^^^^^^^^^^^^^^^^ spec/services/terms_of_service_spec.rb:36:71: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. allow(customer).to receive(:terms_and_conditions_accepted_at) { Time.zone.now - 1.week } ^^^^^^^^^^^^^^^^^^^^^^ spec/services/terms_of_service_spec.rb:38:45: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. ActiveStorage::Blob.new(created_at: Time.zone.now - 2.weeks) ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/bulk_order_management_spec.rb:647:57: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. completed_at: Time.zone.now + 1.week, ^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/bulk_order_management_spec.rb:652:57: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. completed_at: Time.zone.now + 2.weeks, ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/bulk_order_management_spec.rb:661:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. oc3.update!(orders_close_at: Time.zone.now + 2.weeks) ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/bulk_order_management_spec.rb:662:39: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. oc3.update!(orders_open_at: Time.zone.now + 1.week) ^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/order_cycles/list_spec.rb:128:18: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. oc_open_at = Time.zone.now - 2.weeks ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/order_cycles/list_spec.rb:129:19: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. oc_close_at = Time.zone.now + 2.weeks ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/orders_spec.rb:289:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. order2.update!(completed_at: Time.zone.now - 2.weeks) ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/orders_spec.rb:290:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. order3.update!(completed_at: Time.zone.now - 3.weeks) ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/orders_spec.rb:291:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. order4.update!(completed_at: Time.zone.now - 4.weeks) ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/orders_spec.rb:292:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. order5.update!(completed_at: Time.zone.now - 5.weeks) ^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/reports/orders_and_fulfillment_spec.rb:113:31: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. let(:completed_at1) { Time.zone.now - 1500.hours } # 1500 hours in the past ^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/reports/orders_and_fulfillment_spec.rb:114:31: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. let(:completed_at2) { Time.zone.now - 1700.hours } # 1700 hours in the past ^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/reports/orders_and_fulfillment_spec.rb:115:33: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. let(:datetime_start1) { Time.zone.now - 1600.hours } # 1600 hours in the past ^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/reports/orders_and_fulfillment_spec.rb:116:33: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. let(:datetime_start2) { Time.zone.now - 1800.hours } # 1600 hours in the past ^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/reports/orders_and_fulfillment_spec.rb:117:30: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. let(:datetime_end) { Time.zone.now - 1400.hours } # 1400 hours in the past ^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/system/admin/reports/packing_report_spec.rb:13:26: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a method call. let!(:open_datetime) { (1.month.ago).strftime("%Y-%m-%d 00:00") } ^^^^^^^^^^^^^ spec/system/admin/reports/packing_report_spec.rb:13:27: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration. let!(:open_datetime) { (Time.zone.now - 1.month).strftime("%Y-%m-%d 00:00") } ^^^^^^^^^^^^^^^^^^^^^^^ 1481 files inspected, 28 offenses detected, 28 offenses corrected --- .rubocop_todo.yml | 15 --------------- app/services/create_order_cycle.rb | 2 +- spec/jobs/order_cycle_closing_job_spec.rb | 6 +++--- spec/jobs/order_cycle_opened_job_spec.rb | 4 ++-- spec/services/permissions/order_spec.rb | 2 +- spec/services/terms_of_service_spec.rb | 8 ++++---- spec/system/admin/bulk_order_management_spec.rb | 8 ++++---- spec/system/admin/order_cycles/list_spec.rb | 4 ++-- spec/system/admin/orders_spec.rb | 8 ++++---- .../admin/reports/orders_and_fulfillment_spec.rb | 10 +++++----- spec/system/admin/reports/packing_report_spec.rb | 2 +- 11 files changed, 27 insertions(+), 42 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ab39868802..b9fe1e8e41 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -478,21 +478,6 @@ Rails/CompactBlank: - 'lib/reporting/report_ruler.rb' - 'lib/reporting/reports/enterprise_fee_summary/parameters.rb' -# Offense count: 27 -# This cop supports safe autocorrection (--autocorrect). -Rails/DurationArithmetic: - Exclude: - - 'app/services/create_order_cycle.rb' - - 'spec/jobs/order_cycle_closing_job_spec.rb' - - 'spec/jobs/order_cycle_opened_job_spec.rb' - - 'spec/services/permissions/order_spec.rb' - - 'spec/services/terms_of_service_spec.rb' - - 'spec/system/admin/bulk_order_management_spec.rb' - - 'spec/system/admin/order_cycles/list_spec.rb' - - 'spec/system/admin/orders_spec.rb' - - 'spec/system/admin/reports/orders_and_fulfillment_spec.rb' - - 'spec/system/admin/reports/packing_report_spec.rb' - # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). Rails/ExpandedDateRange: diff --git a/app/services/create_order_cycle.rb b/app/services/create_order_cycle.rb index c8f9ce1b18..50900f1c88 100644 --- a/app/services/create_order_cycle.rb +++ b/app/services/create_order_cycle.rb @@ -38,7 +38,7 @@ class CreateOrderCycle coordinator_id: enterprise.id, name: 'Monthly order cycle', orders_open_at: Time.zone.now, - orders_close_at: Time.zone.now + 1.month + orders_close_at: 1.month.from_now ) end diff --git a/spec/jobs/order_cycle_closing_job_spec.rb b/spec/jobs/order_cycle_closing_job_spec.rb index 1e4ebcf61a..1495fd2111 100644 --- a/spec/jobs/order_cycle_closing_job_spec.rb +++ b/spec/jobs/order_cycle_closing_job_spec.rb @@ -4,13 +4,13 @@ require 'spec_helper' describe OrderCycleClosingJob do let(:order_cycle1) { - create(:order_cycle, automatic_notifications: true, orders_close_at: Time.zone.now - 1.minute) + create(:order_cycle, automatic_notifications: true, orders_close_at: 1.minute.ago) } let(:order_cycle2) { - create(:order_cycle, automatic_notifications: true, orders_close_at: Time.zone.now + 1.minute) + create(:order_cycle, automatic_notifications: true, orders_close_at: 1.minute.from_now) } let(:order_cycle3) { - create(:order_cycle, automatic_notifications: false, orders_close_at: Time.zone.now - 1.minute) + create(:order_cycle, automatic_notifications: false, orders_close_at: 1.minute.ago) } it "sends notifications for recently closed order cycles with automatic notifications enabled" do diff --git a/spec/jobs/order_cycle_opened_job_spec.rb b/spec/jobs/order_cycle_opened_job_spec.rb index 142cc01b0e..948abf5029 100644 --- a/spec/jobs/order_cycle_opened_job_spec.rb +++ b/spec/jobs/order_cycle_opened_job_spec.rb @@ -4,13 +4,13 @@ require 'spec_helper' describe OrderCycleOpenedJob do let(:oc_opened_before) { - create(:order_cycle, orders_open_at: Time.zone.now - 1.hour) + create(:order_cycle, orders_open_at: 1.hour.ago) } let(:oc_opened_now) { create(:order_cycle, orders_open_at: Time.zone.now) } let(:oc_opening_soon) { - create(:order_cycle, orders_open_at: Time.zone.now + 1.minute) + create(:order_cycle, orders_open_at: 1.minute.from_now) } it "enqueues jobs for recently opened order cycles only" do diff --git a/spec/services/permissions/order_spec.rb b/spec/services/permissions/order_spec.rb index bd664d9140..7eec197dbe 100644 --- a/spec/services/permissions/order_spec.rb +++ b/spec/services/permissions/order_spec.rb @@ -23,7 +23,7 @@ module Permissions } let(:order_from_last_year) { create(:completed_order_with_totals, order_cycle: order_cycle, distributor: distributor, - completed_at: Time.zone.now - 1.year) + completed_at: 1.year.ago) } before { allow(OpenFoodNetwork::Permissions).to receive(:new) { basic_permissions } } diff --git a/spec/services/terms_of_service_spec.rb b/spec/services/terms_of_service_spec.rb index 514f9dfb50..e6e78e4101 100644 --- a/spec/services/terms_of_service_spec.rb +++ b/spec/services/terms_of_service_spec.rb @@ -18,8 +18,8 @@ describe TermsOfService do context "a customer has accepted the platform terms of service" do before do - allow(customer).to receive(:terms_and_conditions_accepted_at) { Time.zone.now - 1.week } - allow(TermsOfServiceFile).to receive(:updated_at) { Time.zone.now - 2.weeks } + allow(customer).to receive(:terms_and_conditions_accepted_at) { 1.week.ago } + allow(TermsOfServiceFile).to receive(:updated_at) { 2.weeks.ago } end it "should reflect whether the platform TOS have been accepted since the last update" do @@ -33,9 +33,9 @@ describe TermsOfService do context "a customer has accepted the distributor terms of service" do before do - allow(customer).to receive(:terms_and_conditions_accepted_at) { Time.zone.now - 1.week } + allow(customer).to receive(:terms_and_conditions_accepted_at) { 1.week.ago } allow(distributor).to receive(:terms_and_conditions_blob) { - ActiveStorage::Blob.new(created_at: Time.zone.now - 2.weeks) + ActiveStorage::Blob.new(created_at: 2.weeks.ago) } end diff --git a/spec/system/admin/bulk_order_management_spec.rb b/spec/system/admin/bulk_order_management_spec.rb index 3abeaa61ed..e2f6940ee9 100644 --- a/spec/system/admin/bulk_order_management_spec.rb +++ b/spec/system/admin/bulk_order_management_spec.rb @@ -644,12 +644,12 @@ describe ' } let!(:o3) { create(:order_with_distributor, state: 'complete', shipment_state: 'ready', - completed_at: Time.zone.now + 1.week, + completed_at: 1.week.from_now, order_cycle: oc3 ) } let!(:o4) { create(:order_with_distributor, state: 'complete', shipment_state: 'ready', - completed_at: Time.zone.now + 2.weeks, + completed_at: 2.weeks.from_now, order_cycle: oc3 ) } let!(:li1) { create(:line_item_with_shipment, order: o1 ) } @@ -658,8 +658,8 @@ describe ' let!(:li4) { create(:line_item_with_shipment, order: o4 ) } before do - oc3.update!(orders_close_at: Time.zone.now + 2.weeks) - oc3.update!(orders_open_at: Time.zone.now + 1.week) + oc3.update!(orders_close_at: 2.weeks.from_now) + oc3.update!(orders_open_at: 1.week.from_now) visit_bulk_order_management end diff --git a/spec/system/admin/order_cycles/list_spec.rb b/spec/system/admin/order_cycles/list_spec.rb index 0db9266341..027cbb2ebc 100644 --- a/spec/system/admin/order_cycles/list_spec.rb +++ b/spec/system/admin/order_cycles/list_spec.rb @@ -125,8 +125,8 @@ describe ' end describe 'listing order cycles with other locales' do - oc_open_at = Time.zone.now - 2.weeks - oc_close_at = Time.zone.now + 2.weeks + oc_open_at = 2.weeks.ago + oc_close_at = 2.weeks.from_now let!(:oc_pt) { create(:simple_order_cycle, name: 'oc', orders_open_at: oc_open_at, orders_close_at: oc_close_at) diff --git a/spec/system/admin/orders_spec.rb b/spec/system/admin/orders_spec.rb index 2696a84687..f0fa036cbd 100644 --- a/spec/system/admin/orders_spec.rb +++ b/spec/system/admin/orders_spec.rb @@ -286,10 +286,10 @@ describe ' describe "ordering" do context "orders with different completion dates" do before do - order2.update!(completed_at: Time.zone.now - 2.weeks) - order3.update!(completed_at: Time.zone.now - 3.weeks) - order4.update!(completed_at: Time.zone.now - 4.weeks) - order5.update!(completed_at: Time.zone.now - 5.weeks) + order2.update!(completed_at: 2.weeks.ago) + order3.update!(completed_at: 3.weeks.ago) + order4.update!(completed_at: 4.weeks.ago) + order5.update!(completed_at: 5.weeks.ago) login_as_admin visit spree.admin_orders_path end diff --git a/spec/system/admin/reports/orders_and_fulfillment_spec.rb b/spec/system/admin/reports/orders_and_fulfillment_spec.rb index 9a6642405a..977ea12a82 100644 --- a/spec/system/admin/reports/orders_and_fulfillment_spec.rb +++ b/spec/system/admin/reports/orders_and_fulfillment_spec.rb @@ -110,11 +110,11 @@ describe "Orders And Fulfillment" do end context "with two orders on the same day at different times" do - let(:completed_at1) { Time.zone.now - 1500.hours } # 1500 hours in the past - let(:completed_at2) { Time.zone.now - 1700.hours } # 1700 hours in the past - let(:datetime_start1) { Time.zone.now - 1600.hours } # 1600 hours in the past - let(:datetime_start2) { Time.zone.now - 1800.hours } # 1600 hours in the past - let(:datetime_end) { Time.zone.now - 1400.hours } # 1400 hours in the past + let(:completed_at1) { 1500.hours.ago } # 1500 hours in the past + let(:completed_at2) { 1700.hours.ago } # 1700 hours in the past + let(:datetime_start1) { 1600.hours.ago } # 1600 hours in the past + let(:datetime_start2) { 1800.hours.ago } # 1600 hours in the past + let(:datetime_end) { 1400.hours.ago } # 1400 hours in the past before do Timecop.travel(completed_at1) { order1.finalize! } Timecop.travel(completed_at2) { order2.finalize! } diff --git a/spec/system/admin/reports/packing_report_spec.rb b/spec/system/admin/reports/packing_report_spec.rb index 31e08cf1e5..da86e57936 100644 --- a/spec/system/admin/reports/packing_report_spec.rb +++ b/spec/system/admin/reports/packing_report_spec.rb @@ -10,7 +10,7 @@ describe "Packing Reports" do Timecop.freeze(Time.zone.now.strftime("%Y-%m-%d 00:00")) { example.run } end - let!(:open_datetime) { (Time.zone.now - 1.month).strftime("%Y-%m-%d 00:00") } + let!(:open_datetime) { 1.month.ago.strftime("%Y-%m-%d 00:00") } let!(:close_datetime) { Time.zone.now.strftime("%Y-%m-%d 00:00") } describe "Packing reports" do