2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-04 08:47:37 +08:00

FIX: error when deleting a tag associated with a deleted topic

This commit is contained in:
Neil Lalonde 2018-02-21 12:35:53 -05:00
parent 4e7244d8d9
commit 81e873138f
2 changed files with 9 additions and 2 deletions

View file

@ -3,7 +3,7 @@ class TopicTag < ActiveRecord::Base
belongs_to :tag, counter_cache: "topic_count" belongs_to :tag, counter_cache: "topic_count"
after_create do after_create do
if topic.category_id if topic&.category_id
if stat = CategoryTagStat.where(tag_id: tag_id, category_id: topic.category_id).first if stat = CategoryTagStat.where(tag_id: tag_id, category_id: topic.category_id).first
stat.increment!(:topic_count) stat.increment!(:topic_count)
else else
@ -13,7 +13,7 @@ class TopicTag < ActiveRecord::Base
end end
after_destroy do after_destroy do
if topic.category_id if topic&.category_id
if stat = CategoryTagStat.where(tag_id: tag_id, category: topic.category_id).first if stat = CategoryTagStat.where(tag_id: tag_id, category: topic.category_id).first
stat.topic_count == 1 ? stat.destroy : stat.decrement!(:topic_count) stat.topic_count == 1 ? stat.destroy : stat.decrement!(:topic_count)
end end

View file

@ -15,6 +15,13 @@ describe Tag do
SiteSetting.min_trust_level_to_tag_topics = 0 SiteSetting.min_trust_level_to_tag_topics = 0
end end
it "can delete tags on deleted topics" do
tag = Fabricate(:tag)
topic = Fabricate(:topic, tags: [tag])
topic.trash!
expect { tag.destroy }.to change { Tag.count }.by(-1)
end
describe '#tags_by_count_query' do describe '#tags_by_count_query' do
it "returns empty hash if nothing is tagged" do it "returns empty hash if nothing is tagged" do
expect(described_class.tags_by_count_query.count(Tag::COUNT_ARG)).to eq({}) expect(described_class.tags_by_count_query.count(Tag::COUNT_ARG)).to eq({})