# frozen_string_literal: true class ImageHashDuplicationValidator < ActiveModel::Validator def validate(record) hash_candidates = [record.orig_sha512_hash, record.sha512_hash].select &:present? if hash_candidates.any? && !record.post&.hidden_from_users duplicate = record.class.where('(orig_sha512_hash IN (?)) OR (sha512_hash IN (?))', hash_candidates, hash_candidates).first if duplicate && duplicate.id != record.id message = "posts.errors.hash_duplicate_found_#{'hidden_' if duplicate.post&.hidden_from_users}html" record.errors.add :base, I18n.t(message, id: duplicate.post&.id).html_safe end end end end