mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
Update category's topic_count immediately when trashing or recovering a topic; this ensures that a category can be deleted without waiting for the category_stats job to run.
This commit is contained in:
parent
d3fe8f65a2
commit
806bd98f99
2 changed files with 42 additions and 0 deletions
|
@ -22,11 +22,13 @@ class Topic < ActiveRecord::Base
|
||||||
versioned if: :new_version_required?
|
versioned if: :new_version_required?
|
||||||
|
|
||||||
def trash!
|
def trash!
|
||||||
|
update_category_topic_count_by(-1) if deleted_at.nil?
|
||||||
super
|
super
|
||||||
update_flagged_posts_count
|
update_flagged_posts_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def recover!
|
def recover!
|
||||||
|
update_category_topic_count_by(1) unless deleted_at.nil?
|
||||||
super
|
super
|
||||||
update_flagged_posts_count
|
update_flagged_posts_count
|
||||||
end
|
end
|
||||||
|
@ -630,6 +632,14 @@ class Topic < ActiveRecord::Base
|
||||||
def secure_category?
|
def secure_category?
|
||||||
category && category.secure
|
category && category.secure
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def update_category_topic_count_by(num)
|
||||||
|
if category_id.present?
|
||||||
|
Category.where(['id = ?', category_id]).update_all("topic_count = topic_count " + (num > 0 ? '+' : '') + "#{num}")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -1293,4 +1293,36 @@ describe Topic do
|
||||||
Topic.new(:category => nil).should_not be_secure_category
|
Topic.new(:category => nil).should_not be_secure_category
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'trash!' do
|
||||||
|
context "its category's topic count" do
|
||||||
|
let(:category) { Fabricate(:category) }
|
||||||
|
|
||||||
|
it "subtracts 1 if topic is being deleted" do
|
||||||
|
topic = Fabricate(:topic, category: category)
|
||||||
|
expect { topic.trash! }.to change { category.reload.topic_count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't subtract 1 if topic is already deleted" do
|
||||||
|
topic = Fabricate(:topic, category: category, deleted_at: 1.day.ago)
|
||||||
|
expect { topic.trash! }.to_not change { category.reload.topic_count }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'recover!' do
|
||||||
|
context "its category's topic count" do
|
||||||
|
let(:category) { Fabricate(:category) }
|
||||||
|
|
||||||
|
it "adds 1 if topic is deleted" do
|
||||||
|
topic = Fabricate(:topic, category: category, deleted_at: 1.day.ago)
|
||||||
|
expect { topic.recover! }.to change { category.reload.topic_count }.by(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't add 1 if topic is not deleted" do
|
||||||
|
topic = Fabricate(:topic, category: category)
|
||||||
|
expect { topic.recover! }.to_not change { category.reload.topic_count }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue