FIX: migrate old polls rake task

This commit is contained in:
Régis Hanol 2015-04-30 17:08:44 +02:00
parent 9e16e03198
commit e9b70ac9cb

View file

@ -1,5 +1,6 @@
desc "Migrate old polls to new syntax" desc "Migrate old polls to new syntax"
task "poll:migrate_old_polls" => :environment do task "poll:migrate_old_polls" => :environment do
require "timecop"
# iterate over all polls # iterate over all polls
PluginStoreRow.where(plugin_name: "poll") PluginStoreRow.where(plugin_name: "poll")
.where("key LIKE 'poll_options_%'") .where("key LIKE 'poll_options_%'")
@ -14,8 +15,10 @@ 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\]/
first_list = /(^- .+?$\n)\n/m.match(post.raw)[0] lists = /(^[ ]*- .+?$\n)\n/m.match(post.raw)
post.raw = post.raw.sub(first_list, "[poll]\n#{first_list}\n[/poll]") 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")
else else
post.raw = post.raw + " " post.raw = post.raw + " "
end end