From 243d55ec820f5f926b5acacfd2fed127802ed2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 19 Jul 2023 16:34:43 +0200 Subject: [PATCH] Add method to get the archived proposals date limit --- app/models/proposal.rb | 6 +++--- app/models/setting.rb | 4 ++++ db/dev_seeds/proposals.rb | 5 ++--- spec/support/common_actions/proposals.rb | 5 ++--- spec/system/dashboard/dashboard_spec.rb | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 0382ad5e6..74f8864e8 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -72,8 +72,8 @@ class Proposal < ApplicationRecord scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :sort_by_archival_date, -> { archived.sort_by_confidence_score } scope :sort_by_recommendations, -> { order(cached_votes_up: :desc) } - scope :archived, -> { where("proposals.created_at <= ?", Setting["months_to_archive_proposals"].to_i.months.ago) } - scope :not_archived, -> { where("proposals.created_at > ?", Setting["months_to_archive_proposals"].to_i.months.ago) } + scope :archived, -> { where("proposals.created_at <= ?", Setting.archived_proposals_date_limit) } + scope :not_archived, -> { where("proposals.created_at > ?", Setting.archived_proposals_date_limit) } scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago) } scope :retired, -> { where.not(retired_at: nil) } scope :not_retired, -> { where(retired_at: nil) } @@ -221,7 +221,7 @@ class Proposal < ApplicationRecord end def archived? - created_at <= Setting["months_to_archive_proposals"].to_i.months.ago + created_at <= Setting.archived_proposals_date_limit end def notifications diff --git a/app/models/setting.rb b/app/models/setting.rb index 452daeb23..fb2219066 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -229,5 +229,9 @@ class Setting < ApplicationRecord Setting["feature.remote_census"].present? && Setting["remote_census.request.postal_code"].present? end + + def archived_proposals_date_limit + Setting["months_to_archive_proposals"].to_i.months.ago + end end end diff --git a/db/dev_seeds/proposals.rb b/db/dev_seeds/proposals.rb index 61caf0773..6e48cc25d 100644 --- a/db/dev_seeds/proposals.rb +++ b/db/dev_seeds/proposals.rb @@ -49,7 +49,6 @@ section "Creating Archived Proposals" do 5.times do author = User.all.sample description = "

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

")}

" - months_to_archive_proposals = Setting["months_to_archive_proposals"] proposal = Proposal.create!(author: author, title: Faker::Lorem.sentence(word_count: 3).truncate(60), summary: Faker::Lorem.sentence(word_count: 3), @@ -58,8 +57,8 @@ section "Creating Archived Proposals" do tag_list: tags.sample(3).join(","), geozone: Geozone.all.sample, terms_of_service: "1", - created_at: months_to_archive_proposals.to_i.months.ago, - published_at: months_to_archive_proposals.to_i.months.ago) + created_at: Setting.archived_proposals_date_limit, + published_at: Setting.archived_proposals_date_limit) random_locales.map do |locale| Globalize.with_locale(locale) do proposal.title = "Archived proposal title for locale #{locale}" diff --git a/spec/support/common_actions/proposals.rb b/spec/support/common_actions/proposals.rb index 2ee198d14..3a30aa0f1 100644 --- a/spec/support/common_actions/proposals.rb +++ b/spec/support/common_actions/proposals.rb @@ -7,12 +7,11 @@ module Proposals end def create_archived_proposals - months_to_archive_proposals = Setting["months_to_archive_proposals"].to_i [ create(:proposal, title: "This is an expired proposal", - created_at: months_to_archive_proposals.months.ago), + created_at: Setting.archived_proposals_date_limit), create(:proposal, title: "This is an oldest expired proposal", - created_at: (months_to_archive_proposals + 2).months.ago) + created_at: Setting.archived_proposals_date_limit - 2.months) ] end diff --git a/spec/system/dashboard/dashboard_spec.rb b/spec/system/dashboard/dashboard_spec.rb index 6eb79dbb2..bcd3ea8a1 100644 --- a/spec/system/dashboard/dashboard_spec.rb +++ b/spec/system/dashboard/dashboard_spec.rb @@ -313,7 +313,7 @@ describe "Proposal's dashboard" do scenario "Resource admin request button do not appear on archived proposals" do feature = create(:dashboard_action, :resource, :active) - archived = Setting["months_to_archive_proposals"].to_i.months.ago + archived = Setting.archived_proposals_date_limit archived_proposal = create(:proposal, created_at: archived) login_as(archived_proposal.author)