Sha256: defd104b1a6cf799c9682e9f5a5a380f4022cd1f3c55a65a7ac447b2dee10f5d

Contents?: true

Size: 1.06 KB

Versions: 15

Compression:

Stored size: 1.06 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
  include ActionView::Helpers::SanitizeHelper

  def validate_each(record, attribute, value)
    return if value.blank?

    text_value = strip_tags(value)

    validate_caps(record, attribute, text_value)
    validate_marks(record, attribute, text_value)
    validate_caps_first(record, attribute, text_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

15 entries across 15 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/validators/etiquette_validator.rb
decidim-core-0.27.8 app/validators/etiquette_validator.rb
decidim-core-0.27.7 app/validators/etiquette_validator.rb
decidim-core-0.27.6 app/validators/etiquette_validator.rb
decidim-core-0.26.10 app/validators/etiquette_validator.rb
decidim-core-0.26.9 app/validators/etiquette_validator.rb
decidim-core-0.27.5 app/validators/etiquette_validator.rb
decidim-core-0.26.8 app/validators/etiquette_validator.rb
decidim-core-0.27.4 app/validators/etiquette_validator.rb
decidim-core-0.27.3 app/validators/etiquette_validator.rb
decidim-core-0.26.7 app/validators/etiquette_validator.rb
decidim-core-0.26.5 app/validators/etiquette_validator.rb
decidim-core-0.27.2 app/validators/etiquette_validator.rb
decidim-core-0.27.1 app/validators/etiquette_validator.rb
decidim-core-0.26.4 app/validators/etiquette_validator.rb