Add rake task for clean up of old content (#3644)

This commit is contained in:
Daniel O'Connor
2024-02-18 18:43:14 +10:30
committed by GitHub
parent a4797c3f67
commit 8593d2dfd9
3 changed files with 31 additions and 0 deletions

View File

@@ -77,6 +77,17 @@ module PredictPlanting
finish_predicted_at <= Time.zone.today
end
# Deactivate any plantings over time_limit that are super late in small batches.
def self.archive!(time_limit: 3.years.ago, limit: 100)
active_plantings = Planting.annual.active.where("planted_at < ?", time_limit).order(planted_at: :asc).limit(limit)
active_plantings.each do |planting|
if planting.finish_is_predicatable? && planting.super_late?
planting.finished = true
planting.save
end
end
end
private
def calculate_percentage_grown

View File

@@ -78,6 +78,16 @@ class Garden < ApplicationRecord
def reindex(refresh: false); end
# Deactivate any gardens with no active plantings
def self.archive!(time_limit: 3.years.ago, limit: 100)
Garden.active.where("updated_at < ?", time_limit).order(updated_at: :asc).limit(limit).each do |active_garden|
unless active_garden.plantings.active.any?
garden.active = false
garden.save
end
end
end
protected
def strip_blanks

10
lib/tasks/gardens.rake Normal file
View File

@@ -0,0 +1,10 @@
# frozen_string_literal: true
namespace :gardens do
desc "Mark old gardens inactive"
task archive: :environment do
Planting.archive!
Garden.archive!
end
end