From ce07da1d8bc12467ceede077ba60123be7a9c02d Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 24 Jan 2017 13:11:05 +0800 Subject: [PATCH] UX: Only display the words that fails censored words validations. --- config/locales/server.en.yml | 4 ++-- lib/validators/censored_words_validator.rb | 21 +++++++++++++++++---- spec/models/topic_spec.rb | 15 ++++++++------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 3366915212c..6c751415b7c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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 diff --git a/lib/validators/censored_words_validator.rb b/lib/validators/censored_words_validator.rb index 3d3c3685264..5ebd448dcbb 100644 --- a/lib/validators/censored_words_validator.rb +++ b/lib/validators/censored_words_validator.rb @@ -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 diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 809f7954299..bcf49caceb9 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -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