[Git][noosfero/noosfero][master] 4 commits: comment_paragraph: preload comment counts

Bráulio Bhavamitra gitlab at mg.gitlab.com
Thu Feb 11 16:07:54 BRST 2016


Bráulio Bhavamitra pushed to branch master at Noosfero / noosfero


Commits:
b1e38748 by Victor Costa at 2016-02-11T13:10:36-03:00
comment_paragraph: preload comment counts

- - - - -
d54a2cd1 by Victor Costa at 2016-02-11T13:21:39-03:00
comment_paragraph: load comment form only when clicked

- - - - -
c77f058d by Victor Costa at 2016-02-11T14:00:43-03:00
comment_paragraph: prioritize li tag when parsing

- - - - -
f0ec4fa4 by Bráulio Bhavamitra at 2016-02-11T18:07:43+00:00
Merge branch 'comment_paragraph_performance' into 'master'

Improve comment paragraph performance

Also fix a broken unit test

See merge request !788
- - - - -


7 changed files:

- plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
- plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb
- plugins/comment_paragraph/lib/ext/article.rb
- plugins/comment_paragraph/public/comment_paragraph_macro.js
- plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
- plugins/comment_paragraph/test/unit/allow_comment_test.rb
- plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb


Changes:

=====================================
plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
=====================================
--- a/plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
+++ b/plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
@@ -1,4 +1,4 @@
-class CommentParagraphPluginProfileController < ProfileController
+class CommentParagraphPluginProfileController < CommentController
   append_view_path File.join(File.dirname(__FILE__) + '/../../views')
 
   def view_comments
@@ -11,4 +11,14 @@ class CommentParagraphPluginProfileController < ProfileController
     render :partial => 'comment/comment.html.erb', :collection => @comments
   end
 
+  def comment_form
+    @page = profile.articles.find(params[:article_id])
+    render :partial => 'comment/comment_form', :locals => {
+      :comment => Comment.new,
+      :display_link => true,
+      :cancel_triggers_hide => true,
+      :paragraph_uuid => params[:paragraph_uuid]
+    }
+  end
+
 end


=====================================
plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb
=====================================
--- a/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb
+++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb
@@ -10,7 +10,8 @@ class CommentParagraphPlugin::AllowComment < Noosfero::Plugin::Macro
   def parse(params, inner_html, source)
     paragraph_uuid = params[:paragraph_uuid]
     article = source
-    count = article.paragraph_comments.without_spam.in_paragraph(paragraph_uuid).count
+    @paragraph_comments_counts ||= article.paragraph_comments.without_spam.group(:paragraph_uuid).reorder(:paragraph_uuid).count
+    count = @paragraph_comments_counts.fetch(paragraph_uuid, 0)
 
     proc {
       if controller.kind_of?(ContentViewerController) && article.comment_paragraph_plugin_activated?


=====================================
plugins/comment_paragraph/lib/ext/article.rb
=====================================
--- a/plugins/comment_paragraph/lib/ext/article.rb
+++ b/plugins/comment_paragraph/lib/ext/article.rb
@@ -36,7 +36,7 @@ class Article
     if body && (body_changed? || setting_changed?(:comment_paragraph_plugin_activate))
       updated = body_changed? ? body_change[1] : body
       doc =  Nokogiri::HTML(updated)
-      doc.css('li, body > div, body > span, body > p').each do |paragraph|
+      (doc.css('li') + doc.css('body > div, body > span, body > p')).each do |paragraph|
         next if paragraph.css('[data-macro="comment_paragraph_plugin/allow_comment"]').present? || paragraph.content.blank?
 
         commentable = Nokogiri::XML::Node.new("span", doc)


=====================================
plugins/comment_paragraph/public/comment_paragraph_macro.js
=====================================
--- a/plugins/comment_paragraph/public/comment_paragraph_macro.js
+++ b/plugins/comment_paragraph/public/comment_paragraph_macro.js
@@ -80,6 +80,12 @@ jQuery(document).ready(function($) {
         container.find('.display-comment-form').show();
       }
     });
+    var formDiv = container.find('.side-comment .post_comment_box');
+    if(formDiv.find('.page-comment-form').length==0) {
+      $.ajax(formDiv.data('comment_paragraph_form_url')).done(function(data) {
+        formDiv.append(data);
+      });
+    }
   });
 
 


=====================================
plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
=====================================
--- a/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
+++ b/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
@@ -10,6 +10,9 @@ class CommentParagraphPluginProfileControllerTest < ActionController::TestCase
     @profile = create_user('testuser').person
     @article = profile.articles.build(:name => 'test')
     @article.save!
+    @environment = Environment.default
+    @environment.enabled_plugins = ['CommentParagraphPlugin']
+    @environment.save!
   end
   attr_reader :article, :profile
 
@@ -39,4 +42,12 @@ class CommentParagraphPluginProfileControllerTest < ActionController::TestCase
     assert_match /d comment/, @response.body
   end
 
+  should 'load the comment form for a paragraph' do
+    login_as('testuser')
+    comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_uuid => 0)
+    xhr :get, :comment_form, :profile => @profile.identifier, :article_id => article.id, :paragraph_uuid => 0
+    assert_select ".page-comment-form"
+    assert_select "#comment_paragraph_uuid[value=?]", '0'
+  end
+
 end


=====================================
plugins/comment_paragraph/test/unit/allow_comment_test.rb
=====================================
--- a/plugins/comment_paragraph/test/unit/allow_comment_test.rb
+++ b/plugins/comment_paragraph/test/unit/allow_comment_test.rb
@@ -46,4 +46,13 @@ class AllowCommentTest < ActiveSupport::TestCase
     assert_equal 'inner', instance_eval(&content)
   end
 
+  should 'preload comment counts when parsing content' do
+    3.times { fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) }
+    content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article)
+    paragraph_comments_counts = macro.instance_variable_get(:@paragraph_comments_counts)
+    assert_equivalent ['1', '2'], paragraph_comments_counts.keys
+    assert_equal 1, paragraph_comments_counts['1']
+    assert_equal 3, paragraph_comments_counts['2']
+  end
+
 end


=====================================
plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb
=====================================
--- a/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb
+++ b/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb
@@ -11,14 +11,14 @@
   </div>
 
   <% load_comments_url = url_for({:profile => profile_identifier, :controller => 'comment_paragraph_plugin_profile', :action => 'view_comments', :paragraph_uuid => paragraph_uuid, :article_id => article_id}) %>
+  <% load_comment_form_url = url_for({:profile => profile_identifier, :controller => 'comment_paragraph_plugin_profile', :action => 'comment_form', :paragraph_uuid => paragraph_uuid, :article_id => article_id}) %>
 
   <div class="side-comment" data-comment_paragraph_url="<%= load_comments_url %>">
     <div class="article-comments-list">
       <div class="loading"></div>
     </div>
     <div class ="article-comments-list-more"></div>
-    <div class='post_comment_box closed'>
-      <%= render :partial => 'comment/comment_form', :locals => {:comment => Comment.new, :display_link => true, :cancel_triggers_hide => true, :paragraph_uuid => paragraph_uuid}%>
+    <div class='post_comment_box closed' data-comment_paragraph_form_url="<%= load_comment_form_url %>">
     </div>
   </div>
 </div>



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/ee2b501a2e2bf2f58d2fa3475a13cde8b27df9b5...f0ec4fa49a11b9f72cc80affcad105e04e3f8510
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160211/8bf6daed/attachment-0001.html>


More information about the Noosfero-dev mailing list