class PottyMouthValidator
this is not namespaced so that we can do
validates :body, potty_mouth: true
Public Class Methods
add_word_list(type, path)
click to toggle source
# File lib/validates_potty_mouth/potty_mouth_validator.rb, line 35 def add_word_list(type, path) banned_word_lists[type.to_sym] = File.read(path).split("\n").map{|word| word.chomp.downcase}.to_set end
banned_word_lists()
click to toggle source
# File lib/validates_potty_mouth/potty_mouth_validator.rb, line 31 def banned_word_lists @lists ||= Hash.new { Set.new } end
Public Instance Methods
banned?(value)
click to toggle source
# File lib/validates_potty_mouth/potty_mouth_validator.rb, line 14 def banned?(value) text = value.gsub(/[\W\d]/, ' ') # get rid of non-letters words = text.split.to_set words.any?{|word| banned_word?(word)} end
banned_word?(word)
click to toggle source
# File lib/validates_potty_mouth/potty_mouth_validator.rb, line 20 def banned_word?(word) down_word = word.downcase banned_word_list.include?(down_word) || banned_word_list.include?(down_word.stem) end
banned_word_list()
click to toggle source
# File lib/validates_potty_mouth/potty_mouth_validator.rb, line 26 def banned_word_list self.class.banned_word_lists[options.fetch(:list, :default)] end
validate_each(record, attribute, value)
click to toggle source
# File lib/validates_potty_mouth/potty_mouth_validator.rb, line 6 def validate_each(record, attribute, value) return true if value.blank? if banned?(value) record.errors[attribute] << options.fetch(:message, "contains objectionable content") end end