Sha256: 40774245525e5d3cda826ad344cd97efd433f5edb29c00b3ff58c957d1721001
Contents?: true
Size: 989 Bytes
Versions: 83
Compression:
Stored size: 989 Bytes
Contents
# frozen_string_literal: true require "securerandom" module Decidim module Meetings module Registrations # This class handles the generation of meeting registration codes class CodeGenerator # excluded digits: 0, 1, excluded letters: O, I ALPHABET = [*"A".."Z", *"2".."9"] - %w(O I) LENGTH = 8 def initialize(options = {}) @length = options[:length] || LENGTH end def generate(registration) loop do registration_code = choose(@length) # Use the random number if no other registration exists with it. break registration_code unless registration.class.exists?(meeting: registration.meeting, code: registration_code) end end private def choose(length) limit = ALPHABET.size (1..length).map do ALPHABET[SecureRandom.random_number(limit)] end.join end end end end end
Version data entries
83 entries across 83 versions & 1 rubygems