Commit e4eebdf7020c495bb4cc12f30c174ce48a5e305b

Authored by Victor Costa
1 parent 815ff510

comment_paragraph: force quotes when exporting comments

plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
1 -require 'csv'  
2 class CommentParagraphPluginProfileController < CommentController 1 class CommentParagraphPluginProfileController < CommentController
3 append_view_path File.join(File.dirname(__FILE__) + '/../../views') 2 append_view_path File.join(File.dirname(__FILE__) + '/../../views')
4 3
plugins/comment_paragraph/lib/comment_paragraph_plugin/comments_report.rb
  1 +require 'csv'
  2 +
1 module CommentParagraphPlugin::CommentsReport 3 module CommentParagraphPlugin::CommentsReport
2 4
3 #FIXME make this test 5 #FIXME make this test
@@ -15,7 +17,7 @@ module CommentParagraphPlugin::CommentsReport @@ -15,7 +17,7 @@ module CommentParagraphPlugin::CommentsReport
15 @export << create_comment_element(comment, paragraph, paragraph_id) 17 @export << create_comment_element(comment, paragraph, paragraph_id)
16 end 18 end
17 else # There are no comments for this paragraph 19 else # There are no comments for this paragraph
18 - @export << { paragraph_id: paragraph_id, paragraph_text: paragraph } 20 + @export << create_comment_element(nil, paragraph, paragraph_id)
19 end 21 end
20 paragraph_id += 1 22 paragraph_id += 1
21 end 23 end
@@ -27,7 +29,7 @@ module CommentParagraphPlugin::CommentsReport @@ -27,7 +29,7 @@ module CommentParagraphPlugin::CommentsReport
27 return _("No comments for article[%{id}]: %{path}\n\n") % {:id => article.id, :path => article.path} if @export.empty? 29 return _("No comments for article[%{id}]: %{path}\n\n") % {:id => article.id, :path => article.path} if @export.empty?
28 30
29 column_names = @export.first.keys 31 column_names = @export.first.keys
30 - CSV.generate do |csv| 32 + CSV.generate(force_quotes: true) do |csv|
31 csv << column_names 33 csv << column_names
32 @export.each { |x| csv << x.values } 34 @export.each { |x| csv << x.values }
33 end 35 end
@@ -38,13 +40,13 @@ module CommentParagraphPlugin::CommentsReport @@ -38,13 +40,13 @@ module CommentParagraphPlugin::CommentsReport
38 def create_comment_element(comment, paragraph, paragraph_id) 40 def create_comment_element(comment, paragraph, paragraph_id)
39 { 41 {
40 paragraph_id: paragraph_id, 42 paragraph_id: paragraph_id,
41 - paragraph_text: paragraph.present? ? paragraph.text : nil,  
42 - comment_id: comment.id,  
43 - comment_reply_to: comment.reply_of_id,  
44 - comment_title: comment.title,  
45 - comment_content: comment.body,  
46 - comment_author_name: comment.author_name,  
47 - comment_author_email: comment.author_email 43 + paragraph_text: paragraph.present? ? paragraph.text.strip : nil,
  44 + comment_id: comment.present? ? comment.id : '-',
  45 + comment_reply_to: comment.present? ? comment.reply_of_id : '-',
  46 + comment_title: comment.present? ? comment.title : '-',
  47 + comment_content: comment.present? ? comment.body : '-',
  48 + comment_author_name: comment.present? ? comment.author_name : '-',
  49 + comment_author_email: comment.present? ? comment.author_email : '-'
48 } 50 }
49 end 51 end
50 52
plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
@@ -56,8 +56,8 @@ class CommentParagraphPluginProfileControllerTest &lt; ActionController::TestCase @@ -56,8 +56,8 @@ class CommentParagraphPluginProfileControllerTest &lt; ActionController::TestCase
56 xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id 56 xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id
57 assert_equal 'text/csv; charset=UTF-8; header=present', @response.content_type 57 assert_equal 'text/csv; charset=UTF-8; header=present', @response.content_type
58 lines = @response.body.split("\n") 58 lines = @response.body.split("\n")
59 - assert_equal "paragraph_text,comment_id,comment_title,comment_content,comment_author_name,comment_author_email", lines.first  
60 - assert_equal ",#{comment2.id},b comment,b comment,#{comment2.author_name},#{comment2.author_email}", lines.second 59 + assert_equal '"paragraph_id","paragraph_text","comment_id","comment_reply_to","comment_title","comment_content","comment_author_name","comment_author_email"', lines.first
  60 + assert_equal "\"\",\"\",\"#{comment2.id}\",\"\",\"b comment\",\"b comment\",\"#{comment2.author_name}\",\"#{comment2.author_email}\"", lines.second
61 end 61 end
62 62
63 should 'not export any comments as CSV' do 63 should 'not export any comments as CSV' do
plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb
@@ -69,7 +69,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase @@ -69,7 +69,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
69 article = fast_create(Article, :profile_id => profile.id) 69 article = fast_create(Article, :profile_id => profile.id)
70 article.expects(:comment_paragraph_plugin_enabled?).returns(true) 70 article.expects(:comment_paragraph_plugin_enabled?).returns(true)
71 article.expects(:allow_edit?).with(user).returns(true) 71 article.expects(:allow_edit?).with(user).returns(true)
72 - article.expects(:comment_paragraph_plugin_activated?).returns(false) 72 + article.expects(:comment_paragraph_plugin_activated?).at_least_once.returns(false)
73 73
74 assert_equal 'Activate Comments', plugin.article_extra_toolbar_buttons(article).first[:title] 74 assert_equal 'Activate Comments', plugin.article_extra_toolbar_buttons(article).first[:title]
75 end 75 end
@@ -79,7 +79,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase @@ -79,7 +79,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
79 article = fast_create(Article, :profile_id => profile.id) 79 article = fast_create(Article, :profile_id => profile.id)
80 article.expects(:comment_paragraph_plugin_enabled?).returns(true) 80 article.expects(:comment_paragraph_plugin_enabled?).returns(true)
81 article.expects(:allow_edit?).with(user).returns(true) 81 article.expects(:allow_edit?).with(user).returns(true)
82 - article.expects(:comment_paragraph_plugin_activated?).returns(true) 82 + article.expects(:comment_paragraph_plugin_activated?).at_least_once.returns(true)
83 83
84 assert_equal 'Deactivate Comments', plugin.article_extra_toolbar_buttons(article).first[:title] 84 assert_equal 'Deactivate Comments', plugin.article_extra_toolbar_buttons(article).first[:title]
85 end 85 end
@@ -89,9 +89,9 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase @@ -89,9 +89,9 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
89 article = fast_create(Article, :profile_id => profile.id) 89 article = fast_create(Article, :profile_id => profile.id)
90 article.expects(:comment_paragraph_plugin_enabled?).returns(true) 90 article.expects(:comment_paragraph_plugin_enabled?).returns(true)
91 article.expects(:allow_edit?).with(user).returns(true) 91 article.expects(:allow_edit?).with(user).returns(true)
92 - article.expects(:comment_paragraph_plugin_activated?).returns(false) 92 + article.expects(:comment_paragraph_plugin_activated?).at_least_once.returns(true)
93 93
94 - assert_equal 'Export Comments', plugin.article_extra_toolbar_buttons(article).last[:title] 94 + assert_includes plugin.article_extra_toolbar_buttons(article).map {|b| b[:title]}, 'Export Comments'
95 end 95 end
96 96
97 end 97 end