logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

unique_username_validator.rb (437B)


  1. # frozen_string_literal: true
  2. class UniqueUsernameValidator < ActiveModel::Validator
  3. def validate(account)
  4. return if account.username.nil?
  5. normalized_username = account.username.downcase.delete('.')
  6. scope = Account.where(domain: nil).where('lower(username) = ?', normalized_username)
  7. scope = scope.where.not(id: account.id) if account.persisted?
  8. account.errors.add(:username, :taken) if scope.exists?
  9. end
  10. end