Class: LocalizedEachValidator Abstract

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/localized_each_validator.rb,
lib/localized_each_validator/version.rb

Overview

This class is abstract.

Subclass this validator to perform your specific validations.

An ‘EachValidator` that uses the translation table to build its error messages. Override the #valid? method to describe your validation conditions of your subclasses.

The error message translation lookups conform to the standard hierarchy of internationalization keys as described by the ‘ActiveRecord::Errors#generate_message` method. (See its documentation for more information.) The last portion of the translation key path is the error message key, and by default it is the name of the validator class (excepting “Validator”), underscored and demodulized. For example, an `EmailAddressValidator` subclass would use the `email_address` key within the normal ActiveRecord error key structure.

Constant Summary collapse

VERSION =
"2.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.error_keySymbol .error_key(value) ⇒ Object

Overloads:

  • .error_keySymbol

    Returns the error message key this class uses.

    Returns:

    • (Symbol)

      The error message key.

  • .error_key(value) ⇒ Object

    Sets the error message key this class uses.

    Parameters:

    • value (Symbol)

      The new error message key.



39
40
41
42
43
# File 'lib/localized_each_validator.rb', line 39

def self.error_key(value=nil)
  return @error_key || to_s.demodulize.sub(/Validator$/, "").underscore.to_sym unless value

  @error_key = value
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



24
25
26
27
28
29
# File 'lib/localized_each_validator.rb', line 24

def validate_each(record, attribute, value)
  return if options[:allow_nil] && value.nil?
  return if options[:allow_blank] && value.blank?

  record.errors.add(attribute, options[:message] || self.class.error_key) unless valid?(record, attribute, value)
end