<commit_before>require 'active_record/validations/uniqueness.rb'

ActiveRecord::Validations::UniquenessValidator.class_eval do
  def validate_each_with_translations(record, attribute, value)
    klass = record.class
    if klass.translates? && klass.translated_attribute_names.include?(attribute.to_sym)
      finder_class = klass.translation_class
      table = finder_class.arel_table

      relation = build_relation(finder_class, table, attribute, value).and(table[:locale].eq(Globalize.locale))
      relation = relation.and(table[:"#{klass.name.downcase}_id"].not_eq(record.send(:id))) if record.persisted?

#      TODO: add scope with translated attributes

      if finder_class.unscoped.where(relation).exists?
        record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
      end

    else
      validate_each_without_translations(record, attribute, value)
    end
  end
  alias_method_chain :validate_each, :translations
end<commit_msg>use reflect_on_association to get correct foreign key for translations, re: #7<commit_after>require 'active_record/validations/uniqueness.rb'

ActiveRecord::Validations::UniquenessValidator.class_eval do
  def validate_each_with_translations(record, attribute, value)
    klass = record.class
    if klass.translates? && klass.translated_attribute_names.include?(attribute.to_sym)
      finder_class = klass.translation_class
      table = finder_class.arel_table

      relation = build_relation(finder_class, table, attribute, value).and(table[:locale].eq(Globalize.locale))
      relation = relation.and(table[klass.reflect_on_association(:translations).foreign_key].not_eq(record.send(:id))) if record.persisted?

#      TODO: add scope with translated attributes

      if finder_class.unscoped.where(relation).exists?
        record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
      end

    else
      validate_each_without_translations(record, attribute, value)
    end
  end
  alias_method_chain :validate_each, :translations
end
{'fraction_non_alphanumeric': 0.1017274472168906, 'fraction_numerical': 0.0004798464491362764, 'mean_word_length': 5.078717201166181, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7653133', 'n_tokens_mistral': 612, 'n_tokens_neox': 606, 'n_words': 109}