Class: ShortUrl

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/short_url.rb

Instance Method Summary collapse

Instance Method Details

#generate_and_set_codeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/short_url.rb', line 17

def generate_and_set_code
  if self.code.present?
    puts "a valid code is already set for this short url: " + self.code
  else
    temp_code = random_code
    puts "random code = " + temp_code
  	while ShortUrl.where(:code => temp_code).count > 0
      puts "code already exists! " + temp_code
  		temp_code = random_code
      puts "random code = " + temp_code
    end
    self.code = temp_code
  end
end

#random_codeObject



32
33
34
# File 'app/models/short_url.rb', line 32

def random_code
	rand(36**8).to_s(36).gsub("/","_").gsub(/=+$/,"")
end