Skip to content

Commit

Permalink
switch to profanalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Edgar committed Mar 23, 2009
1 parent 365ef31 commit 90bfb91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
##########################################

require_dependency File.join(File.dirname(__FILE__), 'lib/active_record/validations/validates_not_profane')
ActiveRecord::Base.send :include, OurGreen::Validations::NotProfane
ActiveRecord::Base.send :include, ActiveRecord::Validations::NotProfane
31 changes: 23 additions & 8 deletions lib/active_record/validations/validates_not_profane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# #
##########################################

module OurGreen
module ActiveRecord
module Validations
module NotProfane
##
Expand All @@ -29,7 +29,7 @@ module ClassMethods
# regex to check for bad words. We can expand this to a method that
# scans a dictionary later. for now, let's get the real bad words out.
#
@@is_profane_wordlist = /fuck|shit|cunt|nigger|asshole|titties|faggot/ #most obvious ones off the top of my head


##
# class method that enables profanity validation.
Expand All @@ -47,21 +47,36 @@ def validates_not_profane(*attr_names)
# let's check for that label parameter
addName = false
if configuration.has_key?(:label)
msg_string = "#{configuration[:label]} #{configuration[:message]}"
addName = false
msg_string = "#{configuration[:label]} #{configuration[:message]}"
addName = false
else
msg_string = "#{configuration[:message]}"
addName = true
msg_string = "#{configuration[:message]}"
addName = true
end
if configuration.has_key?(:tolerance)
Profanalyzer.tolerance = configuration[:tolerance]
configuration.delete(:tolerance)
end
if configuration.has_key?(:sexual) && configuration[:sexual] == true
Profanalyzer.check_all = false
Profanalyzer.sexual = true
configuration.delete :sexual
end
if configuration.has_key?(:racist) && configuration[:racist] == true
Profanalyzer.check_all = false
Profanalyzer.racist = true
configuration.delete :racist
end

configuration.store(:message, msg_string)
configuration.delete(:label)

# ok now we do real validation. validates_each is a helper method in validations
# that will run our validation on the list of attributes and our config.

validates_each(attr_names, configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) if addName && value =~ @@is_profane_wordlist
record.errors.add_to_base(configuration[:message]) if !addName && value =~ @@is_profane_wordlist
record.errors.add(attr_name, configuration[:message]) if addName && Profanalyzer.profane?(value)
record.errors.add_to_base(configuration[:message]) if !addName && Profanalyzer.profane?(value)

end
end
Expand Down

0 comments on commit 90bfb91

Please sign in to comment.