2019-10-01 20:38:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class WatchedWordsValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
if matches = WordWatcher.new(value).should_block?.presence
|
|
|
|
if matches.size == 1
|
|
|
|
key = 'contains_blocked_word'
|
2021-09-24 04:55:15 -04:00
|
|
|
translation_args = { word: CGI.escapeHTML(matches[0]) }
|
2019-10-01 20:38:34 -04:00
|
|
|
else
|
|
|
|
key = 'contains_blocked_words'
|
2021-09-24 04:55:15 -04:00
|
|
|
translation_args = { words: CGI.escapeHTML(matches.join(', ')) }
|
2019-10-01 20:38:34 -04:00
|
|
|
end
|
|
|
|
record.errors.add(:base, I18n.t(key, translation_args))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|