Simplify Polls::Questions::AnswersComponent view

This commit is contained in:
Senén Rodero Rodríguez
2022-09-01 09:48:48 +02:00
parent bd58023a8a
commit 0ec4d52ed1
2 changed files with 22 additions and 27 deletions

View File

@@ -1,33 +1,31 @@
<div class="poll-question-answers">
<% 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? %>
<span class="button answered"
title="<%= t("poll_questions.show.voted", answer: answer.title) %>">
<%= answer.title %>
title="<%= t("poll_questions.show.voted", answer: question_answer.title) %>">
<%= question_answer.title %>
</span>
<% 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| %>
<span class="button secondary hollow disabled"><%= answer.title %></span>
<% question_answers.order(id: :desc).each do |question_answer| %>
<span class="button secondary hollow disabled"><%= question_answer.title %></span>
<% end %>
<% end %>
</div>

View File

@@ -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