class Decidim::Meetings::Registrations::CodeGenerator

This class handles the generation of meeting registration codes

Constants

ALPHABET

excluded digits: 0, 1, excluded letters: O, I

LENGTH

Public Class Methods

new(options = {}) click to toggle source
# File lib/decidim/meetings/registrations/code_generator.rb, line 14
def initialize(options = {})
  @length = options[:length] || LENGTH
end

Public Instance Methods

generate(registration) click to toggle source
# File lib/decidim/meetings/registrations/code_generator.rb, line 18
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 Instance Methods

choose(length) click to toggle source
# File lib/decidim/meetings/registrations/code_generator.rb, line 29
def choose(length)
  limit = ALPHABET.size

  (1..length).map do
    ALPHABET[SecureRandom.random_number(limit)]
  end.join
end