nil.enroll(aetheric_username, quantum_class_id) (adric) wrote,
nil.enroll(aetheric_username, quantum_class_id)
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: feature, rails
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments