From 0c8dd283953187196280b68fda275bbf281aa6e8 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 13 Jun 2016 11:25:06 +0800 Subject: [PATCH] FIX: Post count wasn't recovered when a post is recovered. --- lib/post_destroyer.rb | 6 ++++++ spec/components/post_destroyer_spec.rb | 28 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 9d35572f0f9..b1538585af6 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -64,6 +64,12 @@ class PostDestroyer def staff_recovered @post.recover! + + if author = @post.user + author.user_stat.post_count += 1 + author.user_stat.save! + end + @post.publish_change_to_clients! :recovered TopicTrackingState.publish_recover(@post.topic) if @post.topic && @post.post_number == 1 end diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index 00573a5f4de..aacad3ca961 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -162,6 +162,34 @@ describe PostDestroyer do post_action = author.user_actions.where(action_type: UserAction::REPLY, target_post_id: reply.id).first expect(post_action).to be_present end + + describe "post_count recovery" do + before do + post + @user = post.user + expect(@user.user_stat.post_count).to eq(1) + end + + context "recovered by user" do + it "should increment the user's post count" do + PostDestroyer.new(@user, post).destroy + expect(@user.user_stat.post_count).to eq(1) + + PostDestroyer.new(@user, post.reload).recover + expect(@user.reload.user_stat.post_count).to eq(1) + end + end + + context "recovered by admin" do + it "should increment the user's post count" do + PostDestroyer.new(moderator, post).destroy + expect(@user.user_stat.post_count).to eq(0) + + PostDestroyer.new(admin, post).recover + expect(@user.reload.user_stat.post_count).to eq(1) + end + end + end end describe 'basic destroying' do