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:
Neil Lalonde
2013-07-08 15:23:20 -04:00
parent d3fe8f65a2
commit 806bd98f99
2 changed files with 42 additions and 0 deletions

View File

@ -22,11 +22,13 @@ class Topic < ActiveRecord::Base
versioned if: :new_version_required?
def trash!
update_category_topic_count_by(-1) if deleted_at.nil?
super
update_flagged_posts_count
end
def recover!
update_category_topic_count_by(1) unless deleted_at.nil?
super
update_flagged_posts_count
end
@ -630,6 +632,14 @@ class Topic < ActiveRecord::Base
def secure_category?
category && category.secure
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
# == Schema Information