Commit 01d4176e0ab9db7c5450b2e880c3e7acbe6a499c

Authored by Victor Costa
1 parent c62c8359

comment_paragraph: refactor export to csv

plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
@@ -12,68 +12,16 @@ class CommentParagraphPluginProfileController < ProfileController @@ -12,68 +12,16 @@ class CommentParagraphPluginProfileController < ProfileController
12 render :partial => 'comment/comment.html.erb', :collection => @comments 12 render :partial => 'comment/comment.html.erb', :collection => @comments
13 end 13 end
14 14
  15 + include CommentParagraphPlugin::CommentsReport
  16 +
15 def export_comments 17 def export_comments
16 article_id = params[:id] 18 article_id = params[:id]
17 article = profile.articles.find(article_id) 19 article = profile.articles.find(article_id)
18 - doc = Nokogiri::HTML(article.body)  
19 - comments = article.comments  
20 - commentsMap = comments.group_by { |cmt| cmt.paragraph_uuid }  
21 - @export = []  
22 - doc.css("[data-macro-paragraph_uuid]").map do |paragraph|  
23 - uuid = paragraph.attributes['data-macro-paragraph_uuid'].value  
24 - comments_for_paragraph = commentsMap[uuid]  
25 - if comments_for_paragraph  
26 - # Put comments for the paragraph  
27 - comments_for_paragraph.each do | cmt |  
28 - element = Hash.new  
29 - paragraph_text = paragraph.present? ? paragraph.text : nil  
30 - element["paragraph_text"] = paragraph_text  
31 - element["comment_id"] = cmt.id  
32 - element["comment_content"] = cmt.body  
33 - element["comment_author_name"] = cmt.author_name  
34 - element["comment_author_email"] = cmt.author_email  
35 - @export << element  
36 - end  
37 - else  
38 - # There are no comments for this paragraph  
39 - element = Hash.new  
40 - paragraph_text = paragraph.present? ? paragraph.text : nil  
41 - element["paragraph_text"] = paragraph_text  
42 - @export << element  
43 - end  
44 - end  
45 - # Now we need to put all other comments that are not attached to a paragraph  
46 - comments_without_paragrah = commentsMap[nil]  
47 - if (comments_without_paragrah)  
48 - comments_without_paragrah.each do | cmt |  
49 - element = Hash.new  
50 - element["paragraph_text"] = ""  
51 - element["comment_id"] = cmt.id  
52 - element["comment_content"] = cmt.body  
53 - element["comment_author_name"] = cmt.author_name  
54 - element["comment_author_email"] = cmt.author_email  
55 - @export << element  
56 - end  
57 - end  
58 - if (@export.first)  
59 - column_names = @export.first.keys  
60 - header = _("Comments for article[%{id}]: %{path}\n\n") % {:id => article_id, :path => article.path}  
61 - csv_body = CSV.generate do |csv|  
62 - csv << column_names  
63 - @export.each do |x|  
64 - csv << x.values  
65 - end  
66 - end  
67 - result = header + csv_body  
68 - else  
69 - result = _("No comments for article[%{id}]: %{path}\n\n") % {:id => article_id, :path => article.path}  
70 - end  
71 - fname = "comments_for_article#{article_id}_#{DateTime.now.to_i}.csv"  
72 - send_data result, 20 + result = export_comments_csv(article)
  21 + filename = "comments_for_article#{article_id}_#{DateTime.now.to_i}.csv"
  22 + send_data result,
73 :type => 'text/csv; charset=UTF-8; header=present', 23 :type => 'text/csv; charset=UTF-8; header=present',
74 - :disposition => "attachment; filename=#{fname}" 24 + :disposition => "attachment; filename=#{filename}"
75 end 25 end
76 26
77 -  
78 -  
79 end 27 end
plugins/comment_paragraph/lib/comment_paragraph_plugin/comments_report.rb 0 → 100644
@@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
  1 +module CommentParagraphPlugin::CommentsReport
  2 +
  3 + #FIXME make this test
  4 + def export_comments_csv(article)
  5 + comments_map = article.comments.group_by { |comment| comment.paragraph_uuid }
  6 + @export = []
  7 + doc = Nokogiri::HTML(article.body)
  8 + doc.css("[data-macro-paragraph_uuid]").map do |paragraph|
  9 + uuid = paragraph.attributes['data-macro-paragraph_uuid'].value
  10 + comments_for_paragraph = comments_map[uuid]
  11 + if comments_for_paragraph
  12 + # Put comments for the paragraph
  13 + comments_for_paragraph.each do | comment |
  14 + @export << create_comment_element(comment, paragraph)
  15 + end
  16 + else # There are no comments for this paragraph
  17 + @export << { paragraph_text: paragraph }
  18 + end
  19 + end
  20 + # Now we need to put all other comments that are not attached to a paragraph
  21 + comments_without_paragrah = comments_map[nil] || []
  22 + comments_without_paragrah.each do | comment |
  23 + @export << create_comment_element(comment, nil)
  24 + end
  25 + return _("No comments for article[%{id}]: %{path}\n\n") % {:id => article.id, :path => article.path} if @export.empty?
  26 +
  27 + column_names = @export.first.keys
  28 + CSV.generate do |csv|
  29 + csv << column_names
  30 + @export.each { |x| csv << x.values }
  31 + end
  32 + end
  33 +
  34 + private
  35 +
  36 + def create_comment_element(comment, paragraph)
  37 + {
  38 + paragraph_text: paragraph.present? ? paragraph.text : nil,
  39 + comment_id: comment.id,
  40 + comment_title: comment.title,
  41 + comment_content: comment.body,
  42 + comment_author_name: comment.author_name,
  43 + comment_author_email: comment.author_email
  44 + }
  45 + end
  46 +
  47 +end
plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
@@ -40,11 +40,13 @@ class CommentParagraphPluginProfileControllerTest &lt; ActionController::TestCase @@ -40,11 +40,13 @@ class CommentParagraphPluginProfileControllerTest &lt; ActionController::TestCase
40 end 40 end
41 41
42 should 'export comments as CSV' do 42 should 'export comments as CSV' do
43 - fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'a comment', :paragraph_uuid => nil)  
44 - fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'b comment', :body => 'b comment', :paragraph_uuid => nil) 43 + comment1 = fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'a comment', :paragraph_uuid => nil)
  44 + comment2 = fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'b comment', :body => 'b comment', :paragraph_uuid => nil)
45 xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id 45 xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id
46 assert_equal 'text/csv; charset=UTF-8; header=present', @response.content_type 46 assert_equal 'text/csv; charset=UTF-8; header=present', @response.content_type
47 - assert_equal "Comments for article[#{article.id}]: #{article.path}", @response.body.split("\n")[0] 47 + lines = @response.body.split("\n")
  48 + assert_equal "paragraph_text,comment_id,comment_title,comment_content,comment_author_name,comment_author_email", lines.first
  49 + assert_equal ",#{comment2.id},b comment,b comment,#{comment2.author_name},#{comment2.author_email}", lines.second
48 end 50 end
49 51
50 should 'not export any comments as CSV' do 52 should 'not export any comments as CSV' do