Require user names to be unique after unicode normalisation
As with the previous checks on case sensitivity this only affects new users, and changes to names of existing users.
This commit is contained in:
parent
75042a04a1
commit
c12f8959dd
5 changed files with 49 additions and 12 deletions
18
app/validators/normalized_uniqueness_validator.rb
Normal file
18
app/validators/normalized_uniqueness_validator.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
class NormalizedUniquenessValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
relation = if options.fetch(:case_sensitive, true)
|
||||
record.class.where("NORMALIZE(#{attribute}, NFKC) = NORMALIZE(?, NFKC)", value)
|
||||
else
|
||||
record.class.where("LOWER(NORMALIZE(#{attribute}, NFKC)) = LOWER(NORMALIZE(?, NFKC))", value)
|
||||
end
|
||||
|
||||
relation = relation.where.not(record.class.primary_key => [record.id_in_database]) if record.persisted?
|
||||
|
||||
if relation.exists?
|
||||
error_options = options.except(:case_sensitive)
|
||||
error_options[:value] = value
|
||||
|
||||
record.errors.add(attribute, :taken, **error_options)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue