I know what you’re thinking: what kind of lazy ass are your Marc ? Did you get a life, lost your computer, your girlfriend went into labour ? More then a month since I released a project! Wow, I must have been pretty busy…
While talking with Carl at the last Montreal on Rails meeting, it occurred to me that it would be cool to integrate spam filtering with Defensio in my next project (more details on this eventually).
Defensio has received good and bad reviews, but from what I understand, it really gets better with time as opposite to Akismet who plateau pretty soon. I think one of the reason why Defensio might be better is that it uses much more data to classify your comments. Not just the content and user IP but the author email, the date of the article and the color of the sox the author’s sister’s father in law wore last weekend. That might be hard and tortuous to supply, especially when he’s on vacation. So that’s why I though I could make a Rails plugin that make it easy to supply all this info without hunting into your sister’s father in law wardrobe.
Get to the point already
1) The first thing your need to do to use the defensio Rails plugin is to install it:
script/plugin install http://code.macournoyer.com/svn/plugins/defensio/
2) Then you might want to create a config file in /yourapp/config/defensio.yml
development:
api_key: 1234abc
owner_url: http://code.macournoyer.com/svn/plugins/defensio
test:
api_key: 1234abc
owner_url: http://code.macournoyer.com/svn/plugins/defensio
production:
api_key: 6789xyz
owner_url: http://code.macournoyer.com/svn/plugins/defensio
You’ll need to get an API key at this point. Ask the guys at Defensio, or spam Carl until he gives you one.
3) Next step is to add the columns used to store the comment spaminess level
script/generate defensio_migration comment
(Replace comment with the name of your model that you want to be marked as spam/ham)
Run it like there’s no tomorrow !
rake db:migrate
4) In your article class (the one that holds the comments), add:
acts_as_defensio_article
By default the following fields will be looked up for values: author, author_email, title, content, permalink.
You can change those fields with the :fields options, like this:
acts_as_defensio_article :fields => { :content => :body }
The content value will be fetched from the body method.
5) In your comment class (the one that can be spam or ham), add:
acts_as_defensio_comment
Same things apply with the :fields thinggy. Looking in the following fields by default: author, content, title, author_email, author_url, article.permalink, trusted_user and logged_in.
6) Add some magic in your controller:
class CommentsController < ApplicationController
def index
@ham = @article.comments.find_all_by_spam(false)
@spam = @article.comments.find_all_by_spam(true, :order => ‘spaminess desc‘)
end
def create
@comment = @article.comments.build(params[:comment])
@comment.env = request.env
if @comment.save
if @comment.spam
flash[:notice] = ‘Your comment has been marked for review‘
else
flash[:notice] = ‘Comment created‘
end
redirect_to article_url(@article)
else
render :action => ‘new‘
end
end
def report_as_spam
@comment = @article.comments.find(params[:id])
@comment.report_as_spam
end
def report_as_ham
@comment = @article.comments.find(params[:id])
@comment.report_as_ham
end
def stats
@stats = Comment.defensio_stats
end
end
You’re done! Now go find some spammers !
Beta aka buggy
I’ve started to use this plugin myself in one of my personal project but not for very long. It might contain some bugz. Although I doubt it… I never write bugs. If you encounter any unwanted features (aka bug) please let me know in the comments here.
Thanks to the guys at Karabunga for making Defensio, using the API was a real pleasure!
Update: There are more detailed code sample in the plugin svn: http://code.macournoyer.com/svn/plugins/defensio/example/
Update: Documentation is online.









Hi - great plugin - comprehensive functionality although the documentation and example could be more verbose.
Question: is there a simple switch to bypass Defensio when in dev mode? I’m just concerned that I will get marked as a spammer by Defensio from all my dev posts, or from testing my “mark as spam” links etc! Or will Defensio see my IP as 127.0.0.1 and ignore them effectively?
Hi Adam,
You can specify a different key in the defensio.yml file or you can simply make the act_as conditionnal:
acts_as_defension_comment … if RAILS_ENV == ‘production’
their’s some examples here: http://code.macournoyer.com/svn/plugins/defensio/example/
hope this helps!