diff --git a/app/controllers/officing/base_controller.rb b/app/controllers/officing/base_controller.rb index e73f2b696..96f185d24 100644 --- a/app/controllers/officing/base_controller.rb +++ b/app/controllers/officing/base_controller.rb @@ -20,10 +20,7 @@ class Officing::BaseController < ApplicationController end def load_officer_assignment - @officer_assignments ||= current_user.poll_officer. - officer_assignments. - voting_days. - where(date: Time.current.to_date) + @officer_assignments ||= current_user.poll_officer.officer_assignments.where(date: Time.current.to_date) end def verify_officer_assignment 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/helpers/stats_helper.rb b/app/helpers/stats_helper.rb index 57db7166a..5dabd3f82 100644 --- a/app/helpers/stats_helper.rb +++ b/app/helpers/stats_helper.rb @@ -31,4 +31,8 @@ module StatsHelper content_tag :div, "", opt end + def calculate_percentage(fraction, total) + percent = fraction / total.to_f + percent.nan? ? 0.0 : (percent * 100).round(3) + end end diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index a63e4ec4a..bcdfe68a4 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -59,7 +59,10 @@ class Poll::Question < ApplicationRecord end def answers_total_votes - question_answers.map { |a| Poll::Answer.where(question_id: self, answer: a.title).count }.sum + 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 b2434aa3b..cde5e00d1 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -32,22 +32,11 @@ class Poll::Question::Answer < ApplicationRecord end def total_votes - Poll::Answer.where(question_id: question, answer: title).count - end - - def most_voted? - most_voted + Poll::Answer.where(question_id: question, answer: title).count + + ::Poll::PartialResult.where(question: question).where(answer: title).sum(:amount) 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/models/poll/stats.rb b/app/models/poll/stats.rb index 7f12e2a56..fe9560c34 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -1,5 +1,6 @@ class Poll class Stats + include StatsHelper def initialize(poll) @poll = poll @@ -26,29 +27,23 @@ class Poll end def total_participants_web_percentage - stats_cache("total_participants_web_percentage") do - total_participants.zero? ? 0 : total_participants_web * 100 / total_participants - end + stats_cache("total_participants_web_percentage") { calculate_percentage(total_participants_web, total_participants) } end def total_participants_booth - stats_cache("total_participants_booth") { voters.where(origin: "booth").count } + stats_cache("total_participants_booth") { total_booth_valid + total_booth_white + total_booth_null } end def total_participants_booth_percentage - stats_cache("total_participants_booth_percentage") do - total_participants.zero? ? 0 : total_participants_booth * 100 / total_participants.to_f - end + stats_cache("total_participants_booth_percentage") { calculate_percentage(total_participants_booth, total_participants) } end def total_web_valid - stats_cache("total_web_valid") { voters.where(origin: "web").count } + stats_cache("total_web_valid") { voters.where(origin: "web").count - total_web_white } end def valid_percentage_web - stats_cache("valid_percentage_web") do - total_valid_votes.zero? ? 0 : total_web_valid * 100 / total_valid_votes.to_f - end + stats_cache("valid_percentage_web") { calculate_percentage(total_web_valid, total_valid_votes) } end def total_web_white @@ -56,7 +51,7 @@ class Poll end def white_percentage_web - stats_cache("white_percentage_web") { 0 } + stats_cache("white_percentage_web") { calculate_percentage(total_web_white, total_white_votes) } end def total_web_null @@ -64,7 +59,7 @@ class Poll end def null_percentage_web - stats_cache("null_percentage_web") { 0 } + stats_cache("null_percentage_web") { calculate_percentage(total_web_null, total_null_votes) } end def total_booth_valid @@ -72,9 +67,7 @@ class Poll end def valid_percentage_booth - stats_cache("valid_percentage_booth") do - total_valid_votes.zero? ? 0 : total_booth_valid * 100 / total_valid_votes.to_f - end + stats_cache("valid_percentage_booth") { calculate_percentage(total_booth_valid, total_valid_votes) } end def total_booth_white @@ -82,9 +75,7 @@ class Poll end def white_percentage_booth - stats_cache("white_percentage_booth") do - total_white_votes.zero? ? 0 : total_booth_white * 100 / total_white_votes.to_f - end + stats_cache("white_percentage_booth") { calculate_percentage(total_booth_white, total_white_votes) } end def total_booth_null @@ -92,9 +83,7 @@ class Poll end def null_percentage_booth - stats_cache("null_percentage_booth") do - total_null_votes.zero? ? 0 : total_booth_null * 100 / total_null_votes.to_f - end + stats_cache("null_percentage_booth") { calculate_percentage(total_booth_null, total_null_votes) } end def total_valid_votes @@ -102,9 +91,7 @@ class Poll end def total_valid_percentage - stats_cache("total_valid_percentage") do - total_participants.zero? ? 0 : total_valid_votes * 100 / total_participants.to_f - end + stats_cache("total_valid_percentage"){ calculate_percentage(total_valid_votes, total_participants) } end def total_white_votes @@ -112,9 +99,7 @@ class Poll end def total_white_percentage - stats_cache("total_white_percentage") do - total_participants.zero? ? 0 : total_white_votes * 100 / total_participants.to_f - end + stats_cache("total_white_percentage") { calculate_percentage(total_white_votes, total_participants) } end def total_null_votes @@ -122,9 +107,7 @@ class Poll end def total_null_percentage - stats_cache("total_null_percentage") do - total_participants.zero? ? 0 : total_null_votes * 100 / total_participants.to_f - end + stats_cache("total_null_percentage") { calculate_percentage(total_null_votes, total_participants) } end def voters diff --git a/app/models/poll/voter.rb b/app/models/poll/voter.rb index 522c28de2..c899cabc9 100644 --- a/app/models/poll/voter.rb +++ b/app/models/poll/voter.rb @@ -1,7 +1,7 @@ class Poll class Voter < ApplicationRecord - VALID_ORIGINS = %w{web booth}.freeze + VALID_ORIGINS = %w[web booth letter].freeze belongs_to :poll belongs_to :user @@ -20,8 +20,9 @@ class Poll before_validation :set_demographic_info, :set_document_info, :set_denormalized_booth_assignment_id - scope :web, -> { where(origin: "web") } - scope :booth, -> { where(origin: "booth") } + scope :web, -> { where(origin: "web") } + scope :booth, -> { where(origin: "booth") } + scope :letter, -> { where(origin: "letter") } def set_demographic_info return if user.blank? 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) %>%) | diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb index 2984e16d9..4c33f6b97 100644 --- a/app/views/polls/show.html.erb +++ b/app/views/polls/show.html.erb @@ -59,7 +59,7 @@ -