Class: ActiveModel::Validations::ProfanityValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::ProfanityValidator
- Defined in:
- lib/validates_not_profane.rb
Constant Summary collapse
- DEFAULT_TOLERANCE =
2
Instance Method Summary collapse
-
#initialize(options) ⇒ ProfanityValidator
constructor
A new instance of ProfanityValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ ProfanityValidator
Returns a new instance of ProfanityValidator.
9 10 11 |
# File 'lib/validates_not_profane.rb', line 9 def initialize() super(.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 .has_key?(:tolerance) Profanalyzer.tolerance = [:tolerance] else Profanalyzer.tolerance = DEFAULT_TOLERANCE end if [:all] Profanalyzer.check_all = true elsif [:sexual] && [:racist] Profanalyzer.check_all = false Profanalyzer.check_sexual = true Profanalyzer.check_racist = true elsif [:sexual] Profanalyzer.check_all = false Profanalyzer.check_sexual = true Profanalyzer.check_racist = false elsif [: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, ) end end |