41 lines
890 B
Ruby
41 lines
890 B
Ruby
module JamRuby
|
|
|
|
class Profanity
|
|
@@dictionary_file = File.join('config/profanity.yml')
|
|
@@dictionary = nil
|
|
|
|
def self.dictionary
|
|
if File.exists? @@dictionary_file
|
|
@@dictionary ||= YAML.load_file(@@dictionary_file)
|
|
else
|
|
@@dictionary = []
|
|
end
|
|
@@dictionary
|
|
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 Profanity.is_profane?(value)
|
|
end
|
|
end
|
|
|