From b1e38748811821c3fe69539c7937950c4e00de61 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 11 Feb 2016 12:57:57 -0300 Subject: [PATCH] comment_paragraph: preload comment counts --- plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb | 3 ++- plugins/comment_paragraph/test/unit/allow_comment_test.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb index 7feb8ec..8713402 100644 --- 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? diff --git a/plugins/comment_paragraph/test/unit/allow_comment_test.rb b/plugins/comment_paragraph/test/unit/allow_comment_test.rb index 121977a..8c481ab 100644 --- 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 -- libgit2 0.21.2