Class: TranslatedAttr::Validators::TranslationsUniqValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/translated_attr/active_record_extensions.rb

Overview

Uniqueness validator

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/translated_attr/active_record_extensions.rb', line 21

def validate_each(record, attribute, value)
  translations_present = record.translations.map{ |t| t.locale.try(:to_sym) }
  # Removes duplicate elements from self.
  # Returns nil if no changes are made (that is, no duplicates are found).
  if translations_present.uniq!
    record.errors[attribute] << I18n.t('translated_attr.errors.translations_uniq')
  end
end