Sha256: 58b4119306f792efb6a2f2630a4ae1f8c010a8a6523a0cf065e70107e813c70b

Contents?: true

Size: 1.37 KB

Versions: 45

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

# This validator takes care of ensuring the validated content is
# respectful, doesn't use caps, and overall is meaningful.
class EtiquetteValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if value.blank?

    validate_caps(record, attribute, value)
    validate_marks(record, attribute, value)
    validate_long_words(record, attribute, value)
    validate_caps_first(record, attribute, value)
    validate_length(record, attribute, value)
  end

  private

  def validate_caps(record, attribute, value)
    return if value.scan(/[A-Z]/).length < value.length / 4
    record.errors.add(attribute, options[:message] || :too_much_caps)
  end

  def validate_marks(record, attribute, value)
    return if value.scan(/[!?¡¿]{2,}/).empty?
    record.errors.add(attribute, options[:message] || :too_many_marks)
  end

  def validate_long_words(record, attribute, value)
    return if value.scan(/[A-z]{35,}/).empty?
    record.errors.add(attribute, options[:message] || :long_words)
  end

  def validate_caps_first(record, attribute, value)
    return if value.scan(/^[a-z]{1}/).empty?
    record.errors.add(attribute, options[:message] || :must_start_with_caps)
  end

  def validate_length(record, attribute, value)
    return if value.length > 15
    record.errors.add(attribute, options[:message] || :too_short)
  end
end

Version data entries

45 entries across 45 versions & 2 rubygems

Version Path
decidim-core-0.9.3 app/validators/etiquette_validator.rb
decidim-core-0.9.2 app/validators/etiquette_validator.rb
decidim-core-0.9.1 app/validators/etiquette_validator.rb
decidim-core-0.9.0 app/validators/etiquette_validator.rb
decidim-core-0.8.4 app/validators/etiquette_validator.rb
decidim-core-0.8.3 app/validators/etiquette_validator.rb
decidim-core-0.8.2 app/validators/etiquette_validator.rb
decidim-core-0.8.1 app/validators/etiquette_validator.rb
decidim-core-0.8.0 app/validators/etiquette_validator.rb
decidim-core-0.7.4 app/validators/etiquette_validator.rb
decidim-core-0.7.3 app/validators/etiquette_validator.rb
decidim-core-0.7.2 app/validators/etiquette_validator.rb
decidim-core-0.7.1 app/validators/etiquette_validator.rb
decidim-core-0.7.0 app/validators/etiquette_validator.rb
decidim-core-0.6.8 app/validators/etiquette_validator.rb
decidim-0.6.8 decidim-core/app/validators/etiquette_validator.rb
decidim-core-0.6.7 app/validators/etiquette_validator.rb
decidim-0.6.7 decidim-core/app/validators/etiquette_validator.rb
decidim-core-0.6.6 app/validators/etiquette_validator.rb
decidim-0.6.6 decidim-core/app/validators/etiquette_validator.rb