diff --git a/app/models/comment.rb b/app/models/comment.rb index f5dd826..fe55f8f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -16,9 +16,7 @@ class Comment < ActiveRecord::Base has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' - named_scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false] named_scope :without_reply, :conditions => ['reply_of_id IS NULL'] - named_scope :spam, :conditions => ['spam = ?', true] # unauthenticated authors: validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) @@ -205,27 +203,15 @@ class Comment < ActiveRecord::Base @rejected = true end - def spam? - !spam.nil? && spam - end - - def ham? - !spam.nil? && !spam - end + include Spammable - def spam! - self.spam = true - self.save! + def after_spam! SpammerLogger.log(ip_address, self) Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam)) - self end - def ham! - self.spam = false - self.save! + def after_ham! Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham)) - self end def marked_as_spam diff --git a/app/models/suggest_article.rb b/app/models/suggest_article.rb index d486323..c2b8deb 100644 --- a/app/models/suggest_article.rb +++ b/app/models/suggest_article.rb @@ -76,17 +76,13 @@ class SuggestArticle < Task _('You need to login on %{system} in order to approve or reject this article.') % { :system => target.environment.name } end - def spam! - super + def after_spam! SpammerLogger.log(ip_address, self) self.delay.marked_as_spam - self end - def ham! - super + def after_ham! self.delay.marked_as_ham - self end def marked_as_spam diff --git a/app/models/task.rb b/app/models/task.rb index cb282db..623bdae 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -235,25 +235,7 @@ class Task < ActiveRecord::Base end end - def spam? - !spam.nil? && spam - end - - def ham? - !spam.nil? && !spam - end - - def spam! - self.spam = true - self.save! - self - end - - def ham! - self.spam = false - self.save! - self - end + include Spammable protected @@ -294,8 +276,6 @@ class Task < ActiveRecord::Base named_scope :opened, :conditions => { :status => [Task::Status::ACTIVE, Task::Status::HIDDEN] } named_scope :of, lambda { |type| conditions = type ? "type LIKE '#{type}'" : "1=1"; {:conditions => [conditions]} } named_scope :order_by, lambda { |attribute, ord| {:order => "#{attribute} #{ord}"} } - named_scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false] - named_scope :spam, :conditions => ['spam = ?', true] named_scope :to, lambda { |profile| diff --git a/lib/spammable.rb b/lib/spammable.rb new file mode 100644 index 0000000..9b322d5 --- /dev/null +++ b/lib/spammable.rb @@ -0,0 +1,44 @@ +module Spammable + def self.included(recipient) + recipient.extend(ClassMethods) + end + + module ClassMethods + def self.extended (base) + base.class_eval do + named_scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false] + named_scope :spam, :conditions => ['spam = ?', true] + end + end + end + + def spam? + !spam.nil? && spam + end + + def ham? + !spam.nil? && !spam + end + + def spam! + before_spam! + self.spam = true + self.save! + after_spam! + self + end + + def ham! + before_ham! + self.spam = false + self.save! + after_ham! + self + end + + def after_spam!; end + def before_spam!; end + + def after_ham!; end + def before_ham!; end +end diff --git a/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb b/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb index d2dcefe..0101578 100644 --- a/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb +++ b/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb @@ -2,24 +2,23 @@ require 'test_helper' class AntiSpamPluginTest < ActiveSupport::TestCase - class Spammable + class SpammableContent attr_accessor :spam + include Spammable def save!; end - def spam!; end - def ham!; end - def spam?; true; end def environment; Environment.default; end end def setup - @spammable = Spammable.new + @spammable = SpammableContent.new @plugin = AntiSpamPlugin.new end attr_accessor :spammable should 'check for spam and mark as spam if server says it is spam' do + spammable.expects(:spam?).returns(true) spammable.expects(:save!) @plugin.check_for_spam(spammable) -- libgit2 0.21.2