Added profanity filter - VRFS-219

This commit is contained in:
Mike Slemmer 2013-07-26 01:07:24 -07:00
parent dffc3f61ad
commit e6dd4fea7a
12 changed files with 81 additions and 20 deletions

View File

@ -1,24 +1,35 @@
module JamRuby
class Profanity
@@dictionary_file = File.join('config/profanity.yml')
@@dictionary = nil
def self.dictionary
@@dictionary ||= YAML.load_file(@@dictionary_file)
end
class Profanity
@@dictionary_file = File.join('config/profanity.yml')
@@dictionary = nil
def self.dictionary
@@dictionary ||= YAML.load_file(@@dictionary_file)
end
def self.check_word(word)
dictionary.include?(word.downcase)
end
dictionary.include?(word.downcase)
end
def self.is_profane?(text)
return false if text.nil?
text.split(/\W+/).each do |word|
return true if check_word(word)
end
return false
text.split(/\W+/).each do |word|
return true if check_word(word)
end
return false
end
end
end
# This needs to be outside the module to work.
class NoProfanityValidator < ActiveModel::EachValidator
# implement the method called during validation
def validate_each(record, attribute, value)
record.errors[attribute] << 'Cannot contain profanity' if Profanity.is_profane?(value)
end
end

View File

@ -5,6 +5,8 @@ module JamRuby
self.primary_key = 'id'
validates :biography, no_profanity: true
# musicians
has_many :band_musicians, :class_name => "JamRuby::BandMusician"
has_many :users, :through => :band_musicians, :class_name => "JamRuby::User"

View File

@ -1,6 +1,8 @@
module JamRuby
class ClaimedRecording < ActiveRecord::Base
validates :name, no_profanity: true
belongs_to :recording, :class_name => "JamRuby::Recording", :inverse_of => :claimed_recordings
belongs_to :user, :class_name => "JamRuby::User", :inverse_of => :claimed_recordings
belongs_to :genre, :class_name => "JamRuby::Genre"

View File

@ -11,6 +11,7 @@ module JamRuby
validates :user_id, :presence => true
validates :friend_id, :presence => true
#validates :status, :inclusion => {:in => STATUS}
validates :message, no_profanity: true
def to_s
return "#{self.id} => #{self.user.to_s}:#{self.friend.to_s}"
@ -67,4 +68,4 @@ module JamRuby
return friend_request
end
end
end
end

View File

@ -16,7 +16,7 @@ module JamRuby
validates :email, :presence => true, format: {with: VALID_EMAIL_REGEX}
validates :autofriend, :inclusion => {:in => [nil, true, false]}
validates :invitation_code, :presence => true
validates :note, length: {maximum: 400} # 400 == arbitrary.
validates :note, length: {maximum: 400}, no_profanity: true # 400 == arbitrary.
validate :valid_personalized_invitation
validate :not_accepted_twice

View File

@ -11,7 +11,7 @@ module JamRuby
validates :user, :presence => true
validates :music_session, :presence => true
validates :text, presence: false, length: {maximum: 140} # arbitrary decision of 140. the database is at 2000 max on this field
validates :text, presence: false, no_profanity: true, length: {maximum: 140} # arbitrary decision of 140. the database is at 2000 max on this field
validates_uniqueness_of :user_id, :scope => :music_session_id
@ -41,4 +41,4 @@ module JamRuby
return JoinRequest.find(id, :conditions => ["user_id = ? OR music_session_id IN (select music_session_id from connections WHERE user_id = ?)", user.id, user.id])
end
end
end
end

View File

@ -27,7 +27,7 @@ module JamRuby
JamRuby::MusicSessionHistory.removed_music_session(obj.user_id, obj.id)
end
validates :description, :presence => true
validates :description, :presence => true, :no_profanity => true
validates :fan_chat, :inclusion => {:in => [true, false]}
validates :fan_access, :inclusion => {:in => [true, false]}
validates :approval_required, :inclusion => {:in => [true, false]}

View File

@ -108,8 +108,8 @@ module JamRuby
before_save :create_remember_token, :if => :should_validate_password?
before_save :stringify_avatar_info , :if => :updating_avatar
validates :first_name, presence: true, length: {maximum: 50}
validates :last_name, presence: true, length: {maximum: 50}
validates :first_name, presence: true, length: {maximum: 50}, no_profanity: true
validates :last_name, presence: true, length: {maximum: 50}, no_profanity: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}
validates :update_email, presence: true, format: {with: VALID_EMAIL_REGEX}, :if => :updating_email

View File

@ -90,4 +90,16 @@ describe InvitedUser do
invited_users.length.should == 1
invited_users[0].should == invited_user
end
it 'should not allow note to have profanity' do
user1 = FactoryGirl.create(:user)
# create the invitation from the end-user
invited_user = FactoryGirl.create(:invited_user, :sender => user1)
invited_user.note = 'fuck you'
invited_user.save
invited_user.valid?.should be_false
end
end

View File

@ -38,5 +38,14 @@ describe JoinRequest do
join_request2.save.should be_false
join_request2.errors.get(:user_id) == ["has already been taken"]
end
end
it "cant contain profanity in the text" do
user1 = FactoryGirl.create(:user)
music_session = FactoryGirl.create(:music_session, :creator => user1)
music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session)
join_request = JoinRequest.new(:user => user1, :music_session => music_session, :text => "fuck you")
join_request.save
join_request.valid?.should be_false
end
end

View File

@ -360,4 +360,13 @@ describe MusicSession do
music_session.valid?.should be_false
music_session.errors["legal_terms"].should == ["is not included in the list"]
end
it "cannot have profanity in the description" do
user1 = FactoryGirl.create(:user)
music_session = FactoryGirl.build(:music_session, :creator => user1, :legal_terms=> false, :description => "fuck you")
music_session.save
music_session.valid?.should be_false
end
end

View File

@ -42,6 +42,7 @@ describe User do
it { should be_admin }
end
describe "when first name is not present" do
before { @user.first_name = " " }
@ -68,6 +69,20 @@ describe User do
it { should_not be_valid }
end
describe "first or last name cant have profanity" do
it "should not let the first name have profanity" do
@user.first_name = "fuck you"
@user.save
@user.should_not be_valid
end
it "should not let the last name have profanity" do
@user.last_name = "fuck you"
@user.save
@user.should_not be_valid
end
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]