Merge pull request #4154 from Growstuff/finish-expired-seeds-task

Add maintenance task to finish expired seeds
This commit is contained in:
google-labs-jules[bot]
2025-08-28 00:16:16 +09:30
committed by GitHub
parent 749134a7de
commit 4d3c4ca10d
3 changed files with 23 additions and 0 deletions

View File

@@ -59,6 +59,7 @@ class Seed < ApplicationRecord
scope :has_location, -> { joins(:owner).where.not('members.location': nil) }
scope :recent, -> { order(created_at: :desc) }
scope :active, -> { where('finished <> true').where('finished_at IS NULL OR finished_at < ?', Time.zone.now) }
scope :expired, -> { active.where('plant_before < ?', Time.zone.today) }
def tradable
tradable_to != 'nowhere'

View File

@@ -53,4 +53,12 @@ namespace :growstuff do
end
end
end
desc "Mark seeds as finished when plant-before date expires"
# usage: rake growstuff:finish_expired_seeds
task finish_expired_seeds: :environment do
Seed.expired.find_each do |seed|
seed.update(finished: true, finished_at: Time.zone.now)
end
end
end

View File

@@ -132,6 +132,20 @@ describe Seed do
end
end
context 'expired' do
it 'returns seeds with a plant_before date in the past' do
expired_seed = FactoryBot.create(:seed, plant_before: 1.day.ago)
not_expired_seed = FactoryBot.create(:seed, plant_before: 1.day.from_now)
described_class.expired.should include expired_seed
described_class.expired.should_not include not_expired_seed
end
it 'does not return finished seeds' do
expired_seed = FactoryBot.create(:seed, plant_before: 1.day.ago, finished: true)
described_class.expired.should_not include expired_seed
end
end
context 'interesting' do
it 'lists interesting seeds' do
# to be interesting a seed must: