Commit 4fcabe81255faed08809913164e770719ba4c64a
1 parent
9bca97e7
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Adding comment paragraph script
Showing
1 changed file
with
70 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,70 @@ | @@ -0,0 +1,70 @@ | ||
| 1 | +#!/usr/bin/env ruby | ||
| 2 | +require File.dirname(__FILE__) + '/../config/environment' | ||
| 3 | + | ||
| 4 | +#http://ruby.bastardsbook.com/chapters/html-parsing/ | ||
| 5 | + | ||
| 6 | +puts "Runing script" | ||
| 7 | + | ||
| 8 | +article = Article.find(410) | ||
| 9 | +new_article = TinyMceArticle.new | ||
| 10 | + | ||
| 11 | +doc = Nokogiri::HTML(article.body) | ||
| 12 | +html_body = '' | ||
| 13 | +doc.css("p").map do |element| | ||
| 14 | + html_body += '<p>' + element.text + '</p>' | ||
| 15 | +end | ||
| 16 | + | ||
| 17 | +comments = article.comments.reorder(:group_id) | ||
| 18 | +comment_association = {} | ||
| 19 | + | ||
| 20 | +puts 'antes' | ||
| 21 | +comments.map do |c| | ||
| 22 | + comment_association[c.id] = c.group_id | ||
| 23 | + puts c.group_id.inspect | ||
| 24 | +end | ||
| 25 | + | ||
| 26 | +puts "Set comment group to nil" | ||
| 27 | +article.comments.map do |comment| | ||
| 28 | + comment.group_id = nil | ||
| 29 | + comment.save | ||
| 30 | +end | ||
| 31 | + | ||
| 32 | +puts "Change article body" | ||
| 33 | +article.body = html_body | ||
| 34 | +article.save | ||
| 35 | +puts article.body | ||
| 36 | +article.comment_paragraph_plugin_activate = !article.comment_paragraph_plugin_activate | ||
| 37 | +article.save! | ||
| 38 | +puts article.body | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + | ||
| 42 | +puts 'depois' | ||
| 43 | +comments.map do |c| | ||
| 44 | + puts c.group_id.inspect | ||
| 45 | +end | ||
| 46 | +puts 'Migrating comments' | ||
| 47 | + | ||
| 48 | +doc = Nokogiri::HTML(article.body) | ||
| 49 | +group = 0 | ||
| 50 | +doc.css("[data-macro-paragraph_uuid]").map do |paragraph| | ||
| 51 | + uid = paragraph.attributes['data-macro-paragraph_uuid'].value | ||
| 52 | + puts "Paragraph: #{uid}" | ||
| 53 | + puts "Cheking paragraph group #{group} in comment #{comments[0].inspect}" | ||
| 54 | + comment = comments.shift if !comments.empty? && comments[0].group_id.to_s == group.to_s | ||
| 55 | + group += 1 | ||
| 56 | + next if comment.nil? | ||
| 57 | + puts "Change coment #{comment.id} to paragraph_uuid #{uid}" | ||
| 58 | + comment.paragraph_uuid = uid | ||
| 59 | + comment.save | ||
| 60 | + comment = nil | ||
| 61 | +end | ||
| 62 | + | ||
| 63 | +#<Comment id: 4, title: "", body: "teste 1", source_id: 410, author_id: 54, name: nil, email: nil, created_at: "2015-09-29 18:54:32", reply_of_id: nil, ip_address: "127.0.0.1", spam: nil, source_type: "Article", user_agent: "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (K...", referrer: "http://localhost:3000/adminuser/coment-group-plugin...", settings: {:comment_paragraph_selected_area=>nil, :comment_paragraph_selected_content=>nil}, paragraph_id: nil, paragraph_uuid: nil, group_id: 0> | ||
| 64 | + | ||
| 65 | +#<Comment id: 2, title: "", body: "teste", source_id: 409, author_id: 54, name: nil, email: nil, created_at: "2015-09-29 18:54:11", reply_of_id: nil, ip_address: "127.0.0.1", spam: nil, source_type: "Article", user_agent: "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (K...", referrer: "http://localhost:3000/adminuser/comentario-por-para...", settings: {:comment_paragraph_selected_area=>nil, :comment_paragraph_selected_content=>nil}, paragraph_id: nil, paragraph_uuid: "9445dee8-0a65-4d5b-a654-5dd0e68b1db3", group_id: nil> | ||
| 66 | + | ||
| 67 | + | ||
| 68 | + | ||
| 69 | +puts new_article.body | ||
| 70 | +# |