diff --git a/README.md b/README.md index 71f437a..30ae743 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,12 @@ Moderate.configure do |config| # Exclude words from the default list (false positives) config.excluded_words = ["good"] + + # 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 1d3b8fc..7b6cdd4 100644 --- a/lib/moderate.rb +++ b/lib/moderate.rb @@ -23,12 +23,18 @@ def configure end class Configuration - attr_accessor :error_message, :additional_words, :excluded_words + 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 = [] + @blacklist_regexp_pattern = nil + @whitelist_regexp_pattern = nil end end end diff --git a/lib/moderate/text.rb b/lib/moderate/text.rb index c4384e4..e8f5365 100644 --- a/lib/moderate/text.rb +++ b/lib/moderate/text.rb @@ -6,6 +6,9 @@ class << self def bad_words?(text) return false if text.blank? + 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) } end @@ -19,8 +22,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 +36,22 @@ def reset_word_list! def logger @logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout) end + + def configuration + @configuration ||= Moderate.configuration + end + + 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 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