Patch Mephisto to add a comments list filter

posted: April 4th, 2007 · by: Sven

in: Programming · tagged as: , , , , , ·  0 comments »

Mephisto is backed by Akismet when it comes to comment-spam-protection and this duo works pretty well. But like any such system it’s not 100% foolproof and therefor Rick has build it that way that comments are put aside, not completely deleted when Akismet thinks that it’s spam. You’re supposed to log into the admin interface, go to “Moderate comments” and then review them. You can approve or delete the comments separately or delete them all at once.

That’s all pretty reasonable and handy. But when I scrolled through my 900 spam comments yesterday I found myself really wanting to filter the list by keywords like “cialis” or “toyota” (what the heck is so cool about Toyota newly?). This way I could reduce the list by the largest portions in some steps and then only have to review the rest of it.

I looked at the source code and found that it’s pretty super simple to patch in support for this kind of feature. So, here you go.

The controller part

The filter keyword can be put into the URL parameters like in admin/comments?filter=toyota so nothing needs to be changed in the routes. We can access the parameter right away in the controller action with params[:filter].

Thus all that’s needed here is to change the index action of Admin::CommentsController to constrain the selected comments by the keyword if one is present.

That’s easy. We’ll just replace this line:


@comments = site.unapproved_comments.find(:all, :include => :article)

… by this slightly longer piece of code:


condition = [:body, :author, :author_url].collect do |column|
  "contents.#{column} LIKE '%#{params[:filter]}%'"
end.join(' OR ') unless params[:filter].blank?
@comments = site.unapproved_comments.find(:all, :include => :article, 
  :conditions => condition)

That’s all. You now can try and add a filter=something parameter to your URL and the comments list should be filtered accordingly.

Later on we might want to refactor this and move the condition building and querying stuff into the Site or Comment model.

The view part

Everything that remains to do now is to add some kind of HTML form to the admin interface that allows to enter a keyword and sends it to the appropriate action.

I’ve added the following to the admin/comments/index.rhtml template:


<!-- begin action nav -->
<div id="page-nav">
  ...
</div>
<div id="filter" class="manual">
  <form action="">
    <label>Filter
    <input type="text" name="filter" value="<%= params[:filter] %>">
  </form>
</div>
<!-- /end action nav --> 

And adjusted the design with the following addition to the mephisto.css stylesheet (located in the public/stylesheets/mephisto directory):


#filter {
    ...
  height: 35px;
}
#filter form {
  float: right;
  margin: 6px;
  font-weight: bold;
  text-shadow: #FFFFFF 2px 2px;
}

Here’s a patch

I’ve filed away a patch for my personal backup purposes: admin_filter_comments_list.diff. I believe it should work with any default Mephisto installation as well.

Leave a comment

Name required
E-Mail and Website optional

If you can read this, you don't use a typical webbrowser that plays nice with CSS.
Please do not fill in anything here!

Hint: Markdown will be applied to your comment. If you post any code, be sure to escape underscores (like so: \_) if you do not want them to be converted to an <em>phasis.

artweb design
Sven Fuchs
Grünberger Str. 65
10245 Berlin, Germany


http://www.artweb-design.de

Fon +49 (30) 47 98 69 96
Fax +49 (30) 47 98 69 97