Module: MuckUsers::Models::MuckAccessCode::ClassMethods

Defined in:
lib/muck-users/models/access_code.rb

Instance Method Summary collapse

Instance Method Details

#active_code?(code) ⇒ Boolean

Checks the database to ensure the specified code is not taken

Returns:

  • (Boolean)


44
45
46
# File 'lib/muck-users/models/access_code.rb', line 44

def active_code?(code)
  self.find_by_code(code)
end

#random_codeObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/muck-users/models/access_code.rb', line 32

def random_code
  code_length = 14 # will generate a code 15 chars long
  letters = ['B', 'C', 'D', 'F', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z']
  numbers = [2, 3, 4, 7, 9]
  promo_set = letters | numbers # combine arrays
  begin
    promo_code = promo_set.sort_by{rand}[0..code_length].to_s # randomize array and take the first 15 elements and make them a string
  end until !self.active_code?(promo_code)
  promo_code
end

#valid_code?(code) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/muck-users/models/access_code.rb', line 26

def valid_code?(code)
  access_code = find_by_code(code)
  valid_code = access_code ? !access_code.overused? : false
  [access_code, valid_code]
end