diff --git a/README.rdoc b/README.rdoc index 71b1f60..c340a14 100644 --- a/README.rdoc +++ b/README.rdoc @@ -21,10 +21,10 @@ In your model, add a single line. end == Customization -If you want your hash ids to be different than some other website using the same plugin, you can throw a random string (salt) at hash_id to make it hash out unique ids for your app. +If you want your hash ids to be different than some other website using the same plugin, you can throw a random string (salt) at hash_id to make it hash out unique ids for your app. You can also customize the minimum hash length. class Post < ActiveRecord::Base - hash_id salt: 'bring_your_own_salt' + hash_id salt: 'bring_your_own_salt', min_length: 3 end == Limitations diff --git a/lib/hashids_rails.rb b/lib/hashids_rails.rb index 963c202..7bb1fba 100644 --- a/lib/hashids_rails.rb +++ b/lib/hashids_rails.rb @@ -5,17 +5,18 @@ def hash_id(options = {}) require 'hashids' extend ClassMethods include InstanceMethods - cattr_accessor :hash_salt + cattr_accessor :hash_salt, :min_hash_length self.hash_salt = (options[:salt] || default_salt) + self.min_hash_length = (options[:min_length] || 3) end - def self.hide(id, salt) - hashids = Hashids.new(salt, 3) + def self.hide(id, salt, min_hash_length) + hashids = Hashids.new(salt, min_hash_length) hashids.encode id end - def self.show(id, salt) - hashids = Hashids.new(salt, 3) + def self.show(id, salt, min_hash_length) + hashids = Hashids.new(salt, min_hash_length) decoded = hashids.decode id decoded[0] if decoded end @@ -39,7 +40,7 @@ def has_hashed_id? end def dehash_id(hashed_id) - HashidsRails.show(hashed_id, self.hash_salt) + HashidsRails.show(hashed_id, self.hash_salt, self.min_hash_length) end # Generate a default salt from the Model name @@ -52,7 +53,7 @@ def default_salt module InstanceMethods def to_param - HashidsRails.hide(self.id, self.class.hash_salt) + HashidsRails.hide(self.id, self.class.hash_salt, self.class.min_hash_length) end # Override ActiveRecord::Persistence#reload