FIX: properly re-compute # of voters in multiple-choice polls

This commit is contained in:
Régis Hanol 2015-12-02 12:33:34 +01:00
parent 19f1314a3a
commit f4184aea33
1 changed files with 9 additions and 9 deletions

View File

@ -74,21 +74,21 @@ after_initialize do
raise StandardError.new I18n.t("poll.requires_at_least_1_valid_option") if options.empty? 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] ||= {}
post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"] ||= {} post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"] ||= {}
post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"][poll_name] = options post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"][poll_name] = options
poll["voters"] = 0 post.custom_fields[VOTES_CUSTOM_FIELD].each do |user_id, user_votes|
all_options = Hash.new(0) next unless votes = user_votes[poll_name]
votes.each { |option| all_options[option] += 1 }
votes = post.custom_fields[VOTES_CUSTOM_FIELD] poll["voters"] += 1 if available_options.intersect?(votes.to_set)
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"]]
end end
poll["options"].each { |o| o["votes"] = all_options[o["id"]] }
post.custom_fields[POLLS_CUSTOM_FIELD] = polls post.custom_fields[POLLS_CUSTOM_FIELD] = polls
post.save_custom_fields(true) post.save_custom_fields(true)