Disguise your model ID's when displayed in the UI or API
Add this line to your application's Gemfile:
gem 'super_id'
And then execute:
$ bundle
Or install it yourself as:
$ gem install super_id
In your model, you can use super id for the primary key
# app/models/flock.rb
class Flock < ActiveRecord::Base
has_many :seagulls
use_super_id_for :id
end
You can use super id for foreign keys too:
# app/models/seagull.rb
class Seagull < ActiveRecord::Base
belongs_to :flock
use_super_id_for [:id, :flock_id]
end
SuperId potentially supports multiple encoding algorithms, but currently is limited to short uid.
Optional
Default: :short_id
# app/models/flock.rb
class Flock < ActiveRecord::Base
has_many :seagulls
use_super_id_for :id, as: :short_uid
end
Each encoding algorithm may have options of its own, which can be added to the use_super_id_for
arguments.
Options and defaults
salt
: ''min_hash_length
: 0alphabet
: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
For example, you can use a different salt for each model, and it will encode equal id's differently:
# app/models/flock.rb
class Flock < ActiveRecord::Base
has_many :seagulls
use_super_id_for :id, as: :short_uid, salt: 'foo'
end
# app/models/seagull.rb
class Seagull < ActiveRecord::Base
belongs_to :flock
use_super_id_for [:id, :flock_id], as: :short_uid, salt: 'bar'
end
- Fork it ( https://github.com/[my-github-username]/encodable_ids/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request