Skip to content

Commit

Permalink
Fu-fu now has a non-destructive method, and there was much rejoicing. [
Browse files Browse the repository at this point in the history
…#4 state:resolved]
  • Loading branch information
adambair committed Jun 4, 2008
1 parent f791aef commit a8cd4bd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
27 changes: 20 additions & 7 deletions README
Expand Up @@ -5,22 +5,35 @@ 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.


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')

Expand Down
2 changes: 2 additions & 0 deletions config/dictionary.yml
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/profanity_filter.rb
Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions 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
4 changes: 2 additions & 2 deletions 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
File renamed without changes.

0 comments on commit a8cd4bd

Please sign in to comment.