Sha256: 854780f4742e65d089e572f029afc643b7384541274319992ddc50298b106cc7
Contents?: true
Size: 1.1 KB
Versions: 1
Compression:
Stored size: 1.1 KB
Contents
require 'stemmer' # this is not namespaced so that we can do # validates :body, potty_mouth: true class PottyMouthValidator < ActiveModel::EachValidator 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 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 def banned_word?(word) down_word = word.downcase banned_word_list.include?(down_word) || banned_word_list.include?(down_word.stem) end def banned_word_list self.class.banned_word_lists[options.fetch(:list, :default)] end class << self def banned_word_lists @lists ||= Hash.new { Set.new } end 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 end end PottyMouthValidator.add_word_list(:default, File.expand_path("../default_word_list.txt", __FILE__))
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
validates_potty_mouth-0.1.0 | lib/validates_potty_mouth/potty_mouth_validator.rb |