Sha256: 83472f66e425c5ee716b1485ea21c3be48f46e041504bb9ea57e64cb17e05d8d

Contents?: true

Size: 789 Bytes

Versions: 4

Compression:

Stored size: 789 Bytes

Contents

# Base class for most validators in the gem.
#
class BaseValidator < ActiveModel::EachValidator
  # Checks if an attribute value is valid.
  #
  # @param [Object] record object to validate
  # @param [String] attribute name of the object attribute to validate
  # @param [Object] value attribute value
  def validate_each(record, attribute, value)
    allow_blank = options.fetch(:allow_blank, false)

    return if allow_blank && value.blank?
    return if valid?(value, options)

    record.errors[attribute] << options.fetch(:message) do
      I18n.t(error_message_key)
    end
  end

  private

  def valid?(_value, _options)
    fail NotImplementedError
  end

  def error_message_key
    key = self.class.name.underscore.gsub('_validator', '')
    "errors.messages.#{key}"
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
missing_validators-2.3.0 lib/missing_validators/validators/base_validator.rb
missing_validators-2.2.0 lib/missing_validators/validators/base_validator.rb
missing_validators-2.1.0 lib/missing_validators/validators/base_validator.rb
missing_validators-2.0.0 lib/missing_validators/validators/base_validator.rb