Browse files

Link provides suggested subdomain on each new instance

  • Loading branch information...
1 parent 28a6e66 commit 2bdd57fda16e84d916f9e522f9941c6b2c9e8e59 @nmanzi committed Oct 8, 2011
Showing with 10 additions and 0 deletions.
  1. +10 −0 app/models/link.rb
View
10 app/models/link.rb
@@ -5,6 +5,7 @@ class Link < ActiveRecord::Base
:presence => true
validates :url, :presence => true, :url => true
before_save :set_defaults
+ after_initialize :set_suggested_subdomain
def self.total_visits
total_visits = 0
@@ -14,6 +15,15 @@ def self.total_visits
return total_visits
end
+ def self.generate_subdomain
+ token = SecureRandom.hex(3)
+ token unless find_by_subdomain(token)
+ end
+
+ def set_suggested_subdomain
+ self.subdomain ||= Link.generate_subdomain
+ end
+
def set_defaults
self.visits ||= 0
end

0 comments on commit 2bdd57f

Please sign in to comment.