mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
UX: Only display the words that fails censored words validations.
This commit is contained in:
@ -1,15 +1,28 @@
|
||||
class CensoredWordsValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
if !SiteSetting.censored_words.blank? && value =~ /#{SiteSetting.censored_words}/i
|
||||
if !SiteSetting.censored_words.blank? &&
|
||||
!(censored_words = value.scan(/#{SiteSetting.censored_words}/i)).empty?
|
||||
|
||||
record.errors.add(
|
||||
attribute, :contains_censored_words,
|
||||
censored_words: SiteSetting.censored_words
|
||||
censored_words: join_censored_words(censored_words)
|
||||
)
|
||||
elsif !SiteSetting.censored_pattern.blank? && value =~ /#{SiteSetting.censored_pattern}/i
|
||||
elsif !SiteSetting.censored_pattern.blank? &&
|
||||
!(censored_words = value.scan(/#{SiteSetting.censored_pattern}/i)).empty?
|
||||
|
||||
byebug
|
||||
record.errors.add(
|
||||
attribute, :matches_censored_pattern,
|
||||
censored_pattern: SiteSetting.censored_pattern
|
||||
censored_words: join_censored_words(censored_words)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def join_censored_words(censored_words)
|
||||
censored_words.map!(&:downcase)
|
||||
censored_words.uniq!
|
||||
censored_words.join(", ")
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user