2017-01-09 03:48:10 -05:00
|
|
|
class CensoredWordsValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2017-01-24 00:11:05 -05:00
|
|
|
if !SiteSetting.censored_words.blank? &&
|
2017-01-16 00:24:47 -05:00
|
|
|
!(censored_words = value.scan(censored_words_regexp)).empty?
|
2017-01-24 00:11:05 -05:00
|
|
|
|
2017-01-09 03:48:10 -05:00
|
|
|
record.errors.add(
|
|
|
|
attribute, :contains_censored_words,
|
2017-01-24 00:11:05 -05:00
|
|
|
censored_words: join_censored_words(censored_words)
|
2017-01-09 03:48:10 -05:00
|
|
|
)
|
2017-01-24 00:11:05 -05:00
|
|
|
elsif !SiteSetting.censored_pattern.blank? &&
|
|
|
|
!(censored_words = value.scan(/#{SiteSetting.censored_pattern}/i)).empty?
|
|
|
|
|
2017-01-09 03:48:10 -05:00
|
|
|
record.errors.add(
|
|
|
|
attribute, :matches_censored_pattern,
|
2017-01-24 00:11:05 -05:00
|
|
|
censored_words: join_censored_words(censored_words)
|
2017-01-09 03:48:10 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-01-24 00:11:05 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def join_censored_words(censored_words)
|
|
|
|
censored_words.map!(&:downcase)
|
|
|
|
censored_words.uniq!
|
2017-01-16 00:24:47 -05:00
|
|
|
censored_words.join(", ".freeze)
|
|
|
|
end
|
|
|
|
|
|
|
|
def censored_words_regexp
|
|
|
|
Regexp.new(
|
|
|
|
SiteSetting.censored_words.split('|'.freeze).map! { |w| Regexp.escape(w) }.join('|'.freeze),
|
|
|
|
true
|
|
|
|
)
|
2017-01-24 00:11:05 -05:00
|
|
|
end
|
2017-01-09 03:48:10 -05:00
|
|
|
end
|