From 49f4a535697bc1609fefa32b22ea410c2b64aaaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 11 Jan 2019 21:44:32 +0100 Subject: [PATCH] Remove duplication in web percentage stats We didn't use metaprogramming from the start because the `null_percentage_web` method had a particular behaviour. However, the behaviour (due to a typo) didn't really matter because there are no null web votes, and so the `null_percentage_web` is always zero. --- app/models/poll/stats.rb | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index 21f9fba94..cc4e5318d 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -53,21 +53,11 @@ class Poll::Stats recounts.sum(:null_amount) end - def valid_percentage_web - calculate_percentage(total_web_valid, total_valid_votes) - end - - def white_percentage_web - calculate_percentage(total_web_white, total_white_votes) - end - - def null_percentage_web - calculate_percentage(total_web_null, total_null_votes) - end - %i[valid white null].each do |type| - define_method :"#{type}_percentage_booth" do - calculate_percentage(send(:"total_booth_#{type}"), send(:"total_#{type}_votes")) + %i[web booth].each do |channel| + define_method :"#{type}_percentage_#{channel}" do + calculate_percentage(send(:"total_#{channel}_#{type}"), send(:"total_#{type}_votes")) + end end define_method :"total_#{type}_votes" do