Skip to content

Instantly share code, notes, and snippets.

@courtsimas
Created July 28, 2011 21:42
Show Gist options
  • Save courtsimas/1112651 to your computer and use it in GitHub Desktop.
Save courtsimas/1112651 to your computer and use it in GitHub Desktop.
class DecencyValidator < ActiveModel::Validator
def validate(record)
I18n.locale = record.locale
record.errors[:base] = I18n.t(:bad_words_warning) if filter_body(record.body, I18n.t(:bad_words).split( /, */ ) ).present?
end
private
def filter_body( check_str = "", prohibited_words = [] )
return nil if check_str.blank? || prohibited_words.blank?
return check_str.downcase.gsub!(/[^a-z]/, " ").split(" ").uniq.map { |w| w if prohibited_words.include?( w ) }.compact!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment