From 2c8ed8414cbb23281af327c11ace20e0c0bd4772 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 3 Jul 2013 16:44:45 -0400 Subject: [PATCH] If an auto-closing topic is manually closed, remove the auto-close countdown --- app/models/topic.rb | 2 +- app/models/topic_status_update.rb | 8 ++++++++ spec/integration/topic_auto_close_spec.rb | 16 +++++++++++----- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 2a1c0eddb..16bbd0df8 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -138,7 +138,7 @@ class Topic < ActiveRecord::Base before_save do if (auto_close_at_changed? and !auto_close_at_was.nil?) or (auto_close_user_id_changed? and auto_close_at) - self.auto_close_started_at ||= Time.zone.now + self.auto_close_started_at ||= Time.zone.now if auto_close_at Jobs.cancel_scheduled_job(:close_topic, {topic_id: id}) true end diff --git a/app/models/topic_status_update.rb b/app/models/topic_status_update.rb index d425b877e..cb890cfbf 100644 --- a/app/models/topic_status_update.rb +++ b/app/models/topic_status_update.rb @@ -18,6 +18,10 @@ TopicStatusUpdate = Struct.new(:topic, :user) do else topic.update_column status.name, status.enabled? end + + if status.manually_closing_topic? && topic.auto_close_at + topic.reload.set_auto_close(nil).save + end end def create_moderator_post_for(status) @@ -57,5 +61,9 @@ TopicStatusUpdate = Struct.new(:topic, :user) do def reopening_topic? (closed? || autoclosed?) && disabled? end + + def manually_closing_topic? + closed? && enabled? + end end end diff --git a/spec/integration/topic_auto_close_spec.rb b/spec/integration/topic_auto_close_spec.rb index 7adab420b..18432bd5c 100644 --- a/spec/integration/topic_auto_close_spec.rb +++ b/spec/integration/topic_auto_close_spec.rb @@ -30,13 +30,13 @@ describe Topic do context 'uncategorized' do Given(:category) { nil } Then { topic.auto_close_at.should be_nil } - And { scheduled_jobs_for(:close_topic).should be_empty } + And { scheduled_jobs_for(:close_topic).should be_empty } end context 'category without default auto-close' do Given(:category) { Fabricate(:category, auto_close_days: nil) } Then { topic.auto_close_at.should be_nil } - And { scheduled_jobs_for(:close_topic).should be_empty } + And { scheduled_jobs_for(:close_topic).should be_empty } end context 'jobs may be queued' do @@ -52,14 +52,20 @@ describe Topic do context 'category has a default auto-close' do Given(:category) { Fabricate(:category, auto_close_days: 2.0) } Then { topic.auto_close_at.should == 2.days.from_now } - And { topic.auto_close_started_at.should == Time.zone.now } - And { scheduled_jobs_for(:close_topic, {topic_id: topic.id}).should have(1).job } - And { scheduled_jobs_for(:close_topic, {topic_id: category.topic.id}).should be_empty } + And { topic.auto_close_started_at.should == Time.zone.now } + And { scheduled_jobs_for(:close_topic, {topic_id: topic.id}).should have(1).job } + And { scheduled_jobs_for(:close_topic, {topic_id: category.topic.id}).should be_empty } context 'topic was created by staff user' do Given(:admin) { Fabricate(:admin) } Given(:staff_topic) { Fabricate(:topic, user: admin, category: category) } Then { scheduled_jobs_for(:close_topic, {topic_id: staff_topic.id, user_id: admin.id}).should have(1).job } + + context 'topic is closed manually' do + When { staff_topic.update_status('closed', true, admin) } + Then { staff_topic.reload.auto_close_at.should be_nil } + And { staff_topic.auto_close_started_at.should be_nil } + end end context 'topic was created by a non-staff user' do