diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 6773282ef..eb054dd1e 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -13,9 +13,6 @@ class Polls::QuestionsController < ApplicationController answer.touch if answer.persisted? answer.save! 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] } end diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 6ec328864..7395c1c03 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -62,4 +62,7 @@ class Poll::Question < ApplicationRecord question_answers.inject(0) { |total, question_answer| total + question_answer.total_votes } end + def most_voted_answer_id + question_answers.max_by { |answer| answer.total_votes }.id + end end diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index fa6ba013d..cde5e00d1 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -36,19 +36,7 @@ class Poll::Question::Answer < ApplicationRecord ::Poll::PartialResult.where(question: question).where(answer: title).sum(:amount) end - def most_voted? - most_voted - end - def total_votes_percentage question.answers_total_votes.zero? ? 0 : (total_votes * 100.0) / question.answers_total_votes 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 diff --git a/app/views/polls/results.html.erb b/app/views/polls/results.html.erb index cafb1c9a9..7627fe018 100644 --- a/app/views/polls/results.html.erb +++ b/app/views/polls/results.html.erb @@ -17,13 +17,14 @@
| > - <% if answer.most_voted %> + | > + <% if answer.id == most_voted_answer_id %> <%= t("polls.show.results.most_voted_answer") %> <% end %> <%= answer.title %> @@ -34,7 +35,7 @@ |
|---|---|
| > + | > <%= answer.total_votes %> (<%= answer.total_votes_percentage.round(2) %>%) |