unique_username_validator.rb 411 B

1234567891011121314
  1. # frozen_string_literal: true
  2. # See also: USERNAME_RE in the Account class
  3. class UniqueUsernameValidator < ActiveModel::Validator
  4. def validate(account)
  5. return if account.username.blank?
  6. scope = Account.with_username(account.username).with_domain(account.domain)
  7. scope = scope.where.not(id: account.id) if account.persisted?
  8. account.errors.add(:username, :taken) if scope.exists?
  9. end
  10. end