2019-10-01 19:38:34 -05: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 03:55:15 -05:00
|
|
|
translation_args = { word: CGI.escapeHTML(matches[0]) }
|
2019-10-01 19:38:34 -05:00
|
|
|
else
|
|
|
|
key = 'contains_blocked_words'
|
2021-09-24 03:55:15 -05:00
|
|
|
translation_args = { words: CGI.escapeHTML(matches.join(', ')) }
|
2019-10-01 19:38:34 -05:00
|
|
|
end
|
|
|
|
record.errors.add(:base, I18n.t(key, translation_args))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|