Skip to content

Instantly share code, notes, and snippets.

@jcypret
Created February 10, 2015 07:40
Show Gist options
  • Save jcypret/347f054e93d0d6d399c9 to your computer and use it in GitHub Desktop.
Save jcypret/347f054e93d0d6d399c9 to your computer and use it in GitHub Desktop.
Initializer to enable Hashids in a Rails app
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