mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
UX: Only display the words that fails censored words validations.
This commit is contained in:
parent
ff508ed75f
commit
ce07da1d8b
3 changed files with 27 additions and 13 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue