Last active
August 29, 2015 14:06
-
-
Save shawndeprey/69a7b7f56faf2d436a2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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