Use normalizes api for UsernameBlock username change (#35606)

This commit is contained in:
Matt Jankowski 2025-08-05 10:10:28 -04:00 committed by GitHub
parent ffeb5da991
commit a968849e9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -29,10 +29,12 @@ class UsernameBlock < ApplicationRecord
validates :username, presence: true, uniqueness: true
scope :matches_exactly, ->(str) { where(exact: true).where(normalized_username: str) }
scope :matches_partially, ->(str) { where(exact: false).where(Arel::Nodes.build_quoted(str).matches(Arel::Nodes.build_quoted('%').concat(arel_table[:normalized_username]).concat(Arel::Nodes.build_quoted('%')))) }
scope :matches_partially, ->(str) { where(exact: false).where(Arel::Nodes.build_quoted(normalize_value_for(:normalized_username, str)).matches(Arel::Nodes.build_quoted('%').concat(arel_table[:normalized_username]).concat(Arel::Nodes.build_quoted('%')))) }
before_save :set_normalized_username
normalizes :normalized_username, with: ->(value) { value.downcase.gsub(Regexp.union(HOMOGLYPHS.keys), HOMOGLYPHS) }
def comparison
exact? ? 'equals' : 'contains'
end
@ -42,8 +44,7 @@ class UsernameBlock < ApplicationRecord
end
def self.matches?(str, allow_with_approval: false)
normalized_str = str.downcase.gsub(Regexp.union(HOMOGLYPHS.keys), HOMOGLYPHS)
matches_exactly(normalized_str).or(matches_partially(normalized_str)).where(allow_with_approval: allow_with_approval).any?
matches_exactly(str).or(matches_partially(str)).where(allow_with_approval: allow_with_approval).any?
end
def to_log_human_identifier
@ -53,10 +54,6 @@ class UsernameBlock < ApplicationRecord
private
def set_normalized_username
self.normalized_username = normalize(username)
end
def normalize(str)
str.downcase.gsub(Regexp.union(HOMOGLYPHS.keys), HOMOGLYPHS)
self.normalized_username = username
end
end