Extract Poll::Results::Question component to render question results

This commit is contained in:
Senén Rodero Rodríguez
2022-08-31 13:59:23 +02:00
parent 7efd7c89ef
commit ed7968349d
3 changed files with 41 additions and 28 deletions

View File

@@ -0,0 +1,25 @@
<h3 id="<%= question.title.parameterize %>"><%= question.title %></h3>
<table id="question_<%= question.id %>_results_table">
<thead>
<tr>
<%- question.question_answers.each do |answer| %>
<th scope="col" class="<%= answer_styles(answer) %>">
<% if most_voted_answer?(answer) %>
<span class="show-for-sr"><%= t("polls.show.results.most_voted_answer") %></span>
<% end %>
<%= answer.title %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<%- question.question_answers.each do |answer| %>
<td id="answer_<%= answer.id %>_result" class="<%= answer_styles(answer) %>">
<%= answer.total_votes %>
(<%= answer.total_votes_percentage.round(2) %>%)
</td>
<% end %>
</tr>
</tbody>
</table>

View File

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

View File

@@ -16,34 +16,7 @@
</div> </div>
<div class="small-12 medium-9 column" data-equalizer-watch> <div class="small-12 medium-9 column" data-equalizer-watch>
<%- @poll.questions.each do |question| %> <%= render Polls::Results::QuestionComponent.with_collection(@poll.questions) %>
<% most_voted_answer_id = question.most_voted_answer_id %>
<h3 id="<%= question.title.parameterize %>"><%= question.title %></h3>
<table id="question_<%= question.id %>_results_table">
<thead>
<tr>
<%- question.question_answers.each do |answer| %>
<th scope="col" <%= answer.id == most_voted_answer_id ? "class=win" : "" %>>
<% if answer.id == most_voted_answer_id %>
<span class="show-for-sr"><%= t("polls.show.results.most_voted_answer") %></span>
<% end %>
<%= answer.title %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<%- question.question_answers.each do |answer| %>
<td id="answer_<%= answer.id %>_result" <%= answer.id == most_voted_answer_id ? "class=win" : "" %>>
<%= answer.total_votes %>
(<%= answer.total_votes_percentage.round(2) %>%)
</td>
<% end %>
</tr>
</tbody>
</table>
<% end %>
</div> </div>
</div> </div>
</div> </div>