Class: CensoredWordsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/censored_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
16
# File 'lib/validators/censored_words_validator.rb', line 4

def validate_each(record, attribute, value)
  words_regexps = WordWatcher.word_matcher_regexp_list(:censor)
  if WordWatcher.words_for_action_exists?(:censor).present? && words_regexps.present?
    censored_words = censor_words(value, words_regexps)
    return if censored_words.blank?

    record.errors.add(
      attribute,
      :contains_censored_words,
      censored_words: join_censored_words(censored_words),
    )
  end
end