From 7eac723a669a4c2ccb535665a69c7b6e2c06ae3a Mon Sep 17 00:00:00 2001 From: Alexander Marychev Date: Sat, 22 Feb 2025 13:18:34 +0300 Subject: [PATCH 1/2] Add regexp_pattern checker to moderate --- README.md | 3 +++ lib/moderate.rb | 3 ++- lib/moderate/text.rb | 14 ++++++++++++-- lib/moderate/text_validator.rb | 1 - 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 71f437a..2684272 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,9 @@ Moderate.configure do |config| # Exclude words from the default list (false positives) config.excluded_words = ["good"] + + # Matchind text_field with regexp pattern + config.regexp_pattern = /.*bad_pattern.*/ end ``` diff --git a/lib/moderate.rb b/lib/moderate.rb index 1d3b8fc..caa9827 100644 --- a/lib/moderate.rb +++ b/lib/moderate.rb @@ -23,12 +23,13 @@ def configure end class Configuration - attr_accessor :error_message, :additional_words, :excluded_words + attr_accessor :error_message, :additional_words, :excluded_words, :regexp_pattern def initialize @error_message = "contains moderatable content (bad words)" @additional_words = [] @excluded_words = [] + @regexp_pattern = nil end end end diff --git a/lib/moderate/text.rb b/lib/moderate/text.rb index c4384e4..3e4b43c 100644 --- a/lib/moderate/text.rb +++ b/lib/moderate/text.rb @@ -6,6 +6,8 @@ class << self def bad_words?(text) return false if text.blank? + return true if match_regexp_pattern?(text) + @words_set ||= Set.new(compute_word_list) text.downcase.split(/\W+/).any? { |word| @words_set.include?(word) } end @@ -19,8 +21,8 @@ def compute_word_list words end - result = (@default_words + Moderate.configuration.additional_words - - Moderate.configuration.excluded_words).to_set + result = (@default_words + configuration.additional_words - + configuration.excluded_words).to_set logger.debug("[moderate gem] Final word list size: #{result.size}") result end @@ -33,6 +35,14 @@ def reset_word_list! def logger @logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout) end + + def configuration + @configuration ||= Moderate.configuration + end + + def match_regexp_pattern?(text) + configuration.regexp_pattern.is_a?(Regexp) && configuration.regexp_pattern.match?(text) + end end end end diff --git a/lib/moderate/text_validator.rb b/lib/moderate/text_validator.rb index 084a16b..e1e8068 100644 --- a/lib/moderate/text_validator.rb +++ b/lib/moderate/text_validator.rb @@ -22,6 +22,5 @@ def validate_each(record, attribute, value) end end end - end end From b67b3fb09591ed323e5a449e19ca31d30633eeb9 Mon Sep 17 00:00:00 2001 From: Alexander Marychev Date: Mon, 24 Feb 2025 09:51:42 +0300 Subject: [PATCH 2/2] Add whitelist_regexp_pattern checker to moderate --- README.md | 7 +++++-- lib/moderate.rb | 9 +++++++-- lib/moderate/text.rb | 15 ++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2684272..30ae743 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,11 @@ Moderate.configure do |config| # Exclude words from the default list (false positives) config.excluded_words = ["good"] - # Matchind text_field with regexp pattern - config.regexp_pattern = /.*bad_pattern.*/ + # Matchind text_field with blacklist regexp pattern + config.blacklist_regexp_pattern = /.*bad_pattern.*/ + + # Matchind text_field with whitelist regexp pattern + config.whitelist_regexp_pattern = /.*good_pattern.*/ end ``` diff --git a/lib/moderate.rb b/lib/moderate.rb index caa9827..7b6cdd4 100644 --- a/lib/moderate.rb +++ b/lib/moderate.rb @@ -23,13 +23,18 @@ def configure end class Configuration - attr_accessor :error_message, :additional_words, :excluded_words, :regexp_pattern + ACCESSORS = %i[ + error_message additional_words excluded_words blacklist_regexp_pattern whitelist_regexp_pattern + ].freeze + + attr_accessor(*ACCESSORS) def initialize @error_message = "contains moderatable content (bad words)" @additional_words = [] @excluded_words = [] - @regexp_pattern = nil + @blacklist_regexp_pattern = nil + @whitelist_regexp_pattern = nil end end end diff --git a/lib/moderate/text.rb b/lib/moderate/text.rb index 3e4b43c..e8f5365 100644 --- a/lib/moderate/text.rb +++ b/lib/moderate/text.rb @@ -6,7 +6,8 @@ class << self def bad_words?(text) return false if text.blank? - return true if match_regexp_pattern?(text) + return true if match_blacklist_regexp_pattern?(text) + return true if not_match_whitelist_regexp_pattern?(text) @words_set ||= Set.new(compute_word_list) text.downcase.split(/\W+/).any? { |word| @words_set.include?(word) } @@ -40,8 +41,16 @@ def configuration @configuration ||= Moderate.configuration end - def match_regexp_pattern?(text) - configuration.regexp_pattern.is_a?(Regexp) && configuration.regexp_pattern.match?(text) + def match_blacklist_regexp_pattern?(text) + pattern = configuration.blacklist_regexp_pattern + + pattern.is_a?(Regexp) && pattern.match?(text) + end + + def not_match_whitelist_regexp_pattern?(text) + pattern = configuration.whitelist_regexp_pattern + + pattern.is_a?(Regexp) && !pattern.match?(text) end end end