Extract Poll::Results::Question component to render question results
This commit is contained in:
25
app/components/polls/results/question_component.html.erb
Normal file
25
app/components/polls/results/question_component.html.erb
Normal 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>
|
||||
15
app/components/polls/results/question_component.rb
Normal file
15
app/components/polls/results/question_component.rb
Normal 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
|
||||
@@ -16,34 +16,7 @@
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-9 column" data-equalizer-watch>
|
||||
<%- @poll.questions.each do |question| %>
|
||||
<% 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 %>
|
||||
<%= render Polls::Results::QuestionComponent.with_collection(@poll.questions) %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user