From 96cd36e86df0a6216334d2d9647410b377406793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 30 Nov 2015 11:23:38 +0100 Subject: [PATCH] FIX: properly compute the number of voters using only valid options --- plugins/poll/plugin.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index 3d794759128..c74ea0d9f87 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -78,12 +78,16 @@ after_initialize do post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"] ||= {} post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"][poll_name] = options - votes = post.custom_fields[VOTES_CUSTOM_FIELD] - poll["voters"] = votes.values.count { |v| v.has_key?(poll_name) } - + poll["voters"] = 0 all_options = Hash.new(0) + + votes = post.custom_fields[VOTES_CUSTOM_FIELD] votes.map { |_, v| v[poll_name] }.flatten.each { |o| all_options[o] += 1 } - poll["options"].each { |option| option["votes"] = all_options[option["id"]] } + + poll["options"].each do |option| + poll["voters"] += all_options[option["id"]] + option["votes"] = all_options[option["id"]] + end post.custom_fields[POLLS_CUSTOM_FIELD] = polls post.save_custom_fields(true)