Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/despamilator_rails.rb

Class Method Summary collapse

Class Method Details

.add_despamilator_validation(settings) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/despamilator_rails.rb', line 60

def add_despamilator_validation settings

  send(validation_method(:save), settings) do |record|

    record.despamilator_checked_attributes.each do |attribute|
      text  = record.send(attribute)
      dspam = Despamilator.new(text)

      record.despamilator_callback(attribute, text, dspam) if dspam.score >= record.despamilator_threshold
    end

  end

end

.assign_despamilator_attributes(settings) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/despamilator_rails.rb', line 75

def assign_despamilator_attributes(settings)
  clean_attributes = settings[:attributes].reject do |attribute|
    attribute.blank?
  end

  raise('At least one attribute must be defined') if clean_attributes.empty?

  define_method(:despamilator_checked_attributes, lambda { clean_attributes })
end

.assign_despamilator_callback(block) ⇒ Object



89
90
91
# File 'lib/despamilator_rails.rb', line 89

def assign_despamilator_callback(block)
  define_method(:despamilator_callback, block || default_despamilator_detection_response)
end

.assign_despamilator_threshold(settings) ⇒ Object



85
86
87
# File 'lib/despamilator_rails.rb', line 85

def assign_despamilator_threshold(settings)
  define_method(:despamilator_threshold, lambda { settings[:threshold] || 1 })
end

.default_despamilator_detection_responseObject



93
94
95
96
97
# File 'lib/despamilator_rails.rb', line 93

def default_despamilator_detection_response
  lambda { |attribute, value, dspam|
    errors.add(attribute, "looks like spam")
  }
end

.validate_with_despamilator(settings, &block) ⇒ Object

Somewhere (such as your environment.rb…)

require 'despamilator_rails'

In your model (basic example):

class YourModel < ActiveRecord::Base
  validate_with_despamilator :attributes => [:some_field]
end

When “some_field” is assigned a spammy value, it will add to the errors. For example…

YourModel.new(:some_field => spammy_value).save! #=> ActiveRecord::RecordInvalid exception!

Or…

your_instance = YourModel.new(:some_field => spammy_value)
your_instance.save
your_instance.errors.full_messages.should #=> ["Some field looks like spam"]

If you want to configure the threshold (which defaults to 1) or add your own callback, you can do the following:

class YourModel < ActiveRecord::Base
  validate_with_despamilator :attributes => [:some_field], :threshold => 1 do |field, value, despamilator|
    raise "spam! field: #{field}, value: #{value}, score: #{despamailtor.score}"
  end
end

This example will…

your_instance = YourModel.new(:some_field => "spammy string")
your_instance.save! #=> Exception "spam! field: some_field, value: spammy string, score: 123"

The callback will receive the field name, the value and the instance of the Despamilator class.



47
48
49
50
51
52
53
54
# File 'lib/despamilator_rails.rb', line 47

def self.validate_with_despamilator settings, &block
  raise "This version only supports ActiveRecord 2. Please download the main version for support of newer versions of ActiveRecord." if VERSION::MAJOR > 2
  assign_despamilator_attributes settings
  assign_despamilator_threshold settings
  assign_despamilator_callback block

  add_despamilator_validation settings
end