Commit 924c784defe626ce71cfb5eda73d9e923023d448
Exists in
staging
and in
2 other branches
Merge branch 'export-comment-paragraph-rails3' into staging
Showing
5 changed files
with
99 additions
and
5 deletions
Show diff stats
plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
1 | +require 'csv' | |
1 | 2 | class CommentParagraphPluginProfileController < ProfileController |
2 | 3 | append_view_path File.join(File.dirname(__FILE__) + '/../../views') |
3 | 4 | |
... | ... | @@ -11,4 +12,16 @@ class CommentParagraphPluginProfileController < ProfileController |
11 | 12 | render :partial => 'comment/comment.html.erb', :collection => @comments |
12 | 13 | end |
13 | 14 | |
15 | + include CommentParagraphPlugin::CommentsReport | |
16 | + | |
17 | + def export_comments | |
18 | + article_id = params[:id] | |
19 | + article = profile.articles.find(article_id) | |
20 | + result = export_comments_csv(article) | |
21 | + filename = "comments_for_article#{article_id}_#{DateTime.now.to_i}.csv" | |
22 | + send_data result, | |
23 | + :type => 'text/csv; charset=UTF-8; header=present', | |
24 | + :disposition => "attachment; filename=#{filename}" | |
25 | + end | |
26 | + | |
14 | 27 | end | ... | ... |
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
... | ... | @@ -47,12 +47,18 @@ class CommentParagraphPlugin < Noosfero::Plugin |
47 | 47 | def article_extra_toolbar_buttons(article) |
48 | 48 | user = context.send :user |
49 | 49 | return [] if !article.comment_paragraph_plugin_enabled? || !article.allow_edit?(user) |
50 | - { | |
50 | + [ | |
51 | + { | |
51 | 52 | :title => article.comment_paragraph_plugin_activated? ? _('Deactivate Comments') : _('Activate Comments'), |
52 | 53 | :url => {:controller => 'comment_paragraph_plugin_myprofile', :profile => article.profile.identifier, :action => 'toggle_activation', :id => article.id}, |
53 | 54 | :icon => :toggle_comment_paragraph |
54 | - } | |
55 | - | |
55 | + }, | |
56 | + { | |
57 | + :title => _('Export Comments'), | |
58 | + :url => {:controller => 'comment_paragraph_plugin_profile', :profile => article.profile.identifier, :action => 'export_comments', :id => article.id}, | |
59 | + :icon => :toggle_comment_paragraph | |
60 | + } | |
61 | + ] | |
56 | 62 | end |
57 | 63 | |
58 | 64 | end | ... | ... |
plugins/comment_paragraph/lib/comment_paragraph_plugin/comments_report.rb
0 → 100644
... | ... | @@ -0,0 +1,51 @@ |
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 | + paragraph_id = 1 | |
9 | + doc.css("[data-macro-paragraph_uuid]").map do |paragraph| | |
10 | + uuid = paragraph.attributes['data-macro-paragraph_uuid'].value | |
11 | + comments_for_paragraph = comments_map[uuid] | |
12 | + if comments_for_paragraph | |
13 | + # Put comments for the paragraph | |
14 | + comments_for_paragraph.each do | comment | | |
15 | + @export << create_comment_element(comment, paragraph, paragraph_id) | |
16 | + end | |
17 | + else # There are no comments for this paragraph | |
18 | + @export << { paragraph_id: paragraph_id, paragraph_text: paragraph } | |
19 | + end | |
20 | + paragraph_id += 1 | |
21 | + end | |
22 | + # Now we need to put all other comments that are not attached to a paragraph | |
23 | + comments_without_paragrah = comments_map[nil] || [] | |
24 | + comments_without_paragrah.each do | comment | | |
25 | + @export << create_comment_element(comment, nil, nil) | |
26 | + end | |
27 | + return _("No comments for article[%{id}]: %{path}\n\n") % {:id => article.id, :path => article.path} if @export.empty? | |
28 | + | |
29 | + column_names = @export.first.keys | |
30 | + CSV.generate do |csv| | |
31 | + csv << column_names | |
32 | + @export.each { |x| csv << x.values } | |
33 | + end | |
34 | + end | |
35 | + | |
36 | + private | |
37 | + | |
38 | + def create_comment_element(comment, paragraph, paragraph_id) | |
39 | + { | |
40 | + 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 | |
48 | + } | |
49 | + end | |
50 | + | |
51 | +end | ... | ... |
plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
... | ... | @@ -39,4 +39,18 @@ class CommentParagraphPluginProfileControllerTest < ActionController::TestCase |
39 | 39 | assert_match /d comment/, @response.body |
40 | 40 | end |
41 | 41 | |
42 | + should 'export comments as CSV' do | |
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 | |
46 | + assert_equal 'text/csv; charset=UTF-8; header=present', @response.content_type | |
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 | |
50 | + end | |
51 | + | |
52 | + should 'not export any comments as CSV' do | |
53 | + xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id | |
54 | + assert_equal "No comments for article[#{article.id}]: #{article.path}", @response.body.split("\n")[0] | |
55 | + end | |
42 | 56 | end | ... | ... |
plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb
... | ... | @@ -71,7 +71,7 @@ class CommentParagraphPluginTest < ActiveSupport::TestCase |
71 | 71 | article.expects(:allow_edit?).with(user).returns(true) |
72 | 72 | article.expects(:comment_paragraph_plugin_activated?).returns(false) |
73 | 73 | |
74 | - assert_equal 'Activate Comments', plugin.article_extra_toolbar_buttons(article)[:title] | |
74 | + assert_equal 'Activate Comments', plugin.article_extra_toolbar_buttons(article).first[:title] | |
75 | 75 | end |
76 | 76 | |
77 | 77 | should 'display Deactivate Comments title if comment paragraph plugin is deactivated' do |
... | ... | @@ -81,7 +81,17 @@ class CommentParagraphPluginTest < ActiveSupport::TestCase |
81 | 81 | article.expects(:allow_edit?).with(user).returns(true) |
82 | 82 | article.expects(:comment_paragraph_plugin_activated?).returns(true) |
83 | 83 | |
84 | - assert_equal 'Deactivate Comments', plugin.article_extra_toolbar_buttons(article)[:title] | |
84 | + assert_equal 'Deactivate Comments', plugin.article_extra_toolbar_buttons(article).first[:title] | |
85 | + end | |
86 | + | |
87 | + should 'display export comments button when comment paragraph plugin is activated' do | |
88 | + profile = fast_create(Profile) | |
89 | + article = fast_create(Article, :profile_id => profile.id) | |
90 | + article.expects(:comment_paragraph_plugin_enabled?).returns(true) | |
91 | + article.expects(:allow_edit?).with(user).returns(true) | |
92 | + article.expects(:comment_paragraph_plugin_activated?).returns(false) | |
93 | + | |
94 | + assert_equal 'Export Comments', plugin.article_extra_toolbar_buttons(article).last[:title] | |
85 | 95 | end |
86 | 96 | |
87 | 97 | end | ... | ... |