diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 490b32fbe..32f4b8e54 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -9,6 +9,7 @@ class Polls::QuestionsController < ApplicationController answer = @question.answers.find_or_initialize_by(author: current_user) answer.answer = params[:answer] + answer.token = params[:token] answer.save! answer.record_voter_participation diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 41a038b46..18a75534c 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -1,5 +1,7 @@ class PollsController < ApplicationController + include PollsHelper + load_and_authorize_resource has_filters %w{current expired incoming} @@ -12,7 +14,7 @@ class PollsController < ApplicationController def show @questions = @poll.questions.for_render.sort_for_list - + @token = poll_answer_author_token(@poll, current_user) @answers_by_question_id = {} poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id)) poll_answers.each do |answer| diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index 27d33ea04..a0a4736aa 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -41,4 +41,15 @@ module PollsHelper booth.name + location end + def poll_answer_author_token(poll, author) + existing_token = Poll::Answer.where(question: poll.questions, author: author) + existing_token.present? ? existing_token.first.token : poll_answer_unique_token + end + + def poll_answer_unique_token + loop do + token = SecureRandom.hex(32) + break token unless Poll::Answer.where(token: token).exists? + end + end end diff --git a/app/views/polls/questions/_answers.html.erb b/app/views/polls/questions/_answers.html.erb index 2bef6de72..8188c4cd1 100644 --- a/app/views/polls/questions/_answers.html.erb +++ b/app/views/polls/questions/_answers.html.erb @@ -2,13 +2,13 @@ <% if can? :answer, question %> <% question.question_answers.each do |answer| %> <% if @answers_by_question_id[question.id] == answer.title %> - <% else %> <%= link_to answer.title, - answer_question_path(question, answer: answer.title), + answer_question_path(question, answer: answer.title, token: token), method: :post, remote: true, title: t("poll_questions.show.vote_answer", answer: answer.title), diff --git a/app/views/polls/questions/_question.html.erb b/app/views/polls/questions/_question.html.erb index 982d0a070..b0df3fb42 100644 --- a/app/views/polls/questions/_question.html.erb +++ b/app/views/polls/questions/_question.html.erb @@ -4,6 +4,6 @@