Commit 457c9f59eaf5b20bfe69c007d55722c1e93ef585

Authored by Rodrigo Souto
1 parent 2c6eb234

blog-page: use article comments_count instead of querying the database for it

Was necessary to create a spam_comments_count in order to get the
correct count without considering spams.

ActionItem3201
app/helpers/content_viewer_helper.rb
... ... @@ -10,7 +10,7 @@ module ContentViewerHelper
10 10 end
11 11  
12 12 def number_of_comments(article)
13   - display_number_of_comments(article.comments.without_spam.count)
  13 + display_number_of_comments(article.comments_count - article.spam_comments_count)
14 14 end
15 15  
16 16 def article_title(article, args = {})
... ...
app/models/article.rb
... ... @@ -450,9 +450,9 @@ class Article < ActiveRecord::Base
450 450 end
451 451  
452 452 named_scope :published, :conditions => { :published => true }
453   - named_scope :folders, lambda {|profile|{:conditions => { :type => profile.folder_types} }}
454   - named_scope :no_folders, lambda {|profile|{:conditions => ['type NOT IN (?)', profile.folder_types]}}
455   - named_scope :galleries, :conditions => { :type => 'Gallery' }
  453 + named_scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types] }}
  454 + named_scope :no_folders, lambda {|profile|{:conditions => ['articles.type NOT IN (?)', profile.folder_types]}}
  455 + named_scope :galleries, :conditions => [ "articles.type IN ('Gallery')" ]
456 456 named_scope :images, :conditions => { :is_image => true }
457 457 named_scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ]
458 458 named_scope :with_types, lambda { |types| { :conditions => [ 'articles.type IN (?)', types ] } }
... ...
app/models/comment.rb
... ... @@ -107,14 +107,17 @@ class Comment < ActiveRecord::Base
107 107 include Noosfero::Plugin::HotSpot
108 108  
109 109 include Spammable
  110 + include CacheCounterHelper
110 111  
111 112 def after_spam!
112 113 SpammerLogger.log(ip_address, self)
113 114 Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam))
  115 + update_cache_counter(:spam_comments_count, source, 1) if source.kind_of?(Article)
114 116 end
115 117  
116 118 def after_ham!
117 119 Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham))
  120 + update_cache_counter(:spam_comments_count, source, -1) if source.kind_of?(Article)
118 121 end
119 122  
120 123 def verify_and_notify
... ...
db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +class AddSpamCommentsCounterCacheToArticles < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :articles, :spam_comments_count, :integer, :default => 0
  4 + add_column :article_versions, :spam_comments_count, :integer, :default => 0
  5 + execute "update articles set spam_comments_count = (select count(*) from comments where comments.source_id = articles.id and comments.source_type = 'Article' and comments.spam = 't');"
  6 + end
  7 +
  8 + def self.down
  9 + remove_column :articles, :spam_comments_count
  10 + remove_column :article_versions, :spam_comments_count
  11 + end
  12 +end
... ...
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 20140505131703) do
  12 +ActiveRecord::Schema.define(:version => 20140709212646) do
13 13  
14 14 create_table "abuse_reports", :force => true do |t|
15 15 t.integer "reporter_id"
... ... @@ -93,6 +93,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140505131703) do
93 93 t.integer "license_id"
94 94 t.integer "image_id"
95 95 t.integer "position"
  96 + t.integer "spam_comments_count", :default => 0
96 97 end
97 98  
98 99 add_index "article_versions", ["article_id"], :name => "index_article_versions_on_article_id"
... ... @@ -138,6 +139,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140505131703) do
138 139 t.integer "license_id"
139 140 t.integer "image_id"
140 141 t.integer "position"
  142 + t.integer "spam_comments_count", :default => 0
141 143 end
142 144  
143 145 add_index "articles", ["comments_count"], :name => "index_articles_on_comments_count"
... ...
test/unit/article_test.rb
... ... @@ -1824,7 +1824,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1824 1824 end
1825 1825 end
1826 1826  
1827   - should 'return articles with specific types' do
  1827 + should 'return articles with specific types' do
1828 1828 Article.delete_all
1829 1829  
1830 1830 c1 = fast_create(TinyMceArticle, :name => 'Testing article 1', :body => 'Article body 1', :profile_id => profile.id)
... ...