FIX: Abort CensoredWordsValidator early if censored_words_regexp nil (#8375)

* Abort CensoredWordsValidator early if censored_words_regexp nil. Sometimes censored_words_regex can end up nil, erroring the validator. This handles the nil condition and also adds a spec for the validator
This commit is contained in:
Martin Brennan
2019-11-20 20:05:06 +10:00
committed by GitHub
parent 7543db086a
commit d45bd7f131
2 changed files with 56 additions and 2 deletions

View File

@@ -2,9 +2,13 @@
class CensoredWordsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if WordWatcher.words_for_action(:censor).present? && (censored_words = censor_words(value, censored_words_regexp)).present?
words_regexp = censored_words_regexp
if WordWatcher.words_for_action(:censor).present? && !words_regexp.nil?
censored_words = censor_words(value, words_regexp)
return if censored_words.blank?
record.errors.add(
attribute, :contains_censored_words,
attribute,
:contains_censored_words,
censored_words: join_censored_words(censored_words)
)
end