0

Custom spam prevention

Posted on 13 February 2008

by Robin Fisher

Inspired by Jonathan, I spent some time last night working on spam prevention for the comments here at captured sparks. I had seen a good article on integrating Akismet with Rails but as this is a home-baked CMS, and having read Jonathan’s approach to the issue decided to have a go myself.

I adopted a similar approach to Jonathan of assigning a score to a comment and then either automatically posting the comment or holding it back for approval . The validation is handled by a before_create callback in the comment model. Here’s an extract of the validation method:

body = self.body

score -= body.scan(/viagra/).size

case score
when 1..999
write_attribute(:approved, true)

At the moment, the validation is very basic and leans towards a cautious approach to automatic posting. Having said that, all comments are saved to the database for review. I’m not content enough to start automatically rejecting the comments yet.

Thank to Jonathan for the inspiration.


There are no comments yet. Why not be the first?