diff --git a/app/components/polls/questions/answers_component.html.erb b/app/components/polls/questions/answers_component.html.erb index 4e61ee767..4c746951c 100644 --- a/app/components/polls/questions/answers_component.html.erb +++ b/app/components/polls/questions/answers_component.html.erb @@ -1,33 +1,31 @@
<% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> - <% question.question_answers.each do |answer| %> - <% if answers_by_question_id[question.id] == answer.title && - (!voted_before_sign_in? || - question.poll.voted_in_booth?(current_user)) %> + <% question_answers.each do |question_answer| %> + <% if already_answered?(question_answer) && !voted_before_sign_in? %> "> - <%= answer.title %> + title="<%= t("poll_questions.show.voted", answer: question_answer.title) %>"> + <%= question_answer.title %> <% else %> - <%= button_to answer_question_path(question, answer: answer.title), + <%= button_to answer_question_path(question, answer: question_answer.title), remote: true, - title: t("poll_questions.show.vote_answer", answer: answer.title), + title: t("poll_questions.show.vote_answer", answer: question_answer.title), class: "button secondary hollow" do %> - <%= answer.title %> + <%= question_answer.title %> <% end %> <% end %> <% end %> <% elsif !user_signed_in? %> - <% question.question_answers.order(id: :desc).each do |answer| %> - <%= link_to answer.title, new_user_session_path, class: "button secondary hollow" %> + <% question_answers.order(id: :desc).each do |question_answer| %> + <%= link_to question_answer.title, new_user_session_path, class: "button secondary hollow" %> <% end %> <% elsif !current_user.level_two_or_three_verified? %> - <% question.question_answers.order(id: :desc).each do |answer| %> - <%= link_to answer.title, verification_path, class: "button secondary hollow" %> + <% question_answers.order(id: :desc).each do |question_answer| %> + <%= link_to question_answer.title, verification_path, class: "button secondary hollow" %> <% end %> <% else %> - <% question.question_answers.order(id: :desc).each do |answer| %> - <%= answer.title %> + <% question_answers.order(id: :desc).each do |question_answer| %> + <%= question_answer.title %> <% end %> <% end %>
diff --git a/app/components/polls/questions/answers_component.rb b/app/components/polls/questions/answers_component.rb index 1d64198a4..f51ad4e7f 100644 --- a/app/components/polls/questions/answers_component.rb +++ b/app/components/polls/questions/answers_component.rb @@ -6,26 +6,23 @@ class Polls::Questions::AnswersComponent < ApplicationComponent @question = question end - def answers_by_question_id - if params[:answer] - { question.id => params[:answer] } - else - stored_answers_by_question_id - end + def already_answered?(question_answer) + user_answers.find_by(answer: question_answer.title).present? end def voted_before_sign_in? - question.answers.where(author: current_user).any? do |vote| + user_answers.any? do |vote| vote.updated_at < current_user.current_sign_in_at end end + def question_answers + question.question_answers + end + private - def stored_answers_by_question_id - poll_answers = ::Poll::Answer.by_question(question.poll.question_ids).by_author(current_user&.id) - poll_answers.each_with_object({}) do |answer, answers_by_question_id| - answers_by_question_id[answer.question_id] = answer.answer - end + def user_answers + @user_answers ||= question.answers.by_author(current_user) end end