Move question most voted answer from boolean to a enumerable max by total_votes
This commit is contained in:
@@ -13,9 +13,6 @@ class Polls::QuestionsController < ApplicationController
|
|||||||
answer.touch if answer.persisted?
|
answer.touch if answer.persisted?
|
||||||
answer.save!
|
answer.save!
|
||||||
answer.record_voter_participation(token)
|
answer.record_voter_participation(token)
|
||||||
@question.question_answers.where(question_id: @question).each do |question_answer|
|
|
||||||
question_answer.set_most_voted
|
|
||||||
end
|
|
||||||
|
|
||||||
@answers_by_question_id = { @question.id => params[:answer] }
|
@answers_by_question_id = { @question.id => params[:answer] }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -62,4 +62,7 @@ class Poll::Question < ApplicationRecord
|
|||||||
question_answers.inject(0) { |total, question_answer| total + question_answer.total_votes }
|
question_answers.inject(0) { |total, question_answer| total + question_answer.total_votes }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def most_voted_answer_id
|
||||||
|
question_answers.max_by { |answer| answer.total_votes }.id
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -36,19 +36,7 @@ class Poll::Question::Answer < ApplicationRecord
|
|||||||
::Poll::PartialResult.where(question: question).where(answer: title).sum(:amount)
|
::Poll::PartialResult.where(question: question).where(answer: title).sum(:amount)
|
||||||
end
|
end
|
||||||
|
|
||||||
def most_voted?
|
|
||||||
most_voted
|
|
||||||
end
|
|
||||||
|
|
||||||
def total_votes_percentage
|
def total_votes_percentage
|
||||||
question.answers_total_votes.zero? ? 0 : (total_votes * 100.0) / question.answers_total_votes
|
question.answers_total_votes.zero? ? 0 : (total_votes * 100.0) / question.answers_total_votes
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_most_voted
|
|
||||||
answers = question.question_answers
|
|
||||||
.map { |a| Poll::Answer.where(question_id: a.question, answer: a.title).count }
|
|
||||||
is_most_voted = answers.none?{ |a| a > total_votes }
|
|
||||||
|
|
||||||
update(most_voted: is_most_voted)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,13 +17,14 @@
|
|||||||
|
|
||||||
<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| %>
|
<%- @poll.questions.each do |question| %>
|
||||||
|
<% most_voted_answer_id = question.most_voted_answer_id %>
|
||||||
<h3 id="<%= question.title.parameterize %>"><%= question.title %></h3>
|
<h3 id="<%= question.title.parameterize %>"><%= question.title %></h3>
|
||||||
<table id="question_<%= question.id %>_results_table">
|
<table id="question_<%= question.id %>_results_table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<%- question.question_answers.each do |answer| %>
|
<%- question.question_answers.each do |answer| %>
|
||||||
<th scope="col" <%= answer.most_voted? ? "class=win" : "" %>>
|
<th scope="col" <%= answer.id == most_voted_answer_id ? "class=win" : "" %>>
|
||||||
<% if answer.most_voted %>
|
<% if answer.id == most_voted_answer_id %>
|
||||||
<span class="show-for-sr"><%= t("polls.show.results.most_voted_answer") %></span>
|
<span class="show-for-sr"><%= t("polls.show.results.most_voted_answer") %></span>
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= answer.title %>
|
<%= answer.title %>
|
||||||
@@ -34,7 +35,7 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<%- question.question_answers.each do |answer| %>
|
<%- question.question_answers.each do |answer| %>
|
||||||
<td id="answer_<%= answer.id %>_result" <%= answer.most_voted? ? "class=win" : "" %>>
|
<td id="answer_<%= answer.id %>_result" <%= answer.id == most_voted_answer_id ? "class=win" : "" %>>
|
||||||
<%= answer.total_votes %>
|
<%= answer.total_votes %>
|
||||||
(<%= answer.total_votes_percentage.round(2) %>%)
|
(<%= answer.total_votes_percentage.round(2) %>%)
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user