diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb index 82043d4..1aa06fc 100644 --- a/app/helpers/content_viewer_helper.rb +++ b/app/helpers/content_viewer_helper.rb @@ -10,7 +10,7 @@ module ContentViewerHelper end def number_of_comments(article) - display_number_of_comments(article.comments.without_spam.count) + display_number_of_comments(article.comments_count - article.spam_comments_count) end def article_title(article, args = {}) diff --git a/app/models/article.rb b/app/models/article.rb index fdef53a..e183453 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -450,9 +450,9 @@ class Article < ActiveRecord::Base end named_scope :published, :conditions => { :published => true } - named_scope :folders, lambda {|profile|{:conditions => { :type => profile.folder_types} }} - named_scope :no_folders, lambda {|profile|{:conditions => ['type NOT IN (?)', profile.folder_types]}} - named_scope :galleries, :conditions => { :type => 'Gallery' } + named_scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types] }} + named_scope :no_folders, lambda {|profile|{:conditions => ['articles.type NOT IN (?)', profile.folder_types]}} + named_scope :galleries, :conditions => [ "articles.type IN ('Gallery')" ] named_scope :images, :conditions => { :is_image => true } named_scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ] named_scope :with_types, lambda { |types| { :conditions => [ 'articles.type IN (?)', types ] } } diff --git a/app/models/comment.rb b/app/models/comment.rb index 4df38ee..1f7e5c6 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -107,14 +107,17 @@ class Comment < ActiveRecord::Base include Noosfero::Plugin::HotSpot include Spammable + include CacheCounterHelper def after_spam! SpammerLogger.log(ip_address, self) Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam)) + update_cache_counter(:spam_comments_count, source, 1) if source.kind_of?(Article) end def after_ham! Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham)) + update_cache_counter(:spam_comments_count, source, -1) if source.kind_of?(Article) end def verify_and_notify diff --git a/db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb b/db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb new file mode 100644 index 0000000..6edd44b --- /dev/null +++ b/db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb @@ -0,0 +1,12 @@ +class AddSpamCommentsCounterCacheToArticles < ActiveRecord::Migration + def self.up + add_column :articles, :spam_comments_count, :integer, :default => 0 + add_column :article_versions, :spam_comments_count, :integer, :default => 0 + 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');" + end + + def self.down + remove_column :articles, :spam_comments_count + remove_column :article_versions, :spam_comments_count + end +end diff --git a/db/schema.rb b/db/schema.rb index 5422600..5c97896 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140505131703) do +ActiveRecord::Schema.define(:version => 20140709212646) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -93,6 +93,7 @@ ActiveRecord::Schema.define(:version => 20140505131703) do t.integer "license_id" t.integer "image_id" t.integer "position" + t.integer "spam_comments_count", :default => 0 end add_index "article_versions", ["article_id"], :name => "index_article_versions_on_article_id" @@ -138,6 +139,7 @@ ActiveRecord::Schema.define(:version => 20140505131703) do t.integer "license_id" t.integer "image_id" t.integer "position" + t.integer "spam_comments_count", :default => 0 end add_index "articles", ["comments_count"], :name => "index_articles_on_comments_count" diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 990e9df..d931263 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1824,7 +1824,7 @@ class ArticleTest < ActiveSupport::TestCase end end - should 'return articles with specific types' do + should 'return articles with specific types' do Article.delete_all c1 = fast_create(TinyMceArticle, :name => 'Testing article 1', :body => 'Article body 1', :profile_id => profile.id) -- libgit2 0.21.2