Skip to content

Instantly share code, notes, and snippets.

@shawndeprey
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawndeprey/69a7b7f56faf2d436a2f to your computer and use it in GitHub Desktop.
Save shawndeprey/69a7b7f56faf2d436a2f to your computer and use it in GitHub Desktop.
class YourModel < ActiveRecord::Base
include Rakismet::Model
# Create Validator which will hit the Akismet API
class SpamValidator < ActiveModel::Validator
def validate(record)
return true unless record.spam? # record.spam? will hit the Akismet API
record.errors[:base] << "Spam message detected."
end
end
attr_accessible :id, :user_ip, :user_agent, :referrer, :full_name, :email, :message, :created_at, :updated_at
# Re-map attributes for Rakismet
rakismet_attrs author: :full_name, author_email: :email, content: :message
# Run the validator for our message content
validates :message, presence: true, spam: true
# Set Client attributes from the request which are required for Akismet's API
def request=(request)
self.user_ip = request.remote_ip
self.user_agent = request.env['HTTP_USER_AGENT']
self.referrer = request.env['HTTP_REFERER']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment