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"
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
stat.increment!(:topic_count)
else
@ -13,7 +13,7 @@ class TopicTag < ActiveRecord::Base
end
after_destroy do
if topic.category_id
if topic&.category_id
if stat = CategoryTagStat.where(tag_id: tag_id, category: topic.category_id).first
stat.topic_count == 1 ? stat.destroy : stat.decrement!(:topic_count)
end

View File

@ -15,6 +15,13 @@ describe Tag do
SiteSetting.min_trust_level_to_tag_topics = 0
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
it "returns empty hash if nothing is tagged" do
expect(described_class.tags_by_count_query.count(Tag::COUNT_ARG)).to eq({})