2015-03-02 10:17:11 -06:00
|
|
|
class PostOwnerChanger
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@post_ids = params[:post_ids]
|
2015-07-14 23:15:34 -05:00
|
|
|
@topic = Topic.with_deleted.find_by(id: params[:topic_id].to_i)
|
2015-03-02 10:17:11 -06:00
|
|
|
@new_owner = params[:new_owner]
|
|
|
|
@acting_user = params[:acting_user]
|
|
|
|
|
|
|
|
raise ArgumentError unless @post_ids && @topic && @new_owner && @acting_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_owner!
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@post_ids.each do |post_id|
|
2015-07-14 23:15:34 -05:00
|
|
|
post = Post.with_deleted.find(post_id)
|
2015-03-02 10:17:11 -06:00
|
|
|
@topic.user = @new_owner if post.is_first_post?
|
|
|
|
post.set_owner(@new_owner, @acting_user)
|
|
|
|
end
|
|
|
|
|
2015-03-11 14:54:11 -05:00
|
|
|
@topic.update_statistics
|
|
|
|
@new_owner.user_stat.update(first_post_created_at: @new_owner.posts(true).order('created_at ASC').first.try(:created_at))
|
2015-04-06 02:27:05 -05:00
|
|
|
@topic.save!
|
2015-03-11 14:54:11 -05:00
|
|
|
end
|
2015-03-02 10:17:11 -06:00
|
|
|
end
|
|
|
|
end
|