From f4184aea333f6b200a165c9212aecdb8d1cbcef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 2 Dec 2015 12:33:34 +0100 Subject: [PATCH] FIX: properly re-compute # of voters in multiple-choice polls --- plugins/poll/plugin.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index c74ea0d9f87..5754ef8ccba 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -74,21 +74,21 @@ after_initialize do raise StandardError.new I18n.t("poll.requires_at_least_1_valid_option") if options.empty? + poll["voters"] = 0 + all_options = Hash.new(0) + post.custom_fields[VOTES_CUSTOM_FIELD] ||= {} post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"] ||= {} post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"][poll_name] = options - 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 do |option| - poll["voters"] += all_options[option["id"]] - option["votes"] = all_options[option["id"]] + post.custom_fields[VOTES_CUSTOM_FIELD].each do |user_id, user_votes| + next unless votes = user_votes[poll_name] + votes.each { |option| all_options[option] += 1 } + poll["voters"] += 1 if available_options.intersect?(votes.to_set) end + poll["options"].each { |o| o["votes"] = all_options[o["id"]] } + post.custom_fields[POLLS_CUSTOM_FIELD] = polls post.save_custom_fields(true)