Class: Decidim::Posts::UserAnswersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/decidim/posts/user_answers_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#user_has_no_permission_path

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/decidim/posts/user_answers_controller.rb', line 6

def create
  answer_id = params[:answer_id].to_i
  checked = params[:checked].to_s.downcase == "true" ? true : false

  answer = Answer.find(answer_id)

  if checked
    if answer.question.single_choice?
      UserAnswer.where(decidim_user_id: current_user.id, decidim_posts_answer_id: answer.question.answers.pluck(:id)).destroy_all
    end
    UserAnswer.find_or_create_by(decidim_user_id: current_user.id, decidim_posts_answer_id: answer_id)
  else
    UserAnswer.where(decidim_user_id: current_user.id, decidim_posts_answer_id: answer_id).destroy_all
  end

  # get count of user_answers for all answers of the question
  user_answers_counts = {}
  answer.question.answers.each do |a|
    user_answers_counts[a.id] = UserAnswer.where(decidim_posts_answer_id: a.id).count
  end
  result = {
    user_answers: user_answers_counts,
    survey_responses_count: answer.question.post.survey_responses_count
  }

  render json: result, status: :ok
end