From 9106331d4feacf20fac6f6bcdcf4b20557d898f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 1 May 2015 15:53:02 +0200 Subject: [PATCH] FIX: 'migrate_old_poll' rake task to support options with HTML --- plugins/poll/lib/tasks/migrate_old_polls.rake | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/plugins/poll/lib/tasks/migrate_old_polls.rake b/plugins/poll/lib/tasks/migrate_old_polls.rake index 839998060..d1422cc1f 100644 --- a/plugins/poll/lib/tasks/migrate_old_polls.rake +++ b/plugins/poll/lib/tasks/migrate_old_polls.rake @@ -15,15 +15,17 @@ task "poll:migrate_old_polls" => :environment do Timecop.freeze(post.created_at + 1.minute) do # fix the RAW when needed if post.raw !~ /\[poll\]/ - lists = /(^[ ]*- .+?$\n)\n/m.match(post.raw) + lists = /^[ ]*- .+?$\n\n/m.match(post.raw) next if lists.blank? || lists.length == 0 first_list = lists[0] - post.raw = post.raw.sub(first_list, "\n[poll]\n#{first_list}\n[/poll]\n") + post.raw = post.raw.sub(first_list, "\n[poll]\n#{first_list.strip}\n[/poll]\n") else post.raw = post.raw + " " end # save the poll post.save + # retrieve the new options + options = post.custom_fields["polls"]["poll"]["options"] # iterate over all votes PluginStoreRow.where(plugin_name: "poll") .where("key LIKE 'poll_vote_#{post_id}_%'") @@ -31,10 +33,13 @@ task "poll:migrate_old_polls" => :environment do .each do |poll_vote_key, vote| # extract the user_id user_id = poll_vote_key["poll_vote_#{post_id}_%".length..-1].to_i - # conver to MD5 (use the same algorithm as the client-side poll dialect) - options = [Digest::MD5.hexdigest([vote].to_json)] + # find the selected option + vote = vote.strip + selected_option = options.detect { |o| o["html"].strip === vote } + # make sure we have a match + next if selected_option.blank? # submit vote - DiscoursePoll::Poll.vote(post_id, "poll", options, user_id) rescue nil + DiscoursePoll::Poll.vote(post_id, "poll", [selected_option["id"]], user_id) rescue nil end end end