Acts_as_snook - Creating an admin panel
Posted over 2 years ago by Philip Ingram
Well, I’m going to try something a little different with this post. I’ll just post some code and explain one or two things that are out of the ordinary, instead of going through a full blow by blow account of it all.
Creating an Admin Panel for Acts As Snook
I basically needed to manage my comments and the resulting spam that is associated with having a blog on the interwebs. I haven’t created one as of yet, so here it is.
The stone cold facts of what i did
Simply put, i used my existing Admin Namespace to include a Comments Controller which listed all of the spam, ham and moderate comments available to me, through acts_as_snook .
Now, acts_as_snook has these methods which can update a Comment record from one status to another. E.g., spam to ham. Sometimes acts_as_snook can’t decide whether it’s spam or ham, so it goes into a moderate status and this is when those methods really shine.
The code…
Fix your routes by adding:
map.namespace :admin do |admin|
admin.resources :comments, :member => {:ham_it => :get, :spam_it => :get}
end
Next create an Admin::CommentsController and add:
class Admin::CommentsController < ApplicationController
before_filter :get_comment, :except => :index
def index
@spam = Comment.spam
@ham = Comment.ham
@moderate = Comment.moderate
end
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to(admin_comments_url) }
format.xml { head :ok }
end
end
#change to ham
def ham_it
@comment.ham!
respond_to do |format|
format.html { redirect_to(admin_comments_url) }
format.xml { head :ok }
end
end
#change to spam
def spam_it
@comment.spam!
respond_to do |format|
format.html { redirect_to(admin_comments_url) }
format.xml { head :ok }
end
end
protected
def get_comment
@comment = Comment.find(params[:id])
end
end
Finally we need a view to use, so why not add something like this:
<h1>Ham Comments (the good ones)</h1>
<% for ham in @ham %>
<div id="admin_box">
<p><%= ham.id %> in <%= post_title(ham.post_id) %> : <%= link_to 'Destroy', [:admin, ham], :confirm => 'Are you sure?', :method => :delete %> | <%= link_to "Spam", spam_it_admin_comment_path(ham) %></p>
<p><%= h ham.body %></p></div>
<% end %>
<h1>Moderate Comments (the iffy ones)</h1>
<% for mod in @moderate %>
<div id="admin_box">
<p><%= mod.id %> in post #<%= post_title(mod.post_id) %> : <%= link_to "Spam", spam_it_admin_comment_path(mod) %> | <%= link_to "Ham", ham_it_admin_comment_path(mod) %></p>
<p><%= h mod.body %></p></div>
<% end %>
<h1>Spam Comments (the bad ones)</h1>
<% for spam in @spam %>
<div id="admin_box">
<p><%= spam.id %> in post #<%= post_title(spam.post_id) %> : <%= link_to 'Destroy', [:admin, spam], :confirm => 'Are you sure?', :method => :delete %> | <%= link_to "Ham", ham_it_admin_comment_path(spam) %></p>
<p><%= h spam.body %></p></div>
<% end %>
Remember, your Comment model schema may vary, but it’s probably similar.
Conclusion
Enjoy. I didn’t bore you with much other content then what was needed. This approach of course can be used to create any kind of admin moderation in an admin namespace.
Comments
"For forskere, en skala gir noen rask kategorisering tilkoblede med en planetens relevans som biologi, hele deres samme måte der det stellar typer umiddelbart kan fortelle en astronom noe om ett bestemt størrelse , temperatur, samt, lysstyrke assosiert med a stjerne "
Det er umulig må ikke slik at du kan varsel tips om hvordan god din utrolige hund sammen med katten føler når du gnir under sine haken aka klø bak ørene. De komfort blant hengivenhet simpelthen av konstant berøring kombinert med kjærtegn fører til over alle lykke og samt med mindre du er glad du ikke kan mektige være sunn, kan du?
Recent Posts
Keeping Subscription dates tidy during February and Leap years
Testing ActiveResource - Basic Tutorial
Active Resource to Sinatra DataMapper backend
ArmRest and the tale of the No Schema Scheme
Textmate Ruby 1.9.1 and rvm - the facts
Easiest Postgres Install Ever - Mac Edition
Two Questions to Help Decide Between RDBMS, MongoDB or CouchDB
Testing Rails 3 - Just the Facts
Acts_as_snook - Creating an admin panel
Top 10 Movies of this Decade... Rebuttal
Thinking_Sphinx - Easy Setup Tutorial