Commit 40426a5af4fb565097ca785abf8fa9c07696c174

Authored by Rodrigo Souto
1 parent 52500266

spammable: fail safe measures

   * Avoiding module inclusion on models that doesn't have a spam
     attribute.
   * Avoiding named_scope filters on models that doesn't have
     named_scope.
lib/spammable.rb
1 module Spammable 1 module Spammable
2 def self.included(recipient) 2 def self.included(recipient)
  3 + raise "This model should have a spam attribute!" if !recipient.new.respond_to?('spam=')
3 recipient.extend(ClassMethods) 4 recipient.extend(ClassMethods)
4 end 5 end
5 6
6 module ClassMethods 7 module ClassMethods
7 def self.extended (base) 8 def self.extended (base)
8 - base.class_eval do  
9 - named_scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false]  
10 - named_scope :spam, :conditions => ['spam = ?', true] 9 + if base.respond_to?(:named_scope)
  10 + base.class_eval do
  11 + named_scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false]
  12 + named_scope :spam, :conditions => ['spam = ?', true]
  13 + end
11 end 14 end
12 end 15 end
13 end 16 end
plugins/anti_spam/test/unit/anti_spam_plugin_test.rb
@@ -4,8 +4,6 @@ class AntiSpamPluginTest < ActiveSupport::TestCase @@ -4,8 +4,6 @@ class AntiSpamPluginTest < ActiveSupport::TestCase
4 4
5 class SpammableContent 5 class SpammableContent
6 attr_accessor :spam 6 attr_accessor :spam
7 - def self.named_scope(*args); end  
8 -  
9 include Spammable 7 include Spammable
10 8
11 def save!; end 9 def save!; end