Permalink
Browse files

added custom rule functionality

  • Loading branch information...
1 parent 52b9e8b commit 4b121dde11fbd7526d1696ee0eceb9d93020cc99 RSL committed Jun 22, 2009
Showing with 96 additions and 7 deletions.
  1. +26 −3 lib/lucky_sneaks/acts_as_snook.rb
  2. +34 −0 test/acts_as_snook_extension_test.rb
  3. +1 −0 test/entry.rb
  4. +21 −0 test/extended_comment.rb
  5. +11 −0 test/schema.rb
  6. +3 −4 test/test_helper.rb
@@ -22,7 +22,7 @@ module ClassMethods
#
# * <tt>:comment_belongs_to</tt> - Symbol or string specifying the association that the comment has a <tt>:belongs_to</tt> relationship with.
# * <tt>:ham_comments_count_field</tt> - Symbol or string specifying an alternate database field to use for the ham_comments_count attribute. Default: +ham_comments_count+
- def acts_as_snook(options = {})
+ def acts_as_snook(options = {}, &block)
cattr_accessor :spam_words
self.spam_words = %w{
-online 4u 4-u acne adipex advicer baccarrat blackjack bllogspot booker buy byob carisoprodol
@@ -61,6 +61,11 @@ def acts_as_snook(options = {})
after_destroy :decrement_ham_comments_count
end
+ cattr_accessor :custom_callbacks_for_snooking
+ if block_given?
+ self.custom_callbacks_for_snooking = block
+ end
+
attr_reader :snook_credits
attr_protected fields_for_snooking[:spam_status_field]
end
@@ -186,6 +191,12 @@ def calculate_snook_for_bbcode
deduct_snook_credits(1 * snook_body.scan(/\[(url|img)/i).size)
end
+ def calculate_snook_for_custom_rules
+ if custom_callbacks_for_snooking
+ instance_eval &custom_callbacks_for_snooking
+ end
+ end
+
def calculate_snook_score
@snook_credits = 0
calculate_snook_for_body_links
@@ -199,17 +210,27 @@ def calculate_snook_score
calculate_snook_for_author_link
calculate_snook_for_matching_previous_body
calculate_snook_for_consonant_runs
+ calculate_snook_for_bbcode
+ calculate_snook_for_custom_rules
status = if @snook_credits > 0
"ham"
elsif @snook_credits == 0
"moderate"
else
"spam"
end
+ status = @forced_snook_status || status
self.send("#{self.class.fields_for_snooking[:spam_status_field]}=", status)
+ @forced_snook_status = nil
+ # Dont save if snook_credits under -10
snook_credits >= -10
end
+ def force_snook_status(value)
+ raise ArgumentError, "force_snook_status called without block" unless block_given?
+ @forced_snook_status = value.to_s if yield
+ end
+
def snook_author
@snook_author ||= self.send(self.class.fields_for_snooking[:author_field]) || ""
end
@@ -231,11 +252,13 @@ def snook_spam_status
end
def add_snook_credits(addition)
- @snook_credits = @snook_credits + addition
+ return if block_given? && !yield
+ @snook_credits += addition
end
def deduct_snook_credits(deduction)
- @snook_credits = @snook_credits - deduction
+ return if block_given? && !yield
+ @snook_credits -= deduction
end
def previous_comment_count_for_snook_author(spam_value)
@@ -0,0 +1,34 @@
+require "test/test_helper"
+
+class ActsAsSnookExtensionTest < Test::Unit::TestCase
+ # This test suite is for nothing more than testing the extension
+ # of acts_as_snook with customized rules.
+
+ def test_extended_rules_override_ham_status
+ attributes = HAM_COMMENTS.first
+ @extended = ExtendedComment.new(attributes)
+ @extended.valid?
+ assert @extended.spam?
+ end
+
+ def test_extended_rules_override_spam_status
+ attributes = SPAM_COMMENTS[3]
+ @extended = ExtendedComment.new(attributes)
+ @extended.valid?
+ assert @extended.ham?
+ end
+
+ def test_forced_spam_status
+ attributes = HAM_COMMENTS[1].merge(:author => "spambot")
+ @extended = ExtendedComment.new(attributes)
+ @extended.valid?
+ assert @extended.spam?
+ end
+
+ def test_forced_ham_status
+ attributes = SPAM_COMMENTS[1].merge(:author => "hambot")
+ @extended = ExtendedComment.new(attributes)
+ @extended.valid?
+ assert @extended.ham?
+ end
+end
View
@@ -1,3 +1,4 @@
class Entry < ActiveRecord::Base
has_many :comments
+ has_many :extended_comments
end
View
@@ -0,0 +1,21 @@
+class ExtendedComment < ActiveRecord::Base
+ belongs_to :entry
+
+ acts_as_snook do
+ deduct_snook_credits(10) do
+ body =~ /Simon/
+ end
+
+ add_snook_credits(10) do
+ author == "Charles Ford"
+ end
+
+ force_snook_status(:spam) do
+ author == "spambot"
+ end
+
+ force_snook_status(:ham) do
+ author == "hambot"
+ end
+ end
+end
View
@@ -11,4 +11,15 @@
t.string :title
t.integer :ham_comments_count, :default => 0
end
+
+ create_table :extended_comments, :force => true do |t|
+ t.integer :entry_id
+ t.string :author, :email, :url, :spam_status
+ t.text :body
+ end
+
+ create_table :extended_entries, :force => true do |t|
+ t.string :title
+ t.integer :ham_comments_count, :default => 0
+ end
end
View
@@ -16,10 +16,9 @@
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => "acts_as_url.sqlite3")
-require File.join(File.dirname(__FILE__), '../init')
-require File.join(File.dirname(__FILE__), 'schema')
-require File.join(File.dirname(__FILE__), 'comment')
-require File.join(File.dirname(__FILE__), 'entry')
+%w{../init schema comment entry extended_comment}.each do |foo|
+ require File.join(File.dirname(__FILE__), foo)
+end
Entry.create!(:title => "The Tale of Flight 815")

0 comments on commit 4b121dd

Please sign in to comment.