Created
July 28, 2011 21:42
-
-
Save courtsimas/1112651 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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