From 7dbe1dce860250d59cc9c6898dd42850c5fb589e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 4 Jan 2019 19:15:44 +0100 Subject: [PATCH] Refactor web and booth votes calculations --- app/models/poll/stats.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index e61a50541..f8ad25560 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -22,20 +22,16 @@ class Poll::Stats total_participants_web + total_participants_booth end - def total_participants_web - total_web_valid + total_web_white + total_web_null - end + %i[web booth].each do |channel| + define_method :"total_participants_#{channel}" do + send(:"total_#{channel}_valid") + + send(:"total_#{channel}_white") + + send(:"total_#{channel}_null") + end - def total_participants_web_percentage - calculate_percentage(total_participants_web, total_participants) - end - - def total_participants_booth - total_booth_valid + total_booth_white + total_booth_null - end - - def total_participants_booth_percentage - calculate_percentage(total_participants_booth, total_participants) + define_method :"total_participants_#{channel}_percentage" do + calculate_percentage(send(:"total_participants_#{channel}"), total_participants) + end end def total_web_valid