Topic: Lanuage Filter for user-submitted text?

Hi guys,

this is my first time post!

i'm trying to find information on creating a language filter. Is there any built-in stuff for this?

so far, here's what I've done:

Table swearwords in db with "word" column for the swearword

app/models/swearword.rb
class Swearword < ActiveRecord::Base
  def self.pattern
    pattern=[]
    Swearword.find(:all).each do |sw|
      pattern << sw.word.gsub(/[^a-z0-9\$]/i,'').gsub(/\$/,'\$')
    end
    pattern.join('|') #also tried replacing this line with pattern="badword1|badword2"
  end
end
app/models/player.rb
class Player < ActiveRecord::Base
  has_many :seats
  has_many :ratings
#  has_one :current_rating,
#          :class_name => 'Rating',
#          :order => 'dt_effective_from DESC'

  validates_presence_of :first_name, :last_name
  validate :description_must_not_swear
  protected
  def description_must_not_swear
    errors.add(:description, 'cannot contain offensive language') if description.gsub(/[^a-z]/i,'') =~ %r{Swearword.pattern}
  end
end

But this still lets you post the swear words into the player description! does anyone have any idea why?

Thanks a lot,
Chris

Last edited by theJones (2010-01-17 13:53:19)

Re: Lanuage Filter for user-submitted text?

I haven't tested this out but I think the reason the errors aren't being added is becuase the =~ pattern matcher doesn't return true if it finds a match, instead it returns the position in the string it finds the match.

From the docs:

"cat o' 9 tails" =~ /\d/   #=> 7

So you might have to change the method to something like:

swearword_position = description.gsub(/[^a-z]/i,'') =~ %r{Swearword.pattern}
errors.add(:description, 'cannot contain offensive language') unless swearword_position.nil?