Sha256: 35388a7423ff3e39567fc104bfba3226f3cb3d68cb6140809d05e4e6273e6a21

Contents?: true

Size: 984 Bytes

Versions: 22

Compression:

Stored size: 984 Bytes

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_caps_first(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_caps_first(record, attribute, value)
    return if value.scan(/\A[a-z]{1}/).empty?

    record.errors.add(attribute, options[:message] || :must_start_with_caps)
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
decidim-core-0.27.0 app/validators/etiquette_validator.rb
decidim-core-0.26.3 app/validators/etiquette_validator.rb
decidim-core-0.27.0.rc2 app/validators/etiquette_validator.rb
decidim-core-0.27.0.rc1 app/validators/etiquette_validator.rb
decidim-core-0.26.2 app/validators/etiquette_validator.rb
decidim-core-0.26.1 app/validators/etiquette_validator.rb
decidim-core-0.26.0 app/validators/etiquette_validator.rb
decidim-core-0.26.0.rc2 app/validators/etiquette_validator.rb
decidim-core-0.26.0.rc1 app/validators/etiquette_validator.rb
decidim-core-0.25.2 app/validators/etiquette_validator.rb
decidim-core-0.25.1 app/validators/etiquette_validator.rb
decidim-core-0.25.0 app/validators/etiquette_validator.rb
decidim-core-0.25.0.rc4 app/validators/etiquette_validator.rb
decidim-core-0.25.0.rc3 app/validators/etiquette_validator.rb
decidim-core-0.25.0.rc2 app/validators/etiquette_validator.rb
decidim-core-0.25.0.rc1 app/validators/etiquette_validator.rb
decidim-core-0.24.3 app/validators/etiquette_validator.rb
decidim-core-0.24.2 app/validators/etiquette_validator.rb
decidim-core-0.24.1 app/validators/etiquette_validator.rb
decidim-core-0.24.0 app/validators/etiquette_validator.rb