Class: ActiveModel::Validations::ProfanityValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validates_not_profane.rb

Constant Summary collapse

DEFAULT_TOLERANCE =
2

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ProfanityValidator

Returns a new instance of ProfanityValidator.



9
10
11
# File 'lib/validates_not_profane.rb', line 9

def initialize(options)
  super(options.reverse_merge(:message => "contains profanity"))
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/validates_not_profane.rb', line 13

def validate_each(record, attribute, value)
  if options.has_key?(:tolerance)
    Profanalyzer.tolerance = options[:tolerance]
  else
    Profanalyzer.tolerance = DEFAULT_TOLERANCE
  end

  if options[:all]
    Profanalyzer.check_all    = true
  elsif options[:sexual] && options[:racist]
    Profanalyzer.check_all    = false
    Profanalyzer.check_sexual = true
    Profanalyzer.check_racist = true
  elsif options[:sexual]
    Profanalyzer.check_all    = false
    Profanalyzer.check_sexual = true
    Profanalyzer.check_racist = false
  elsif options[:racist]
    Profanalyzer.check_all    = false
    Profanalyzer.check_sexual = false
    Profanalyzer.check_racist = true
  else
    Profanalyzer.check_all    = true
  end

  if Profanalyzer.profane?(value)
    record.errors.add(attribute, :profane, options)
  end
end