Commit c10c9a67eb72ba199c6c5c64c85e14bd89a4279c
1 parent
3ce0f54e
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
community_track: fix recursive comment count
Showing
3 changed files
with
36 additions
and
4 deletions
Show diff stats
plugins/community_track/lib/community_track_plugin/track.rb
... | ... | @@ -7,6 +7,16 @@ class CommunityTrackPlugin::Track < Folder |
7 | 7 | |
8 | 8 | attr_accessible :goals, :expected_results |
9 | 9 | |
10 | + @children_comments | |
11 | + | |
12 | + def comments_count | |
13 | + if defined?(@comments_count) | |
14 | + @comments_count | |
15 | + else | |
16 | + @comments_count = update_comments_count | |
17 | + end | |
18 | + end | |
19 | + | |
10 | 20 | def validate_categories |
11 | 21 | errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank? |
12 | 22 | end |
... | ... | @@ -49,8 +59,17 @@ class CommunityTrackPlugin::Track < Folder |
49 | 59 | false |
50 | 60 | end |
51 | 61 | |
52 | - def comments_count | |
53 | - steps_unsorted.joins(:children).sum('children_articles.comments_count') | |
62 | + def update_comments_count | |
63 | + @children_comments = 0 | |
64 | + sum_children_comments self | |
65 | + @children_comments | |
66 | + end | |
67 | + | |
68 | + def sum_children_comments node | |
69 | + node.children.each do |c| | |
70 | + @children_comments += c.comments_count | |
71 | + sum_children_comments c | |
72 | + end | |
54 | 73 | end |
55 | 74 | |
56 | 75 | def css_class_name | ... | ... |
plugins/community_track/test/unit/community_track_plugin/track_test.rb
... | ... | @@ -3,10 +3,12 @@ require File.dirname(__FILE__) + '/../../test_helper' |
3 | 3 | class TrackTest < ActiveSupport::TestCase |
4 | 4 | |
5 | 5 | def setup |
6 | - @profile = fast_create(Community) | |
6 | + @profile = create(Community) | |
7 | 7 | @track = create_track('track', @profile) |
8 | 8 | @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => @profile) |
9 | + @track.children << @step | |
9 | 10 | @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id) |
11 | + @step.children << @tool | |
10 | 12 | end |
11 | 13 | |
12 | 14 | should 'describe yourself' do |
... | ... | @@ -25,8 +27,18 @@ class TrackTest < ActiveSupport::TestCase |
25 | 27 | assert_equal 0, @track.comments_count |
26 | 28 | owner = create_user('testuser').person |
27 | 29 | article = create(Article, :name => 'article', :parent_id => @step.id, :profile_id => owner.id) |
30 | + @track.children << @step | |
31 | + @step.children << article | |
28 | 32 | comment = create(Comment, :source => article, :author_id => owner.id) |
29 | - assert_equal 1, @track.comments_count | |
33 | + @step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step2', :profile => @profile) | |
34 | + @step2.tool_type = 'Forum' | |
35 | + forum = fast_create(Forum, :parent_id => @step2.id) | |
36 | + article_forum = create(Article, :name => 'article_forum', :parent_id => forum.id, :profile_id => owner.id) | |
37 | + forum.children << article_forum | |
38 | + forum_comment = create(Comment, :source => article_forum, :author_id => owner.id) | |
39 | + @track.stubs(:children).returns([@step, @step2]) | |
40 | + #@track.children = [@step, @step2] | |
41 | + assert_equal 2, @track.comments_count | |
30 | 42 | end |
31 | 43 | |
32 | 44 | should 'return children steps' do | ... | ... |