Class: WatchedWordsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/watched_words_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/validators/watched_words_validator.rb', line 4

def validate_each(record, attribute, value)
  if matches = WordWatcher.new(value).should_block?.presence
    if matches.size == 1
      key = "contains_blocked_word"
      translation_args = { word: CGI.escapeHTML(matches[0]) }
    else
      key = "contains_blocked_words"
      translation_args = { words: CGI.escapeHTML(matches.join(", ")) }
    end
    record.errors.add(:base, I18n.t(key, translation_args))
  end
end