Sha256: 6fac0c7252c1973ad5ea784f66e9e7ef48124d334e572402ad6fb5a899c27eee

Contents?: true

Size: 1.6 KB

Versions: 27

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Dsu
  module Validators
    class DescriptionValidator < ActiveModel::Validator
      def validate(record)
        description = record.description

        if description.blank?
          record.errors.add(:description, :blank)
          return
        end

        unless description.is_a?(String)
          # TODO: I18n.
          record.errors.add(:description, 'is the wrong object type. ' \
                                          "\"String\" was expected, but \"#{description.class}\" was received.")
          return
        end

        validate_description record
      end

      private

      def validate_description(record)
        description = record.description

        return if description.length.between?(min_description_length(record), max_description_length(record))

        if description.length < min_description_length(record)
          # TODO: I18n.
          record.errors.add(:description, "is too short: \"#{record.short_description}\" " \
                                          "(minimum is #{min_description_length(record)} characters).")
        elsif description.length > max_description_length(record)
          # TODO: I18n.
          record.errors.add(:description, "is too long: \"#{record.short_description}\" " \
                                          "(maximum is #{max_description_length(record)} characters).")
        end
      end

      def min_description_length(record)
        record.class::MIN_DESCRIPTION_LENGTH
      end

      def max_description_length(record)
        record.class::MAX_DESCRIPTION_LENGTH
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
dsu-3.0.5 lib/dsu/validators/description_validator.rb
dsu-3.0.4 lib/dsu/validators/description_validator.rb
dsu-3.0.3 lib/dsu/validators/description_validator.rb
dsu-3.0.1 lib/dsu/validators/description_validator.rb
dsu-3.0.0 lib/dsu/validators/description_validator.rb
dsu-3.0.0.beta.3 lib/dsu/validators/description_validator.rb
dsu-3.0.0.beta.2 lib/dsu/validators/description_validator.rb
dsu-3.0.0.beta.1 lib/dsu/validators/description_validator.rb
dsu-3.0.0.beta.0 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.12 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.11 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.10 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.9 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.8 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.7 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.6 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.5 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.4 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.3 lib/dsu/validators/description_validator.rb
dsu-3.0.0.alpha.2 lib/dsu/validators/description_validator.rb