2017-01-09 02:48:10 -06:00
|
|
|
class CensoredWordsValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2017-01-23 23:11:05 -06:00
|
|
|
if !SiteSetting.censored_words.blank? &&
|
|
|
|
!(censored_words = value.scan(/#{SiteSetting.censored_words}/i)).empty?
|
|
|
|
|
2017-01-09 02:48:10 -06:00
|
|
|
record.errors.add(
|
|
|
|
attribute, :contains_censored_words,
|
2017-01-23 23:11:05 -06:00
|
|
|
censored_words: join_censored_words(censored_words)
|
2017-01-09 02:48:10 -06:00
|
|
|
)
|
2017-01-23 23:11:05 -06:00
|
|
|
elsif !SiteSetting.censored_pattern.blank? &&
|
|
|
|
!(censored_words = value.scan(/#{SiteSetting.censored_pattern}/i)).empty?
|
|
|
|
|
2017-01-09 02:48:10 -06:00
|
|
|
record.errors.add(
|
|
|
|
attribute, :matches_censored_pattern,
|
2017-01-23 23:11:05 -06:00
|
|
|
censored_words: join_censored_words(censored_words)
|
2017-01-09 02:48:10 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-01-23 23:11:05 -06:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def join_censored_words(censored_words)
|
|
|
|
censored_words.map!(&:downcase)
|
|
|
|
censored_words.uniq!
|
|
|
|
censored_words.join(", ")
|
|
|
|
end
|
2017-01-09 02:48:10 -06:00
|
|
|
end
|