19 lines
399 B
Ruby
19 lines
399 B
Ruby
# -*- encoding : utf-8 -*-
|
|
class TinyUrl < ApplicationRecord
|
|
#attr_accessible :nbr_views, :slug, :start_at, :stop_at, :url
|
|
|
|
validates :slug, :presence => true, :uniqueness => true
|
|
|
|
|
|
before_validation do
|
|
|
|
if !self.slug?
|
|
self.slug = loop do
|
|
|
|
self.slug = SecureRandom.hex(3)
|
|
break self.slug unless TinyUrl.exists?(slug: self.slug)
|
|
end
|
|
end
|
|
end
|
|
end
|