diff --git a/README b/README index aba372c..8927e86 100644 --- a/README +++ b/README @@ -5,7 +5,7 @@ This plugin will allow you to filter profanity using basic replacement or a dict Disclaimer -======= +========== This plugin is provided as is - therefore, the creators and contributors of this plugin are not responsible for any damages that may result from it's usage. Use at your own risk; backup your data. @@ -13,14 +13,27 @@ This plugin is provided as is - therefore, the creators and contributors of this Example ======= -You can use it as an ActiveRecord include: +You can use it in your models: + + Notice -- there are two profanity filters, one is destructive. Beware the exclamation point (profanity_filter!). + + Non-Destructive (filters content when called, original text remains in the database) + + profanity_filter :foo, :bar + # banned words will be replaced with @#$% + profanity_filter :foo, :bar, :method => 'dictionary' + # banned words will have their vowels replaced + + Destructive (saves the filtered content to the database) + + profanity_filter! :foo, :bar + # banned words will be replaced with @#$% + profanity_filter! :foo, :bar, :method => 'dictionary' + # banned words will have their vowels replaced + - profanity_filter! :foo, :bar - # banned words will be replaced with @#$% - profanity_filter! :foo, :bar, :method => 'dictionary' - # banned words will have their vowels replaced +You can also use the filter directly: -You can also use this directly: ProfanityFilter::Base.clean(text) ProfanityFilter::Base.clean(text, 'dictionary') diff --git a/config/dictionary.yml b/config/dictionary.yml index 2a8df88..970eaa4 100644 --- a/config/dictionary.yml +++ b/config/dictionary.yml @@ -23,6 +23,8 @@ bitchin: b*tch*n bitching: b*tch*ng blowjob: bl*wj*b blowjobs: bl*wj*bs +bullshit: b*llsh*t +bulshit: b*llsh*t clit: cl*t cock: c*ck cocks: c*cks diff --git a/lib/profanity_filter.rb b/lib/profanity_filter.rb index b6fb280..d4360f6 100644 --- a/lib/profanity_filter.rb +++ b/lib/profanity_filter.rb @@ -9,6 +9,18 @@ def profanity_filter!(*attr_names) attr_names.each { |attr_name| setup_callbacks_for(attr_name, option) } end + def profanity_filter(*attr_names) + option = attr_names.pop[:method] rescue nil if attr_names.last.is_a?(Hash) + + 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 + alias_method attr_name.to_sym, "#{attr_name}_clean".to_sym + end + end + end + def setup_callbacks_for(attr_name, option) before_validation do |record| record[attr_name.to_sym] = ProfanityFilter::Base.clean(record[attr_name.to_sym], option) diff --git a/test/benign_filter_test.rb b/test/benign_filter_test.rb new file mode 100644 index 0000000..6f8d799 --- /dev/null +++ b/test/benign_filter_test.rb @@ -0,0 +1,51 @@ +require File.dirname(__FILE__) + '/test_harness' + +module BasicPostHelper + class Post < ActiveRecord::Base + profanity_filter :title, :body + end +end + +module DictionaryPostHelper + class Post < ActiveRecord::Base + profanity_filter :title, :body, :method => 'dictionary' + end +end + +class BasicProfanityFilterTest < Test::Unit::TestCase + include BasicPostHelper + + def profane_post(opts={}) + Post.new({:title => 'A Fucking Title', :body => "This is some shitty post by a fucking user"}.merge(opts)) + end + + def test_it_should_filter_specified_fields + p = profane_post + p.save + assert_equal 'A @#$% Title', p.title + assert_equal 'A @#$% Title', p.title_clean + assert_equal 'A Fucking Title', p.title_original + assert_equal 'This is some @#$% post by a @#$% user', p.body + assert_equal 'This is some @#$% post by a @#$% user', p.body_clean + assert_equal 'This is some shitty post by a fucking user', p.body_original + end +end + +class DictionaryProfanityFilterTest < Test::Unit::TestCase + include DictionaryPostHelper + + def profane_post(opts={}) + Post.new({:title => 'A Fucking Title', :body => "This is some shitty post by a fucking user"}.merge(opts)) + end + + def test_it_should_filter_specified_fields + p = profane_post + p.save + assert_equal 'A f*ck*ng Title', p.title + assert_equal 'A f*ck*ng Title', p.title_clean + assert_equal 'A Fucking Title', p.title_original + assert_equal 'This is some sh*tty post by a f*ck*ng user', p.body + assert_equal 'This is some sh*tty post by a f*ck*ng user', p.body_clean + assert_equal 'This is some shitty post by a fucking user', p.body_original + end +end \ No newline at end of file diff --git a/test/database.yml b/test/database.yml index ec8975e..61ce075 100644 --- a/test/database.yml +++ b/test/database.yml @@ -1,6 +1,6 @@ sqlite: :adapter: sqlite - :dbfile: profanity_filter.sqlite.db + :dbfile: vendor/plugins/fu-fu/test/profanity_filter.sqlite.db sqlite3: :adapter: sqlite3 - :dbfile: profanity_filter.sqlite3.db + :dbfile: vendor/plugins/fu-fu/test/profanity_filter.sqlite3.db diff --git a/test/filter_test.rb b/test/destructive_filter_test.rb similarity index 100% rename from test/filter_test.rb rename to test/destructive_filter_test.rb