A platform for community discussion. Free, open, simple.
FIX: update categories topic_count when converting topic to a PM and vice versa
Arpit Jalanc36e201eb355
diff --git a/app/models/topic_converter.rb b/app/models/topic_converter.rbindex aed196943a8..76704a7a8ce 100644--- a/app/models/topic_converter.rb+++ b/app/models/topic_converter.rb@@ -24,6 +24,7 @@ class TopicConverter@topic.archetype = Archetype.default@topic.saveupdate_user_stats+ update_category_topic_count_by(1)# TODO: Every post in a PRIVATE MESSAGE looks the same: each is a UserAction::NEW_PRIVATE_MESSAGE.# So we need to remove all those user actions and re-log all the posts.@@ -46,6 +47,7 @@ class TopicConverterdef convert_to_private_messageTopic.transaction do+ update_category_topic_count_by(-1)@topic.category_id = nil@topic.archetype = Archetype.private_messageadd_allowed_users@@ -92,4 +94,10 @@ class TopicConverterendend+ def update_category_topic_count_by(num)+ if @topic.category_id.present?+ Category.where(['id = ?', @topic.category_id]).update_all("topic_count = topic_count " + (num > 0 ? '+' : '') + "#{num}")+ end+ end+enddiff --git a/spec/models/topic_converter_spec.rb b/spec/models/topic_converter_spec.rbindex 66e2c94380d..adbe5e911d4 100644--- a/spec/models/topic_converter_spec.rb+++ b/spec/models/topic_converter_spec.rb@@ -5,7 +5,7 @@ describe TopicConverter docontext 'convert_to_public_topic' dolet(:admin) { Fabricate(:admin) }let(:author) { Fabricate(:user) }- let(:category) { Fabricate(:category) }+ let(:category) { Fabricate(:category, topic_count: 1) }let(:private_message) { Fabricate(:private_message_topic, user: author) } # creates a topic without a first postlet(:first_post) { Fabricate(:post, topic: private_message, user: author) }let(:other_user) { private_message.topic_allowed_users.find { |u| u.user != author }.user }@@ -19,6 +19,7 @@ describe TopicConverter doexpect(topic).to be_validexpect(topic.archetype).to eq("regular")expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id)+ expect(topic.category.topic_count).to eq(1)enddescribe 'when uncategorized category is not allowed' do@@ -38,6 +39,7 @@ describe TopicConverter do.where(read_restricted: false).order('id asc').firstexpect(topic.category_id).to eq(first_category.id)+ expect(topic.category.topic_count).to eq(2)endend@@ -46,6 +48,7 @@ describe TopicConverter dotopic = TopicConverter.new(private_message, admin).convert_to_public_topic(category.id)expect(topic.reload.category).to eq(category)+ expect(topic.category.topic_count).to eq(2)endend@@ -86,13 +89,16 @@ describe TopicConverter docontext 'convert_to_private_message' dolet(:admin) { Fabricate(:admin) }let(:author) { Fabricate(:user) }- let(:topic) { Fabricate(:topic, user: author) }+ let(:category) { Fabricate(:category) }+ let(:topic) { Fabricate(:topic, user: author, category_id: category.id) }context 'success' doit "converts regular topic to private message" doprivate_message = topic.convert_to_private_message(admin)expect(private_message).to be_validexpect(topic.archetype).to eq("private_message")+ expect(topic.category_id).to eq(nil)+ expect(category.topic_count).to eq(0)endit "updates user stats" do