The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

18 lines
579 B

  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.nil?
  6. normalized_username = account.username.downcase
  7. normalized_domain = account.domain&.downcase
  8. scope = Account.where(Account.arel_table[:username].lower.eq normalized_username).where(Account.arel_table[:domain].lower.eq normalized_domain)
  9. scope = scope.where.not(id: account.id) if account.persisted?
  10. account.errors.add(:username, :taken) if scope.exists?
  11. end
  12. end