mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 04:52:45 +08:00
UX: Only display the words that fails censored words validations.
This commit is contained in:
parent
ff508ed75f
commit
ce07da1d8b
|
@ -96,8 +96,8 @@ en:
|
|||
inclusion: is not included in the list
|
||||
invalid: is invalid
|
||||
is_invalid: "seems unclear, is it a complete sentence?"
|
||||
contains_censored_words: "includes one or more of the censored words: %{censored_words}"
|
||||
matches_censored_pattern: "matches the following censored Regex: %{censored_pattern}"
|
||||
contains_censored_words: "contains the following censored words: %{censored_words}"
|
||||
matches_censored_pattern: "contains the following words that matches the site's censored regexp: %{censored_words}"
|
||||
less_than: must be less than %{count}
|
||||
less_than_or_equal_to: must be less than or equal to %{count}
|
||||
not_a_number: is not a number
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -14,29 +14,30 @@ describe Topic do
|
|||
it { is_expected.to validate_presence_of :title }
|
||||
|
||||
describe 'censored words' do
|
||||
site_setting(:censored_words, 'pineapple|pen')
|
||||
site_setting(:censored_pattern, 'orange.*')
|
||||
|
||||
describe 'when title contains censored words' do
|
||||
site_setting(:censored_words, 'pineapple|pen')
|
||||
|
||||
it 'should not be valid' do
|
||||
topic.title = 'I have a Pineapple'
|
||||
topic.title = 'pen PinEapple apple pen '
|
||||
|
||||
expect(topic).to_not be_valid
|
||||
|
||||
expect(topic.errors.full_messages.first).to include(I18n.t(
|
||||
'errors.messages.contains_censored_words', censored_words: SiteSetting.censored_words
|
||||
'errors.messages.contains_censored_words', censored_words: 'pen, pineapple'
|
||||
))
|
||||
end
|
||||
end
|
||||
|
||||
describe 'when title matches censored pattern' do
|
||||
site_setting(:censored_pattern, 'orange.*')
|
||||
|
||||
it 'should not be valid' do
|
||||
topic.title = 'I have orangEjuice'
|
||||
topic.title = 'I have orangEjuice orange monkey orange stuff'
|
||||
|
||||
expect(topic).to_not be_valid
|
||||
|
||||
expect(topic.errors.full_messages.first).to include(I18n.t(
|
||||
'errors.messages.matches_censored_pattern', censored_pattern: SiteSetting.censored_pattern
|
||||
'errors.messages.matches_censored_pattern', censored_words: 'orange monkey orange stuff'
|
||||
))
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user