FIX: PostDestroyer needs to update user stats. Delete All Posts button was broken, making it impossible to delete users.

This commit is contained in:
Neil Lalonde 2014-08-14 15:21:10 -04:00
parent 26a404f4d0
commit 658cdd2c9e
2 changed files with 47 additions and 8 deletions

View File

@ -28,6 +28,7 @@ class PostDestroyer
def initialize(user, post) def initialize(user, post)
@user = user @user = user
@post = post @post = post
@topic = post.topic if post
end end
def destroy def destroy
@ -72,6 +73,7 @@ class PostDestroyer
remove_associated_notifications remove_associated_notifications
@post.topic.trash!(@user) if @post.topic && @post.post_number == 1 @post.topic.trash!(@user) if @post.topic && @post.post_number == 1
update_associated_category_latest_topic update_associated_category_latest_topic
update_user_counts
end end
publish("deleted") publish("deleted")
end end
@ -178,4 +180,31 @@ class PostDestroyer
@post.topic.category.update_latest @post.topic.category.update_latest
end 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 end

View File

@ -119,25 +119,35 @@ describe PostDestroyer do
end end
context "as a moderator" do context "as a moderator" do
before do
PostDestroyer.new(moderator, post).destroy
end
it "deletes the post" do it "deletes the post" do
PostDestroyer.new(moderator, post).destroy
post.deleted_at.should be_present post.deleted_at.should be_present
post.deleted_by.should == moderator post.deleted_by.should == moderator
end 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 end
context "as an admin" do context "as an admin" do
before do
PostDestroyer.new(admin, post).destroy
end
it "deletes the post" do it "deletes the post" do
PostDestroyer.new(admin, post).destroy
post.deleted_at.should be_present post.deleted_at.should be_present
post.deleted_by.should == admin post.deleted_by.should == admin
end 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
end end