Created
February 10, 2015 07:40
-
-
Save jcypret/347f054e93d0d6d399c9 to your computer and use it in GitHub Desktop.
Initializer to enable Hashids in a 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
module HashidsSupport | |
extend ActiveSupport::Concern | |
class_methods do | |
def hashids | |
Hashids.new(table_name, 6) | |
end | |
def encode_id(id) | |
hashids.encode(id) | |
end | |
def decode_id(id) | |
hashids.decode(id).first | |
end | |
def hashid_find(hashid) | |
find(decode_id(hashid)) | |
end | |
end | |
def encoded_id | |
self.class.encode_id(id) | |
end | |
def to_param | |
encoded_id | |
end | |
end | |
ActiveRecord::Base.send :include, HashidsSupport |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment