mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-03-24 05:42:03 -04:00
FIX: 'migrate_old_poll' rake task to support options with HTML
This commit is contained in:
parent
80003e0821
commit
9106331d4f
1 changed files with 10 additions and 5 deletions
|
@ -15,15 +15,17 @@ task "poll:migrate_old_polls" => :environment do
|
||||||
Timecop.freeze(post.created_at + 1.minute) do
|
Timecop.freeze(post.created_at + 1.minute) do
|
||||||
# fix the RAW when needed
|
# fix the RAW when needed
|
||||||
if post.raw !~ /\[poll\]/
|
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
|
next if lists.blank? || lists.length == 0
|
||||||
first_list = lists[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
|
else
|
||||||
post.raw = post.raw + " "
|
post.raw = post.raw + " "
|
||||||
end
|
end
|
||||||
# save the poll
|
# save the poll
|
||||||
post.save
|
post.save
|
||||||
|
# retrieve the new options
|
||||||
|
options = post.custom_fields["polls"]["poll"]["options"]
|
||||||
# iterate over all votes
|
# iterate over all votes
|
||||||
PluginStoreRow.where(plugin_name: "poll")
|
PluginStoreRow.where(plugin_name: "poll")
|
||||||
.where("key LIKE 'poll_vote_#{post_id}_%'")
|
.where("key LIKE 'poll_vote_#{post_id}_%'")
|
||||||
|
@ -31,10 +33,13 @@ task "poll:migrate_old_polls" => :environment do
|
||||||
.each do |poll_vote_key, vote|
|
.each do |poll_vote_key, vote|
|
||||||
# extract the user_id
|
# extract the user_id
|
||||||
user_id = poll_vote_key["poll_vote_#{post_id}_%".length..-1].to_i
|
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)
|
# find the selected option
|
||||||
options = [Digest::MD5.hexdigest([vote].to_json)]
|
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
|
# 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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue