From c10c9a67eb72ba199c6c5c64c85e14bd89a4279c Mon Sep 17 00:00:00 2001 From: Evandro Jr Date: Mon, 8 Jun 2015 16:44:24 -0300 Subject: [PATCH] community_track: fix recursive comment count --- plugins/community_track/lib/community_track_plugin/track.rb | 23 +++++++++++++++++++++-- plugins/community_track/test/unit/community_track_plugin/track_test.rb | 16 ++++++++++++++-- public/plugins/community_track | 1 + 3 files changed, 36 insertions(+), 4 deletions(-) create mode 120000 public/plugins/community_track diff --git a/plugins/community_track/lib/community_track_plugin/track.rb b/plugins/community_track/lib/community_track_plugin/track.rb index 4df5ad6..212a6d0 100644 --- a/plugins/community_track/lib/community_track_plugin/track.rb +++ b/plugins/community_track/lib/community_track_plugin/track.rb @@ -7,6 +7,16 @@ class CommunityTrackPlugin::Track < Folder attr_accessible :goals, :expected_results + @children_comments + + def comments_count + if defined?(@comments_count) + @comments_count + else + @comments_count = update_comments_count + end + end + def validate_categories errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank? end @@ -49,8 +59,17 @@ class CommunityTrackPlugin::Track < Folder false end - def comments_count - steps_unsorted.joins(:children).sum('children_articles.comments_count') + def update_comments_count + @children_comments = 0 + sum_children_comments self + @children_comments + end + + def sum_children_comments node + node.children.each do |c| + @children_comments += c.comments_count + sum_children_comments c + end end def css_class_name diff --git a/plugins/community_track/test/unit/community_track_plugin/track_test.rb b/plugins/community_track/test/unit/community_track_plugin/track_test.rb index ce4602d..f34be43 100644 --- a/plugins/community_track/test/unit/community_track_plugin/track_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/track_test.rb @@ -3,10 +3,12 @@ require File.dirname(__FILE__) + '/../../test_helper' class TrackTest < ActiveSupport::TestCase def setup - @profile = fast_create(Community) + @profile = create(Community) @track = create_track('track', @profile) @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => @profile) + @track.children << @step @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id) + @step.children << @tool end should 'describe yourself' do @@ -25,8 +27,18 @@ class TrackTest < ActiveSupport::TestCase assert_equal 0, @track.comments_count owner = create_user('testuser').person article = create(Article, :name => 'article', :parent_id => @step.id, :profile_id => owner.id) + @track.children << @step + @step.children << article comment = create(Comment, :source => article, :author_id => owner.id) - assert_equal 1, @track.comments_count + @step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step2', :profile => @profile) + @step2.tool_type = 'Forum' + forum = fast_create(Forum, :parent_id => @step2.id) + article_forum = create(Article, :name => 'article_forum', :parent_id => forum.id, :profile_id => owner.id) + forum.children << article_forum + forum_comment = create(Comment, :source => article_forum, :author_id => owner.id) + @track.stubs(:children).returns([@step, @step2]) + #@track.children = [@step, @step2] + assert_equal 2, @track.comments_count end should 'return children steps' do diff --git a/public/plugins/community_track b/public/plugins/community_track new file mode 120000 index 0000000..c8f295b --- /dev/null +++ b/public/plugins/community_track @@ -0,0 +1 @@ +/home/93274300500/projetos/noosfero/config/plugins/community_track/public \ No newline at end of file -- libgit2 0.21.2