If an auto-closing topic is manually closed, remove the auto-close countdown

This commit is contained in:
Neil Lalonde 2013-07-03 16:44:45 -04:00
parent 44cd5505d3
commit 2c8ed8414c
3 changed files with 20 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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