Refactor valid, white and null votes calculations

This commit is contained in:
Javi Martín
2019-01-04 19:15:14 +01:00
parent f7d4507ebe
commit 0e2e5a27c3

View File

@@ -42,72 +42,50 @@ class Poll::Stats
voters.where(origin: "web").count - total_web_white voters.where(origin: "web").count - total_web_white
end end
def valid_percentage_web
calculate_percentage(total_web_valid, total_valid_votes)
end
def total_web_white def total_web_white
0 0
end end
def white_percentage_web
calculate_percentage(total_web_white, total_white_votes)
end
def total_web_null def total_web_null
0 0
end end
def null_percentage_web
calculate_percentage(total_web_null, total_null_votes)
end
def total_booth_valid def total_booth_valid
recounts.sum(:total_amount) recounts.sum(:total_amount)
end end
def valid_percentage_booth
calculate_percentage(total_booth_valid, total_valid_votes)
end
def total_booth_white def total_booth_white
recounts.sum(:white_amount) recounts.sum(:white_amount)
end end
def white_percentage_booth
calculate_percentage(total_booth_white, total_white_votes)
end
def total_booth_null def total_booth_null
recounts.sum(:null_amount) recounts.sum(:null_amount)
end end
def null_percentage_booth def valid_percentage_web
calculate_percentage(total_booth_null, total_null_votes) calculate_percentage(total_web_valid, total_valid_votes)
end end
def total_valid_votes def white_percentage_web
total_web_valid + total_booth_valid calculate_percentage(total_web_white, total_white_votes)
end end
def total_valid_percentage def null_percentage_web
calculate_percentage(total_valid_votes, total_participants) calculate_percentage(total_web_null, total_null_votes)
end end
def total_white_votes %i[valid white null].each do |type|
total_web_white + total_booth_white define_method :"#{type}_percentage_booth" do
calculate_percentage(send(:"total_booth_#{type}"), send(:"total_#{type}_votes"))
end end
def total_white_percentage define_method :"total_#{type}_votes" do
calculate_percentage(total_white_votes, total_participants) send(:"total_web_#{type}") + send(:"total_booth_#{type}")
end end
def total_null_votes define_method :"total_#{type}_percentage" do
total_web_null + total_booth_null calculate_percentage(send(:"total_#{type}_votes"), total_participants)
end end
def total_null_percentage
calculate_percentage(total_null_votes, total_participants)
end end
def voters def voters