?

Log in

No account? Create an account
entries friends calendar profile adric.net Previous Previous Next Next
Unique Username? - nil.enroll(aetheric_username, quantum_class_id)
yljatlhQo'! QIch lo'laltbebej!
adric
adric
Unique Username?

### Check for unique username and generate a better one if needed.
## Refined to now only hit the database once per call.
def unique_username?
  ##p "Debug: guess at top of predicate: #{@guess}"  
  ## Cache the usernames of everyone with the same last name
  @taken = MoocashOrder.find_all_by_student_lname(self.student_lname).collect 
    do |moo| moo.username end
  ##p "Debug: taken list before while: #{@taken}"

  ##implied for  @inc 0 to length of fname
  @inc = 1

  ##prime @guess with the one we know doesn't work, so we enter the while ...
  @guess = self.username
  ##p "Debug: guess before while: #{@guess}"

  ## Keep trying guesses until we get a non match
  while @taken.include?(@guess)
      ## use more of first name:
      @guess = "#{self.student_fname.downcase[0,@inc+1]}#{self.student_lname.downcase}"
      ##p "Debug: first guess in while: #{@guess}"
      ## shortcut out if we find a non-matching username
      break if not @taken.include?(@guess)
      
      ## and then try appending numbers, ugh
      @guess = "#{@guess}#{@inc}"
      ##p "Debug: second guess in while: #{@guess}"
      ## shortcut out if we find a non-matching username
      break if not @taken.include?(@guess)
      
      ##sanity break:
      if @inc > 29
        p "Incremented to 29, giving up!" 
      end
      
      ## and increment imaginary for loop
      @inc = @inc + 1 
  end 
  ##p "Debug: guess after while: #{@guess}"
  self.username = @guess
end 

Tags: ,

Leave a comment