From 725861f34430a1eac5d9ad2a7a330a799e6545b9 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 3 Jun 2015 14:22:41 +0530 Subject: [PATCH] FIX: move draft cleanup to the dedicated method and add test --- app/jobs/scheduled/clean_up_drafts.rb | 12 ------------ app/models/draft.rb | 4 ++++ config/site_settings.yml | 2 +- spec/models/draft_spec.rb | 9 ++++++++- 4 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 app/jobs/scheduled/clean_up_drafts.rb diff --git a/app/jobs/scheduled/clean_up_drafts.rb b/app/jobs/scheduled/clean_up_drafts.rb deleted file mode 100644 index b3ace672b..000000000 --- a/app/jobs/scheduled/clean_up_drafts.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Jobs - class CleanUpDrafts < Jobs::Scheduled - every 1.week - - def execute(args) - delete_drafts_older_than_n_days = SiteSetting.delete_drafts_older_than_n_days.days.ago - - # remove old drafts - Draft.where("updated_at < ?", delete_drafts_older_than_n_days).destroy_all - end - end -end diff --git a/app/models/draft.rb b/app/models/draft.rb index efc098984..2a209fd0f 100644 --- a/app/models/draft.rb +++ b/app/models/draft.rb @@ -41,6 +41,10 @@ class Draft < ActiveRecord::Base WHERE s.draft_key = drafts.draft_key AND s.user_id = drafts.user_id )") + + # remove old drafts + delete_drafts_older_than_n_days = SiteSetting.delete_drafts_older_than_n_days.days.ago + Draft.where("updated_at < ?", delete_drafts_older_than_n_days).destroy_all end end diff --git a/config/site_settings.yml b/config/site_settings.yml index 33764aff1..c514c8029 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -893,7 +893,7 @@ uncategorized: default: 0 hidden: true - delete_drafts_older_than_n_days: 90 + delete_drafts_older_than_n_days: 180 tos_topic_id: default: -1 diff --git a/spec/models/draft_spec.rb b/spec/models/draft_spec.rb index 55cd3ac82..ab1b0ff18 100644 --- a/spec/models/draft_spec.rb +++ b/spec/models/draft_spec.rb @@ -34,7 +34,7 @@ describe Draft do expect(Draft.get(@user, "test", 1)).to eq "hello" end - it 'can cleanup old' do + it 'can cleanup old drafts' do user = Fabricate(:user) key = Draft::NEW_TOPIC @@ -55,6 +55,13 @@ describe Draft do Draft.cleanup! expect(Draft.count).to eq 1 + + # should cleanup drafts more than 180 days old + SiteSetting.stubs(:delete_drafts_older_than_n_days).returns(180) + + Draft.last.update_columns(updated_at: 200.days.ago) + Draft.cleanup! + expect(Draft.count).to eq 0 end