Class: AnswersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/answers_controller.rb

Overview

Web controller. Provides actions that relate to answers. Pretty much the standard set of resources, really - it’s questions that have a few more actions.

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#convert_to_commentObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/answers_controller.rb', line 8

def convert_to_comment
  return not_found unless current_user.has_post_privilege?('flag_curate', @answer)

  text = @answer.body_markdown
  comments = helpers.split_words_max_length(text, 500)
  post = Post.find(params[:post_id])
  thread = post.comment_threads.find_or_create_by(title: 'General comments', deleted: false)
  created = nil
  ActiveRecord::Base.transaction do
    created = comments.map do |c|
      Comment.create!(user: @answer.user, post: post, comment_thread: thread, community: @answer.community,
                      content: c)
    end
    @answer.update!(deleted: true, deleted_at: DateTime.now, deleted_by: current_user)
    AuditLog.moderator_audit(event_type: 'convert_to_comment', related: @answer, user: current_user,
                             comment: created.map(&:id).join(', '))
  end
  render json: { success: true, comments: created.map(&:id) }
end