Class: AbAdmin::Concerns::Validations::UniqTranslationValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/ab_admin/concerns/validations.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


5
6
7
8
9
10
11
12
# File 'lib/ab_admin/concerns/validations.rb', line 5

def validate_each(record, attribute, value)
  (options[:locales] || ::I18n.available_locales).each do |l|
    next if record.read_attribute(attribute, locale: l).blank?
    records_scope = record.class.const_get(:Translation).where("#{record.class.model_name.singular}_id != #{record.id || 0}")
    same = records_scope.where(name: record.read_attribute(attribute, locale: l), locale: l.to_s).exists?
    record.errors.add("#{attribute}_#{l}", :taken) if same
  end
end