Skip to content

Commit

Permalink
defensio!
Browse files Browse the repository at this point in the history
  • Loading branch information
redinger committed Jun 8, 2009
1 parent 31ef043 commit 8282ec8
Show file tree
Hide file tree
Showing 24 changed files with 1,044 additions and 6 deletions.
7 changes: 6 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def new
def create
@comment = Comment.new((session[:pending_comment] || params[:comment] || {}).reject {|key, value| !Comment.protected_attribute?(key) })
@comment.post = @post

@comment.env = request.env
session[:pending_comment] = nil

unless @comment.requires_openid_authentication?
Expand All @@ -52,6 +52,11 @@ def create
end

if session[:pending_comment].nil? && @comment.save
if @comment.spam
flash[:notice] = 'Your comment has been marked for review'
else
flash[:notice] = 'Comment created'
end
redirect_to post_path(@post)
else
render :template => 'posts/show'
Expand Down
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Comment < ActiveRecord::Base
attr_accessor :openid_error
attr_accessor :openid_valid

acts_as_defensio_comment :fields => {:content => :body, :article => :post}

belongs_to :post

before_save :apply_filter
Expand Down
13 changes: 12 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ class Post < ActiveRecord::Base

acts_as_taggable

acts_as_defensio_article :fields => {:content => :body_html, :permalink => :slug}

has_many :comments, :dependent => :destroy
has_many :approved_comments, :class_name => 'Comment'
has_many :approved_comments, :class_name => 'Comment', :conditions => {:spam => false}

before_validation :generate_slug
before_validation :set_dates
Expand All @@ -19,6 +21,15 @@ def validate_published_at_natural
end

attr_accessor :minor_edit

def author
"Christopher Redinger"
end

def author_email
"info@agiledisciple.com"
end

def minor_edit
@minor_edit ||= "1"
end
Expand Down
1 change: 1 addition & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<% content_for(:page_title) do -%>
<%=h post_title(@post) -%>
<% end -%>
<% if flash[:notice] %><p><%= flash[:notice] %></p><% end %>
<%= render :partial => 'posts/post', :locals => {:post => @post} %>
<ol class="commentlist">
<% @post.approved_comments.each do |comment| -%>
Expand Down
4 changes: 0 additions & 4 deletions config/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
database.yml
defensio.yml
<<<<<<< HEAD:config/.gitignore
newrelic.yml
=======
newrelic.yml
>>>>>>> 5e29f7f21b667ccc8f30e0e679fbe8f4033f38d5:config/.gitignore
16 changes: 16 additions & 0 deletions db/migrate/20090608144306_add_defensio_columns_to_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AddDefensioColumnsToComments < ActiveRecord::Migration
def self.up
add_column :comments, :spam, :boolean, :default => false
add_column :comments, :spaminess, :float
add_column :comments, :signature, :string
# Uncomment this if you wanna customize when an article is announced to Defensio server
# add_column :article, :announced, :boolean, :default => false
end

def self.down
remove_column :comments, :spam
remove_column :comments, :spaminess
remove_column :comments, :signature
# add_column :article, :announced
end
end
37 changes: 37 additions & 0 deletions vendor/plugins/defensio/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
== Defensio

* Complete doc of Defensio API: Defensio::Client
* Plugin doc: Defensio::ActsAs::ClassMethods

=== Installation

1. Install the plugin
script/plugin install http://code.macournoyer.com/svn/plugins/defensio/
2. Get an API key at http://defensio.com and dump it in config/defensio.yml
3. Add new column to your spammable model
script/generate defensio_migration comment

You're ready to go!

=== Usage

In your "article" class, add:

acts_as_defensio_article

In your "comment" class, add:

acts_as_defensio_comment :fields => { :content => :comment }

The :fields option allows you to configure fields that are sent
to Defensio to classify a comment.

Look at Defensio::Client for a list of all those fields.

In the controller, before creating a comment set the request environment variables:
@comment.env = request.env

Then when the comment is created the +spam+, +spaminess+ and +signature+
columns will magically be set for your own pleasure.

Check out other examples in http://code.macournoyer.com/svn/plugins/defensio/example
29 changes: 29 additions & 0 deletions vendor/plugins/defensio/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/contrib/rubyforgepublisher'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the defensio plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the defensio plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Defensio'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

task :pub_doc => :rdoc do
Rake::SshDirPublisher.new("macournoyer@macournoyer.com",
"code.macournoyer.com/defensio",
"rdoc").upload
end
6 changes: 6 additions & 0 deletions vendor/plugins/defensio/example/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Article < ActiveRecord::Base
# Columns: author, author_email, title, content, permalink
has_many :comments

acts_as_defensio_article
end
16 changes: 16 additions & 0 deletions vendor/plugins/defensio/example/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Comment < ActiveRecord::Base
# Columns: author, content, title, author_email, author_url, permalink, article_id
attr_accessor :current_user

belongs_to :article

acts_as_defensio_comment

def user_logged_in
current_user.logged_in?
end

def trusted_user
current_user.admin?
end
end
37 changes: 37 additions & 0 deletions vendor/plugins/defensio/example/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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
@comment.current_user = self.current_user

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
13 changes: 13 additions & 0 deletions vendor/plugins/defensio/example/defensio.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
# Skip validation of the API key
validate_key: false

production:
api_key: 6789xyz
owner_url: http://code.macournoyer.com/svn/plugins/defensio
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class DefensioMigrationGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
m.migration_template 'migration.rb', 'db/migrate', :assigns => {
:table_name => @name.tableize, :class_name => @name.classify.pluralize
}, :migration_file_name => "add_defensio_columns_to_#{@name.tableize}"
end
end

protected
# Override with your own usage banner.
def banner
"Usage: #{$0} defensio_migration model"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AddDefensioColumnsTo<%= class_name %> < ActiveRecord::Migration
def self.up
add_column :<%= table_name %>, :spam, :boolean, :default => false
add_column :<%= table_name %>, :spaminess, :float
add_column :<%= table_name %>, :signature, :string
# Uncomment this if you wanna customize when an article is announced to Defensio server
# add_column :article, :announced, :boolean, :default => false
end

def self.down
remove_column :<%= table_name %>, :spam
remove_column :<%= table_name %>, :spaminess
remove_column :<%= table_name %>, :signature
# add_column :article, :announced
end
end
1 change: 1 addition & 0 deletions vendor/plugins/defensio/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'defensio'
3 changes: 3 additions & 0 deletions vendor/plugins/defensio/install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'fileutils'
FileUtils.cp "#{File.dirname(__FILE__)}/example/defensio.yml", "#{RAILS_ROOT}/config/defensio.yml"
puts File.read("#{File.dirname(__FILE__)}/README")
2 changes: 2 additions & 0 deletions vendor/plugins/defensio/lib/defensio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'defensio/client'
require 'defensio/acts_as'
Loading

0 comments on commit 8282ec8

Please sign in to comment.