diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 3e9c391fd33..56056925998 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -28,6 +28,7 @@ class PostDestroyer def initialize(user, post) @user = user @post = post + @topic = post.topic if post end def destroy @@ -72,6 +73,7 @@ class PostDestroyer remove_associated_notifications @post.topic.trash!(@user) if @post.topic && @post.post_number == 1 update_associated_category_latest_topic + update_user_counts end publish("deleted") end @@ -178,4 +180,31 @@ class PostDestroyer @post.topic.category.update_latest end + def update_user_counts + author = @post.user + + return unless author + + author.create_user_stat if author.user_stat.nil? + + if @post.created_at == author.user_stat.first_post_created_at + author.user_stat.first_post_created_at = author.posts.order('created_at ASC').first.try(:created_at) + end + + author.user_stat.post_count -= 1 + author.user_stat.topic_count -= 1 if @post.post_number == 1 + + # We don't count replies to your own topics + if @topic && author.id != @topic.user_id + author.user_stat.update_topic_reply_count + end + + author.user_stat.save! + + if @post.created_at == author.last_posted_at + author.last_posted_at = author.posts.order('created_at DESC').first.try(:created_at) + author.save! + end + end + end diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index 609e0040b81..669beb1824a 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -119,25 +119,35 @@ describe PostDestroyer do end context "as a moderator" do - before do - PostDestroyer.new(moderator, post).destroy - end - it "deletes the post" do + PostDestroyer.new(moderator, post).destroy post.deleted_at.should be_present post.deleted_by.should == moderator end + + it "updates the user's post_count" do + author = post.user + expect { + PostDestroyer.new(moderator, post).destroy + author.reload + }.to change { author.post_count }.by(-1) + end end context "as an admin" do - before do - PostDestroyer.new(admin, post).destroy - end - it "deletes the post" do + PostDestroyer.new(admin, post).destroy post.deleted_at.should be_present post.deleted_by.should == admin end + + it "updates the user's post_count" do + author = post.user + expect { + PostDestroyer.new(admin, post).destroy + author.reload + }.to change { author.post_count }.by(-1) + end end end