Created
July 22, 2011 14:54
-
-
Save nicalpi/1099608 to your computer and use it in GitHub Desktop.
Creates easy shorten UUID in your RAILS APP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## See http:// | |
gem 'base32-crockford', :require => 'base32/crockford' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails g migration AddPermanentUuidToDocuments permanent_uuid:string | |
rake db:migrate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Document < ActiveRecord::Base | |
[..] | |
before_validation :set_permanent_uuid, :on => :create | |
validates_presence_of :permanent_uuid | |
validates_uniqueness_of :permanent_uuid | |
[..] | |
def to_param | |
"#{permanent_uuid}" | |
end | |
protected | |
def set_permanent_id | |
# If you want to a shorter UUID you can play with other options than Time | |
self.permanent_uuid = Base32::Crockford.encode(Time.now.to_i) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment