From 4000d1aeca84ca281f9454a3303ddfcfab5c18eb Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 30 Jul 2024 11:09:37 -0400 Subject: [PATCH] Extricate `Invite::VALID_CODE_CHARACTERS` constant to generate codes --- app/models/invite.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/invite.rb b/app/models/invite.rb index ea095a3ac17..e9b66724b6c 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -20,6 +20,10 @@ class Invite < ApplicationRecord include Expireable COMMENT_SIZE_LIMIT = 420 + VALID_CODE_CHARACTERS = ( + # Lowercase chars & Uppercase chars & numbers with homoglyphs removed + [*('a'..'z'), *('A'..'Z'), *('0'..'9')] - %w(0 1 I l O) + ).freeze belongs_to :user, inverse_of: :invites has_many :users, inverse_of: :invite, dependent: nil @@ -38,8 +42,8 @@ class Invite < ApplicationRecord def set_code loop do - self.code = ([*('a'..'z'), *('A'..'Z'), *('0'..'9')] - %w(0 1 I l O)).sample(8).join - break if Invite.find_by(code: code).nil? + self.code = VALID_CODE_CHARACTERS.sample(8).join + break unless self.class.exists?(code: code) end end end