mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 04:13:44 +08:00
f331b5eab2
Currently, the topic is only validated for censored words and should be validated for blocked words as well. Blocked word validation is now used by both Post and Topic. To avoid code duplication, I extracted blocked words validation code into separate Validator, and use it in both places. The only downside is that even if the topic contains blocked words validation message is saying "Your post contains a word that's not allowed: tomato" but I think this is descriptive enough.
17 lines
507 B
Ruby
17 lines
507 B
Ruby
# 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'
|
|
translation_args = { word: matches[0] }
|
|
else
|
|
key = 'contains_blocked_words'
|
|
translation_args = { words: matches.join(', ') }
|
|
end
|
|
record.errors.add(:base, I18n.t(key, translation_args))
|
|
end
|
|
end
|
|
end
|