From c8270d58bdd573695db2279cd44386a8dd78c60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 24 Aug 2022 22:32:43 +0200 Subject: [PATCH] Add and apply Rails/DurationArithmetic rubocop rule This rule was added in rubocop-rails 2.13.0. We were already applying it most of the time. --- .rubocop.yml | 3 +++ db/dev_seeds/banners.rb | 6 +++--- db/dev_seeds/budgets.rb | 4 ++-- db/dev_seeds/debates.rb | 4 ++-- db/dev_seeds/proposals.rb | 6 +++--- db/dev_seeds/users.rb | 2 +- spec/models/dashboard/action_spec.rb | 4 ++-- spec/models/poll/poll_spec.rb | 8 ++++---- spec/requests/dashboard/group_supports_spec.rb | 2 +- spec/system/admin/machine_learning_spec.rb | 4 ++-- spec/system/debates_spec.rb | 4 ++-- spec/system/moderation/budget_investments_spec.rb | 4 ++-- spec/system/moderation/comments_spec.rb | 4 ++-- spec/system/moderation/debates_spec.rb | 4 ++-- spec/system/moderation/proposal_notifications_spec.rb | 6 +++--- spec/system/moderation/proposals_spec.rb | 4 ++-- spec/system/proposals_spec.rb | 4 ++-- spec/system/users_auth_spec.rb | 8 ++++---- 18 files changed, 42 insertions(+), 39 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 30a9dd223..71f0bec9d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -236,6 +236,9 @@ Rails/CreateTableWithTimestamps: Rails/Date: Enabled: true +Rails/DurationArithmetic: + Enabled: true + Rails/DynamicFindBy: Enabled: true Whitelist: diff --git a/db/dev_seeds/banners.rb b/db/dev_seeds/banners.rb index af16f01ca..d34078a7b 100644 --- a/db/dev_seeds/banners.rb +++ b/db/dev_seeds/banners.rb @@ -6,9 +6,9 @@ section "Creating banners" do banner = Banner.new(title: title, description: description, target_url: target_url, - post_started_at: rand((Time.current - 1.week)..(Time.current - 1.day)), - post_ended_at: rand((Time.current - 1.day)..(Time.current + 1.week)), - created_at: rand((Time.current - 1.week)..Time.current)) + post_started_at: rand((1.week.ago)..(1.day.ago)), + post_ended_at: rand((1.day.ago)..(1.week.from_now)), + created_at: rand((1.week.ago)..Time.current)) I18n.available_locales.map do |locale| Globalize.with_locale(locale) do banner.description = "Description for locale #{locale}" diff --git a/db/dev_seeds/budgets.rb b/db/dev_seeds/budgets.rb index e3dc861c2..9a4bea0d9 100644 --- a/db/dev_seeds/budgets.rb +++ b/db/dev_seeds/budgets.rb @@ -113,7 +113,7 @@ section "Creating Investments" do heading: heading, group: heading.group, budget: heading.group.budget, - created_at: rand((Time.current - 1.week)..Time.current), + created_at: rand((1.week.ago)..Time.current), feasibility: %w[undecided unfeasible feasible feasible feasible feasible].sample, unfeasibility_explanation: Faker::Lorem.paragraph, valuation_finished: [false, true].sample, @@ -160,7 +160,7 @@ section "Winner Investments" do budget: heading.group.budget, title: Faker::Lorem.sentence(word_count: 3).truncate(60), description: "

#{Faker::Lorem.paragraphs.join("

")}

", - created_at: rand((Time.current - 1.week)..Time.current), + created_at: rand((1.week.ago)..Time.current), feasibility: "feasible", valuation_finished: true, selected: true, diff --git a/db/dev_seeds/debates.rb b/db/dev_seeds/debates.rb index 7987b0312..6e2737e09 100644 --- a/db/dev_seeds/debates.rb +++ b/db/dev_seeds/debates.rb @@ -5,7 +5,7 @@ section "Creating Debates" do description = "

#{Faker::Lorem.paragraphs.join("

")}

" debate = Debate.create!(author: author, title: Faker::Lorem.sentence(word_count: 3).truncate(60), - created_at: rand((Time.current - 1.week)..Time.current), + created_at: rand((1.week.ago)..Time.current), description: description, tag_list: tags.sample(3).join(","), geozone: Geozone.all.sample, @@ -26,7 +26,7 @@ section "Creating Debates" do debate = Debate.create!(author: author, title: Faker::Lorem.sentence(word_count: 3).truncate(60), - created_at: rand((Time.current - 1.week)..Time.current), + created_at: rand((1.week.ago)..Time.current), description: description, tag_list: tags.sample(3).join(","), geozone: Geozone.all.sample, diff --git a/db/dev_seeds/proposals.rb b/db/dev_seeds/proposals.rb index a2ccbfb08..453960ded 100644 --- a/db/dev_seeds/proposals.rb +++ b/db/dev_seeds/proposals.rb @@ -34,7 +34,7 @@ section "Creating Proposals" do summary: summary, responsible_name: Faker::Name.name, description: description, - created_at: rand((Time.current - 1.week)..Time.current), + created_at: rand((1.week.ago)..Time.current), tag_list: tags.sample(3).join(","), geozone: Geozone.all.sample, terms_of_service: "1", @@ -89,7 +89,7 @@ section "Creating Successful Proposals" do summary: Faker::Lorem.sentence(word_count: 3), responsible_name: Faker::Name.name, description: description, - created_at: rand((Time.current - 1.week)..Time.current), + created_at: rand((1.week.ago)..Time.current), tag_list: tags.sample(3).join(","), geozone: Geozone.all.sample, terms_of_service: "1", @@ -115,7 +115,7 @@ section "Creating Successful Proposals" do summary: Faker::Lorem.sentence(word_count: 3), responsible_name: Faker::Name.name, description: description, - created_at: rand((Time.current - 1.week)..Time.current), + created_at: rand((1.week.ago)..Time.current), tag_list: tags.sample(3).join(","), geozone: Geozone.all.sample, terms_of_service: "1", diff --git a/db/dev_seeds/users.rb b/db/dev_seeds/users.rb index e62424c35..9f36ac09d 100644 --- a/db/dev_seeds/users.rb +++ b/db/dev_seeds/users.rb @@ -9,7 +9,7 @@ section "Creating Users" do confirmed_at: Time.current, terms_of_service: "1", gender: %w[male female].sample, - date_of_birth: rand((Time.current - 80.years)..(Time.current - 16.years)), + date_of_birth: rand((80.years.ago)..(16.years.ago)), public_activity: (rand(1..100) > 30) ) end diff --git a/spec/models/dashboard/action_spec.rb b/spec/models/dashboard/action_spec.rb index 9527cf637..5364ff64c 100644 --- a/spec/models/dashboard/action_spec.rb +++ b/spec/models/dashboard/action_spec.rb @@ -77,7 +77,7 @@ describe Dashboard::Action do it "is active when published after day_offset" do action = build(:dashboard_action, required_supports: 0, day_offset: 10) - proposal = build(:proposal, published_at: Time.current - 10.days) + proposal = build(:proposal, published_at: 10.days.ago) expect(action).to be_active_for(proposal) end @@ -91,7 +91,7 @@ describe Dashboard::Action do it "is not active when not enough time published" do action = build(:dashboard_action, required_supports: 0, day_offset: 10) - proposal = build(:proposal, published_at: Time.current - 9.days) + proposal = build(:proposal, published_at: 9.days.ago) expect(action).not_to be_active_for(proposal) end diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index 0066231f1..172d2b81d 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -352,7 +352,7 @@ describe Poll do describe "#sort_for_list" do it "returns polls sorted by name ASC" do - starts_at = Time.current + 1.day + starts_at = 1.day.from_now poll1 = create(:poll, geozone_restricted: true, starts_at: starts_at, name: "Zzz...") poll2 = create(:poll, geozone_restricted: true, starts_at: starts_at, name: "Mmmm...") poll3 = create(:poll, geozone_restricted: true, starts_at: starts_at, name: "Aaaaah!") @@ -361,7 +361,7 @@ describe Poll do end it "returns not geozone restricted polls first" do - starts_at = Time.current + 1.day + starts_at = 1.day.from_now poll1 = create(:poll, geozone_restricted: false, starts_at: starts_at, name: "Zzz...") poll2 = create(:poll, geozone_restricted: true, starts_at: starts_at, name: "Aaaaaah!") @@ -382,7 +382,7 @@ describe Poll do end it "returns polls earlier to start first" do - starts_at = Time.current + 1.day + starts_at = 1.day.from_now poll1 = create(:poll, geozone_restricted: false, starts_at: starts_at - 1.hour, name: "Zzz...") poll2 = create(:poll, geozone_restricted: false, starts_at: starts_at, name: "Aaaaah!") @@ -402,7 +402,7 @@ describe Poll do end it "orders by name considering fallback locales" do - starts_at = Time.current + 1.day + starts_at = 1.day.from_now poll1 = create(:poll, starts_at: starts_at, name: "Charlie") poll2 = create(:poll, starts_at: starts_at, name: "Delta") poll3 = I18n.with_locale(:es) do diff --git a/spec/requests/dashboard/group_supports_spec.rb b/spec/requests/dashboard/group_supports_spec.rb index 88071b245..af02c1abd 100644 --- a/spec/requests/dashboard/group_supports_spec.rb +++ b/spec/requests/dashboard/group_supports_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" describe Dashboard::GroupSupports do - let(:created_at) { Time.current - 9.days } + let(:created_at) { 9.days.ago } let(:proposal) { create(:proposal, created_at: created_at, published_at: created_at) } before do diff --git a/spec/system/admin/machine_learning_spec.rb b/spec/system/admin/machine_learning_spec.rb index b649a42f7..ebc83de6d 100644 --- a/spec/system/admin/machine_learning_spec.rb +++ b/spec/system/admin/machine_learning_spec.rb @@ -211,11 +211,11 @@ describe "Machine learning" do FileUtils.mkdir_p Rails.root.join("public", "machine_learning", "data") allow_any_instance_of(MachineLearning).to receive(:run) do - MachineLearningJob.first.update!(finished_at: Time.current + 2.minutes) + MachineLearningJob.first.update!(finished_at: 2.minutes.from_now) create(:machine_learning_info, script: "proposals_summary_comments_textrank.py", kind: "comments_summary", - updated_at: Time.current + 2.minutes) + updated_at: 2.minutes.from_now) comments_file = MachineLearning::DATA_FOLDER.join(MachineLearning.comments_filename) File.open(comments_file, "w") { |file| file.write([].to_json) } proposals_comments_summary_file = MachineLearning::DATA_FOLDER.join(MachineLearning.proposals_comments_summary_filename) diff --git a/spec/system/debates_spec.rb b/spec/system/debates_spec.rb index e56e77c9f..71febb1e9 100644 --- a/spec/system/debates_spec.rb +++ b/spec/system/debates_spec.rb @@ -411,8 +411,8 @@ describe "Debates" do scenario "Debates are ordered by newest" do best_debate = create(:debate, title: "Best", created_at: Time.current) - medium_debate = create(:debate, title: "Medium", created_at: Time.current - 1.hour) - worst_debate = create(:debate, title: "Worst", created_at: Time.current - 1.day) + medium_debate = create(:debate, title: "Medium", created_at: 1.hour.ago) + worst_debate = create(:debate, title: "Worst", created_at: 1.day.ago) visit debates_path click_link "newest" diff --git a/spec/system/moderation/budget_investments_spec.rb b/spec/system/moderation/budget_investments_spec.rb index 76d3f7c2f..0ec232715 100644 --- a/spec/system/moderation/budget_investments_spec.rb +++ b/spec/system/moderation/budget_investments_spec.rb @@ -200,14 +200,14 @@ describe "Moderate budget investments" do flagged_investment = create(:budget_investment, heading: heading, title: "Flagged investment", - created_at: Time.current - 1.day, + created_at: 1.day.ago, flags_count: 5 ) flagged_new_investment = create(:budget_investment, heading: heading, title: "Flagged new investment", - created_at: Time.current - 12.hours, + created_at: 12.hours.ago, flags_count: 3 ) diff --git a/spec/system/moderation/comments_spec.rb b/spec/system/moderation/comments_spec.rb index aeaf5c280..294ba3598 100644 --- a/spec/system/moderation/comments_spec.rb +++ b/spec/system/moderation/comments_spec.rb @@ -211,8 +211,8 @@ describe "Moderate comments" do end scenario "sorting comments" do - flagged_comment = create(:comment, body: "Flagged comment", created_at: Time.current - 1.day, flags_count: 5) - flagged_new_comment = create(:comment, body: "Flagged new comment", created_at: Time.current - 12.hours, flags_count: 3) + flagged_comment = create(:comment, body: "Flagged comment", created_at: 1.day.ago, flags_count: 5) + flagged_new_comment = create(:comment, body: "Flagged new comment", created_at: 12.hours.ago, flags_count: 3) newer_comment = create(:comment, body: "Newer comment", created_at: Time.current) visit moderation_comments_path(order: "newest") diff --git a/spec/system/moderation/debates_spec.rb b/spec/system/moderation/debates_spec.rb index 8cd734858..092476220 100644 --- a/spec/system/moderation/debates_spec.rb +++ b/spec/system/moderation/debates_spec.rb @@ -180,8 +180,8 @@ describe "Moderate debates" do end scenario "sorting debates" do - flagged_debate = create(:debate, title: "Flagged debate", created_at: Time.current - 1.day, flags_count: 5) - flagged_new_debate = create(:debate, title: "Flagged new debate", created_at: Time.current - 12.hours, flags_count: 3) + flagged_debate = create(:debate, title: "Flagged debate", created_at: 1.day.ago, flags_count: 5) + flagged_new_debate = create(:debate, title: "Flagged new debate", created_at: 12.hours.ago, flags_count: 3) newer_debate = create(:debate, title: "Newer debate", created_at: Time.current) visit moderation_debates_path(order: "created_at") diff --git a/spec/system/moderation/proposal_notifications_spec.rb b/spec/system/moderation/proposal_notifications_spec.rb index d2ad9b7d2..9b2ee8b83 100644 --- a/spec/system/moderation/proposal_notifications_spec.rb +++ b/spec/system/moderation/proposal_notifications_spec.rb @@ -188,10 +188,10 @@ describe "Moderate proposal notifications" do end scenario "sorting proposal notifications" do - moderated_notification = create(:proposal_notification, :moderated, title: "Moderated notification", created_at: Time.current - 1.day) - moderated_new_notification = create(:proposal_notification, :moderated, title: "Moderated new notification", created_at: Time.current - 12.hours) + moderated_notification = create(:proposal_notification, :moderated, title: "Moderated notification", created_at: 1.day.ago) + moderated_new_notification = create(:proposal_notification, :moderated, title: "Moderated new notification", created_at: 12.hours.ago) newer_notification = create(:proposal_notification, title: "Newer notification", created_at: Time.current) - old_moderated_notification = create(:proposal_notification, :moderated, title: "Older notification", created_at: Time.current - 2.days) + old_moderated_notification = create(:proposal_notification, :moderated, title: "Older notification", created_at: 2.days.ago) visit moderation_proposal_notifications_path(filter: "all", order: "created_at") diff --git a/spec/system/moderation/proposals_spec.rb b/spec/system/moderation/proposals_spec.rb index ceef0bf86..8bbc739cc 100644 --- a/spec/system/moderation/proposals_spec.rb +++ b/spec/system/moderation/proposals_spec.rb @@ -179,8 +179,8 @@ describe "Moderate proposals" do end scenario "sorting proposals" do - flagged_proposal = create(:proposal, title: "Flagged proposal", created_at: Time.current - 1.day, flags_count: 5) - flagged_new_proposal = create(:proposal, title: "Flagged new proposal", created_at: Time.current - 12.hours, flags_count: 3) + flagged_proposal = create(:proposal, title: "Flagged proposal", created_at: 1.day.ago, flags_count: 5) + flagged_new_proposal = create(:proposal, title: "Flagged new proposal", created_at: 12.hours.ago, flags_count: 3) newer_proposal = create(:proposal, title: "Newer proposal", created_at: Time.current) visit moderation_proposals_path(order: "created_at") diff --git a/spec/system/proposals_spec.rb b/spec/system/proposals_spec.rb index 3a05e4f63..a921fc31e 100644 --- a/spec/system/proposals_spec.rb +++ b/spec/system/proposals_spec.rb @@ -794,8 +794,8 @@ describe "Proposals" do scenario "Proposals are ordered by newest" do best_proposal = create(:proposal, title: "Best proposal", created_at: Time.current) - medium_proposal = create(:proposal, title: "Medium proposal", created_at: Time.current - 1.hour) - worst_proposal = create(:proposal, title: "Worst proposal", created_at: Time.current - 1.day) + medium_proposal = create(:proposal, title: "Medium proposal", created_at: 1.hour.ago) + worst_proposal = create(:proposal, title: "Worst proposal", created_at: 1.day.ago) visit proposals_path click_link "newest" diff --git a/spec/system/users_auth_spec.rb b/spec/system/users_auth_spec.rb index bfe06a67e..c41c23e77 100644 --- a/spec/system/users_auth_spec.rb +++ b/spec/system/users_auth_spec.rb @@ -660,7 +660,7 @@ describe "Users" do scenario "Sign in, admin with password expired" do user = create(:administrator).user - user.update!(password_changed_at: Time.current - 1.year) + user.update!(password_changed_at: 1.year.ago) visit new_user_session_path fill_in "Email or username", with: user.email @@ -679,7 +679,7 @@ describe "Users" do end scenario "Sign in, admin without password expired" do - user = create(:user, password_changed_at: Time.current - 360.days) + user = create(:user, password_changed_at: 360.days.ago) admin = create(:administrator, user: user) login_as(admin.user) @@ -689,7 +689,7 @@ describe "Users" do end scenario "Sign in, user with password expired" do - user = create(:user, password_changed_at: Time.current - 1.year) + user = create(:user, password_changed_at: 1.year.ago) login_as(user) visit root_path @@ -699,7 +699,7 @@ describe "Users" do scenario "Admin with password expired trying to use same password" do user = create(:administrator).user - user.update!(password_changed_at: Time.current - 1.year, password: "123456789") + user.update!(password_changed_at: 1.year.ago, password: "123456789") visit new_user_session_path fill_in "Email or username", with: user.email