From b0bdc73d9eaa24c52b31b991fae4199b96b8d1d8 Mon Sep 17 00:00:00 2001 From: Andrew Hecht Date: Wed, 7 Mar 2012 17:13:58 -0800 Subject: [PATCH 1/5] Allow the addition of custom profanity words using the append_dictionary method. --- lib/profanity_filter.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/profanity_filter.rb b/lib/profanity_filter.rb index 302c03b..7d8edb0 100644 --- a/lib/profanity_filter.rb +++ b/lib/profanity_filter.rb @@ -42,6 +42,11 @@ def dictionary @@dictionary ||= YAML.load_file(@@dictionary_file) end + def append_dictionary( file ) + dictionary #Insure the dictionary is loaded + @@dictionary = @@dictionary.merge(YAML.load_file( file ) ) + end + def banned?(word = '') dictionary.include?(word.downcase) if word end From f59b3a4992f45ca7437fe3d60644b76e6bf67a10 Mon Sep 17 00:00:00 2001 From: Andrew Hecht Date: Wed, 7 Mar 2012 17:23:44 -0800 Subject: [PATCH 2/5] Added the ability to remove words from the base dictionary using the remove_from_dictionary method. --- lib/profanity_filter.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/profanity_filter.rb b/lib/profanity_filter.rb index 7d8edb0..e326bc4 100644 --- a/lib/profanity_filter.rb +++ b/lib/profanity_filter.rb @@ -43,8 +43,16 @@ def dictionary end def append_dictionary( file ) - dictionary #Insure the dictionary is loaded - @@dictionary = @@dictionary.merge(YAML.load_file( file ) ) + @@dictionary = dictionary.merge(YAML.load_file( file ) ) + end + + def remove_from_dictionary( file ) + excluded_words = YAML.load_file( file ) + if excluded_words + dictionary.keep_if do |dictionary_word| + !( excluded_words.include?(dictionary_word) ) + end + end end def banned?(word = '') From 0f308cd479b2e126ac6e318a536ba6b30c429006 Mon Sep 17 00:00:00 2001 From: Andrew Hecht Date: Thu, 8 Mar 2012 16:00:06 -0800 Subject: [PATCH 3/5] Unbind the profanity filter methods before preforming a save. ActiveRecord 3.2.x seems to look for methods named after attributes, and use those values during the save. The unbind_profanity and bind_profanity methods are instance methods so they will not effect other instances of the class. ActiveRecord seems to gather the attributes prior to calling before_save so I had to override the save method, and call my before/after methods by hand. --- lib/profanity_filter.rb | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/profanity_filter.rb b/lib/profanity_filter.rb index e326bc4..8a4c0fd 100644 --- a/lib/profanity_filter.rb +++ b/lib/profanity_filter.rb @@ -19,8 +19,40 @@ def profanity_filter(*attr_names) attr_names.each do |attr_name| instance_eval do define_method "#{attr_name}_clean" do; ProfanityFilter::Base.clean(self[attr_name.to_sym], option); end - define_method "#{attr_name}_original"do; self[attr_name]; end + define_method "#{attr_name}_original" do; self[attr_name]; end + define_method "profanity_filtered_attrs" do; attr_names; end alias_method attr_name.to_sym, "#{attr_name}_clean".to_sym + + define_method "unbind_profanity" do + profanity_filtered_attrs.each do |attr_name| + eval %( + class << self + undef_method :#{attr_name} + def #{attr_name} + @attributes[%q(#{attr_name})] + end + end + ) + end + end + define_method "bind_profanity" do + profanity_filtered_attrs.each do |attr_name| + eval %( + class << self + undef_method :#{attr_name} + alias_method :#{attr_name}, :#{attr_name}_clean + end + ) + end + end + + #Before and after save does not get triggered until after the attributes have been accessed. + #SO... lets override the save method. + define_method "save" do |*args| + unbind_profanity + super(*args) + bind_profanity + end end end end From ade77d236b1376d31ed8e10c6d73ed30cd9e22b9 Mon Sep 17 00:00:00 2001 From: Andrew Hecht Date: Sat, 10 Mar 2012 21:46:32 -0800 Subject: [PATCH 4/5] Store the result of the super call to active record upon a save, and return that result back from our overriden method. --- lib/profanity_filter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/profanity_filter.rb b/lib/profanity_filter.rb index 8a4c0fd..dc56f13 100644 --- a/lib/profanity_filter.rb +++ b/lib/profanity_filter.rb @@ -50,8 +50,9 @@ class << self #SO... lets override the save method. define_method "save" do |*args| unbind_profanity - super(*args) + result = super(*args) bind_profanity + return result end end end From 87bc5ffca774edb54a99e792f743bce7813df5ca Mon Sep 17 00:00:00 2001 From: Velkitor Date: Thu, 8 Jan 2015 14:59:05 -0800 Subject: [PATCH 5/5] Support Rails 4.2 FromUser/FromDatabase attributes --- lib/profanity_filter.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/profanity_filter.rb b/lib/profanity_filter.rb index dc56f13..7d96fb9 100644 --- a/lib/profanity_filter.rb +++ b/lib/profanity_filter.rb @@ -29,7 +29,12 @@ def profanity_filter(*attr_names) class << self undef_method :#{attr_name} def #{attr_name} - @attributes[%q(#{attr_name})] + v = @attributes[%q(#{attr_name})] + if v.kind_of?(String) + v + elsif v.respond_to?(:value) + v.value + end end end )