FIX: topic and post counts are not updated when ownership of a post is changed

This commit is contained in:
Neil Lalonde
2015-03-02 11:17:11 -05:00
parent 64d0d12632
commit 1bf4f34049
5 changed files with 107 additions and 39 deletions

View File

@ -332,24 +332,15 @@ class TopicsController < ApplicationController
guardian.ensure_can_change_post_owner!
post_ids = params[:post_ids].to_a
topic = Topic.find_by(id: params[:topic_id].to_i)
new_user = User.find_by(username: params[:username])
return render json: failed_json, status: 422 unless post_ids && topic && new_user
ActiveRecord::Base.transaction do
post_ids.each do |post_id|
post = Post.find(post_id)
# update topic owner (first avatar)
topic.user = new_user if post.is_first_post?
post.set_owner(new_user, current_user)
end
begin
PostOwnerChanger.new( post_ids: params[:post_ids].to_a,
topic_id: params[:topic_id].to_i,
new_owner: User.find_by(username: params[:username]),
acting_user: current_user ).change_owner!
render json: success_json
rescue ArgumentError
render json: failed_json, status: 422
end
topic.update_statistics
render json: success_json
end
def clear_pin