Announcing Profanity Filter for Rails

By Adam Bair March 17, 2011 in rails, open source, gem, profanity

I'm pleased to announce Profanity Filter for Rails. This gem filters undesirable words from text and displays a friendly version. Filtering is non-destructive by default but has the option of replacing the original text. It's based on a dictionary that is customizable so you can add additional words as needed.

Profanity Filter is the evolution of a plugin I created a few years ago called Fu-Fu (the Profanity Filter for Rails). This new version has been renamed, tested with Rails 3 and has been moved over to the Intridea github account to ensure proper support (bug fixing, development time, etc) going forward.

Installation

gem install profanity_filter

Rails 3 with Bundler

Open your Gemfile and add:

gem "profanity_filter"

Rails 2.x without Bundler

Open config/environment.rb and add:

config.gem "profanity_filter"

Usage: Basic

You can call the profanity_filter method within your ActiveRecord models.

class Post < ActiveRecord::Base
  profanity_filter :title, :body
end

Usage: 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 be replaced by value in config/dictionary.yml

profanity_filter :foo, :bar, :method => 'vowels'
  #=> banned words will have their vowels replaced

profanity_filter :foo, :bar, :method => 'hollow'
  #=> all letters except the first and last will be replaced

Non-destructive versions of the filtered attribute:
  some_model.foo => 'filtered version'
  some_model.foo_original => 'non-filtered version'

Usage: Destructive

Filters content in the database, original text is lost. Note the "!" when calling the profanity_filter method.

profanity_filter! :foo, :bar
  #=> banned words will be replaced with @#=>$%

profanity_filter! :foo, :bar, :method => 'dictionary'
  #=> banned words will be replaced by value in config/dictionary.yml

profanity_filter! :foo, :bar, :method => 'vowels'
  #=> banned words will have their vowels replaced

profanity_filter! :foo, :bar, :method => 'hollow'
  #=> all letters except the first and last will be replaced

You can find additional information in the README.

Contributing

I've used this on a number of projects and it's served me well but it's by no means perfect. If you find something that doesn't work for you or have a suggestion please let me know and I'll see what I can do (directly or through Github Issues). If you have an improvement/refactoring I welcome pull requests on Github. The project has a full test suite with benchmarking and the code base is tiny so feel free to jump in!

You can find Profanity Filter on Intridea's Github account.

blog comments powered by Disqus