Skip to content

Instantly share code, notes, and snippets.

@xenda
Created January 2, 2012 22:55
Show Gist options
  • Save xenda/1552515 to your computer and use it in GitHub Desktop.
Save xenda/1552515 to your computer and use it in GitHub Desktop.
module CommentSpamValidator
def self.included(model)
model.class_eval do
before_save :check_if_spam?
end
end
def mark_as_spam
self.spam = true
end
def content_method
if self.respond_to? :content
self.content
else
self.texto
end
end
def filtered_words
filtered(:words)
end
def filtered_emails
filtered(:emails)
end
def filtered(kind)
return spam_options.send("filtered_#{kind}").split("\n") if spam_options
[]
end
def spam_options
@spam_options ||= SpamOptions.first
end
def spamming_words
filtered_words.inject(0){|sum, current| sum += (content_method.include?(current) ? 1 : 0 ) }
end
def contains_filtered_emails?
filtered_emails.include? self.email
end
def check_if_spam?
if spamming_words > 2 or contains_filtered_emails?
mark_as_spam
end
end
def spammed?
self.class.find(:all, :conditions => "spam is true")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment