#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/environment' #http://ruby.bastardsbook.com/chapters/html-parsing/ puts "Runing script" article = Article.find(410) new_article = TinyMceArticle.new doc = Nokogiri::HTML(article.body) html_body = '' doc.css("p").map do |element| html_body += '
' + element.text + '
' end comments = article.comments.reorder(:group_id) comment_association = {} puts 'antes' comments.map do |c| comment_association[c.id] = c.group_id puts c.group_id.inspect end puts "Set comment group to nil" article.comments.map do |comment| comment.group_id = nil comment.save end puts "Change article body" article.body = html_body article.save puts article.body article.comment_paragraph_plugin_activate = !article.comment_paragraph_plugin_activate article.save! puts article.body puts 'depois' comments.map do |c| puts c.group_id.inspect end puts 'Migrating comments' doc = Nokogiri::HTML(article.body) group = 0 doc.css("[data-macro-paragraph_uuid]").map do |paragraph| uid = paragraph.attributes['data-macro-paragraph_uuid'].value puts "Paragraph: #{uid}" puts "Cheking paragraph group #{group} in comment #{comments[0].inspect}" comment = comments.shift if !comments.empty? && comments[0].group_id.to_s == group.to_s group += 1 next if comment.nil? puts "Change coment #{comment.id} to paragraph_uuid #{uid}" comment.paragraph_uuid = uid comment.save comment = nil end #