module JamRuby class Profanity @@dictionary_file = File.join(File.dirname(__FILE__), '../../..', 'config/profanity.yml') @@dictionary = nil def self.dictionary @@dictionary ||= load_dictionary end def self.load_dictionary YAML.load_file(@@dictionary_file) end def self.check_word(word) dictionary.include?(word.downcase) end def self.is_profane?(text) return false if text.nil? text.split(/\W+/).each do |word| return true if check_word(word) end return false end end end # This needs to be outside the module to work. class NoProfanityValidator < ActiveModel::EachValidator # implement the method called during validation def validate_each(record, attribute, value) record.errors[attribute] << 'cannot contain profanity' if JamRuby::Profanity.is_profane?(value) end end