Class: Decidim::Meetings::Registrations::CodeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/meetings/registrations/code_generator.rb

Overview

This class handles the generation of meeting registration codes

Constant Summary collapse

ALPHABET =

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

[*"A".."Z", *"2".."9"] - %w(O I)
LENGTH =
8

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CodeGenerator

Returns a new instance of CodeGenerator.



14
15
16
# File 'lib/decidim/meetings/registrations/code_generator.rb', line 14

def initialize(options = {})
  @length = options[:length] || LENGTH
end

Instance Method Details

#generate(registration) ⇒ Object



18
19
20
21
22
23
24
25
# 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