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

  • Weird dreams

    One first with some nearly non-fictional work drama, personality conflict with the Boss. It had something to do with Irish music he was playing…

  • Weird dreams and then loud thoughts when not asleep

    Memories have faded and the plotlines weren't all that cohesive to begin with. Did not seem to get the "it's the last day of school and I haven't…

  • When not asleep..

    I came perilously close to having a Monday this week. I woke up to turn the alarum off at 1805 and then laid back down for a bit, proud that had I…

  • 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