Commit b1e38748811821c3fe69539c7937950c4e00de61

Authored by Victor Costa
1 parent 833e0012

comment_paragraph: preload comment counts

plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb
@@ -10,7 +10,8 @@ class CommentParagraphPlugin::AllowComment < Noosfero::Plugin::Macro @@ -10,7 +10,8 @@ class CommentParagraphPlugin::AllowComment < Noosfero::Plugin::Macro
10 def parse(params, inner_html, source) 10 def parse(params, inner_html, source)
11 paragraph_uuid = params[:paragraph_uuid] 11 paragraph_uuid = params[:paragraph_uuid]
12 article = source 12 article = source
13 - count = article.paragraph_comments.without_spam.in_paragraph(paragraph_uuid).count 13 + @paragraph_comments_counts ||= article.paragraph_comments.without_spam.group(:paragraph_uuid).reorder(:paragraph_uuid).count
  14 + count = @paragraph_comments_counts.fetch(paragraph_uuid, 0)
14 15
15 proc { 16 proc {
16 if controller.kind_of?(ContentViewerController) && article.comment_paragraph_plugin_activated? 17 if controller.kind_of?(ContentViewerController) && article.comment_paragraph_plugin_activated?
plugins/comment_paragraph/test/unit/allow_comment_test.rb
@@ -46,4 +46,13 @@ class AllowCommentTest < ActiveSupport::TestCase @@ -46,4 +46,13 @@ class AllowCommentTest < ActiveSupport::TestCase
46 assert_equal 'inner', instance_eval(&content) 46 assert_equal 'inner', instance_eval(&content)
47 end 47 end
48 48
  49 + should 'preload comment counts when parsing content' do
  50 + 3.times { fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) }
  51 + content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article)
  52 + paragraph_comments_counts = macro.instance_variable_get(:@paragraph_comments_counts)
  53 + assert_equivalent ['1', '2'], paragraph_comments_counts.keys
  54 + assert_equal 1, paragraph_comments_counts['1']
  55 + assert_equal 3, paragraph_comments_counts['2']
  56 + end
  57 +
49 end 58 end