Sha256: 8041e6db2e36dac54226343775ecc6d2a0ed865b9a9efd9b5f5e8047a825f5fb

Contents?: true

Size: 516 Bytes

Versions: 1

Compression:

Stored size: 516 Bytes

Contents

# this is not namespaced so that we can do
#   validates :body, not_shouting: true
class NotShoutingValidator < ActiveModel::EachValidator
  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

  def shouting?(value)
    threshold = options.fetch(:threshold, 0.5)
    (value.gsub(/[^A-Z]/, '').length.to_f / value.length) > threshold
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
validates_not_shouting-0.0.1 lib/validates_not_shouting/not_shouting_validator.rb