From ed7968349ddc825c9dfd5bee5213890b0e77396c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?=
<15726+Senen@users.noreply.github.com>
Date: Wed, 31 Aug 2022 13:59:23 +0200
Subject: [PATCH] Extract `Poll::Results::Question` component to render
question results
---
.../polls/results/question_component.html.erb | 25 ++++++++++++++++
.../polls/results/question_component.rb | 15 ++++++++++
app/views/polls/results.html.erb | 29 +------------------
3 files changed, 41 insertions(+), 28 deletions(-)
create mode 100644 app/components/polls/results/question_component.html.erb
create mode 100644 app/components/polls/results/question_component.rb
diff --git a/app/components/polls/results/question_component.html.erb b/app/components/polls/results/question_component.html.erb
new file mode 100644
index 000000000..f11a56898
--- /dev/null
+++ b/app/components/polls/results/question_component.html.erb
@@ -0,0 +1,25 @@
+
<%= question.title %>
+
+
+
+ <%- question.question_answers.each do |answer| %>
+ |
+ <% if most_voted_answer?(answer) %>
+ <%= t("polls.show.results.most_voted_answer") %>
+ <% end %>
+ <%= answer.title %>
+ |
+ <% end %>
+
+
+
+
+ <%- question.question_answers.each do |answer| %>
+ |
+ <%= answer.total_votes %>
+ (<%= answer.total_votes_percentage.round(2) %>%)
+ |
+ <% end %>
+
+
+
diff --git a/app/components/polls/results/question_component.rb b/app/components/polls/results/question_component.rb
new file mode 100644
index 000000000..f62a6b69d
--- /dev/null
+++ b/app/components/polls/results/question_component.rb
@@ -0,0 +1,15 @@
+class Polls::Results::QuestionComponent < ApplicationComponent
+ attr_reader :question
+
+ def initialize(question:)
+ @question = question
+ end
+
+ def answer_styles(answer)
+ "win" if most_voted_answer?(answer)
+ end
+
+ def most_voted_answer?(answer)
+ answer.id == question.most_voted_answer_id
+ end
+end
diff --git a/app/views/polls/results.html.erb b/app/views/polls/results.html.erb
index 745b31914..eaaaaae5b 100644
--- a/app/views/polls/results.html.erb
+++ b/app/views/polls/results.html.erb
@@ -16,34 +16,7 @@
- <%- @poll.questions.each do |question| %>
- <% most_voted_answer_id = question.most_voted_answer_id %>
-
<%= question.title %>
-
-
-
- <%- question.question_answers.each do |answer| %>
- | >
- <% if answer.id == most_voted_answer_id %>
- <%= t("polls.show.results.most_voted_answer") %>
- <% end %>
- <%= answer.title %>
- |
- <% end %>
-
-
-
-
- <%- question.question_answers.each do |answer| %>
- | >
- <%= answer.total_votes %>
- (<%= answer.total_votes_percentage.round(2) %>%)
- |
- <% end %>
-
-
-
- <% end %>
+ <%= render Polls::Results::QuestionComponent.with_collection(@poll.questions) %>