class NotShoutingValidator
this is not namespaced so that we can do
validates :body, not_shouting: true
Public Instance Methods
shouting?(value)
click to toggle source
# File lib/validates_not_shouting/not_shouting_validator.rb, line 12 def shouting?(value) threshold = options.fetch(:threshold, 0.5) (value.gsub(/[^A-Z]/, '').length.to_f / value.length) > threshold end
validate_each(record, attribute, value)
click to toggle source
# File lib/validates_not_shouting/not_shouting_validator.rb, line 4 def validate_each(record, attribute, value) return true if value.blank? if shouting?(value) record.errors[attribute] << options.fetch(:message, "cannot be mostly caps. It's not polite to shout.") end end