• Stars
    star
    198
  • Rank 189,990 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 3 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Friendly Prefixed IDs for your Ruby on Rails models

Prefixed IDs

๐Ÿ†” Friendly Prefixed IDs for your Ruby on Rails models

Build Status Gem Version

Generate prefixed IDs for your models with a friendly prefix. For example:

user_12345abcd
acct_23lksjdg3

This gem works by hashing the record's original :id attribute using Hashids, which transforms numbers like 347 into a string like yr8. It uses the table's name and an optional additional salt to hash values, returning a string like tablename_hashedvalue.

Inspired by Stripe's prefixed IDs in their API.

๐Ÿš€ Installation

Add this line to your application's Gemfile:

gem 'prefixed_ids'

๐Ÿ“ Usage

Add has_prefix_id :my_prefix to your models to autogenerate prefixed IDs.

class User < ApplicationRecord
  has_prefix_id :user
end

This will generate a value like user_1234abcd.

Note: You should add has_prefix_id before associations because it overrides has_many to add prefix ID lookups.

Prefix ID Param

To retrieve the prefix ID, simply call:

@user.to_param

If to_param override is disabled:

@user.prefix_id
Query by Prefixed ID

To query using the prefixed ID, you can use either find, find_by_prefix_id, or find_by_prefix_id!:

User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")

โš ๏ธ Note that find still finds records by the primary key. Eg. localhost/users/1 still works. If you're targeting security issues by masking the ID, make sure to use find_by_prefix_id and add a salt.

We also override to_param by default so it'll be used in URLs automatically.

To disable find and to_param overrides, simply pass in the options:

class User < ApplicationRecord
  has_prefix_id :user, override_find: false, override_param: false
end
Salt

A salt is a secret value that makes it impossible to reverse engineer IDs. We recommend adding a salt to make your Prefix IDs unguessable.

Global Salt
# config/initializers/prefixed_ids.rb
PrefixedIds.salt = "salt"
Per Model Salt
class User
  has_prefix_id :user, salt: "usersalt"
end

Generic Lookup By Prefix ID

Imagine you have a prefixed ID but you don't know which model it belongs to.

PrefixedIds.find("user_5vJjbzXq9KrLEMm3")
#=> #<User>
PrefixedIds.find("acct_2iAnOP0xGDYk6dpe")
#=> #<Account>

Customizing Prefix IDs

You can customize the prefix, length, and attribute name for PrefixedIds.

class Account < ApplicationRecord
  has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: "", fallback: false
end

By default, find will accept both Prefix IDs and regular IDs. Setting fallback: false will disable finding by regular IDs and will only allow Prefix IDs.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

๐Ÿ™ Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/excid3/prefixed_ids. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

๐Ÿ“ License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the PrefixedIds project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

More Repositories

1

noticed

Notifications for Ruby on Rails applications
Ruby
2,275
star
2

simple_calendar

A wonderfully simple calendar gem for Rails
Ruby
1,476
star
3

jumpstart

Easily jumpstart a new Rails application with a bunch of great features by default
HTML
1,168
star
4

tailwindcss-stimulus-components

A set of StimulusJS components for TailwindCSS apps similar to Bootstrap JS components.
JavaScript
1,083
star
5

madmin

A robust Admin Interface for Ruby on Rails apps
JavaScript
497
star
6

receipts

Easy receipts and invoices for your Rails applications
Ruby
495
star
7

simple_discussion

A simple, extensible Rails forum
Ruby
368
star
8

revise_auth

A pure Rails authentication system like Devise.
Ruby
192
star
9

stack_rescue

A gem for Rails to automatically search Google for exceptions raised in your Rails applications
Ruby
171
star
10

esbuild-rails

Esbuild Rails plugin
JavaScript
137
star
11

signalman

Development tools for Ruby on Rails
Ruby
92
star
12

beginnerbounties.com

Small Projects for Junior Developers
Ruby
77
star
13

dotfiles

My personal dotfiles for macOS: zsh, MacVim, iterm, and more.
Vim Script
70
star
14

stimulus-slimselect

A Stimulus wrapper around SlimSelect
JavaScript
66
star
15

python-apt

This is a Python library interface to libapt, which allows you to query and manipulat APT package repository information using the Python programming language.
C++
63
star
16

combined_time_select

A Rails time_select like Google Calendar with combined hour and minute time_select
Ruby
60
star
17

logbot

A minimal IRC bot for logging channels
Python
59
star
18

textual-solarized

A port of the Solarized theme to Textual IRC
JavaScript
31
star
19

simple_calendar-ajax-example

An example Rails application to demonstrate SimpleCalendar AJAX next/previous links
Ruby
29
star
20

scheduled_tweets

Ruby
28
star
21

gorails-forum

Ruby
24
star
22

betterminitest.com

Learn how to write better Minitest tests
CSS
21
star
23

Rails-Learning

List of resources to learning this amazing platform for building top-quality web apps with Ruby on Rails! โค
20
star
24

trello-stimulus-reflex

A trello clone using Stimulus Reflex
Ruby
17
star
25

railshackathon.com

The RailsHackathon.com website
HTML
17
star
26

asdf-vars

An asdf extension that safely sets global and per-project environment variables, based upon rbenv
Shell
17
star
27

railsconf-2020-actiontext

Railsconf 2020 example app for showcasing ActionText
Ruby
16
star
28

nine_to_five

Ruby
14
star
29

oauthable

Setup, test, and implement the OAuth connections to over 70 different APIs.
Ruby
13
star
30

gorails-ruby

An interactive CLI for GoRails, Railsbytes, Jumpstart, and more.
Ruby
12
star
31

animatedgif.me-v2

Ruby
11
star
32

keryx

Python
11
star
33

current_projects

Lists a user's active github projects
Ruby
9
star
34

israilsdead.com

It's pretty self explanatory I think
HTML
9
star
35

urlgrabber

A high-level cross-protocol url-grabber
Python
8
star
36

Sublime-Ruby-on-Rails.tmLanguage

8
star
37

fluttrly

An elegantly simple todo list
Ruby
8
star
38

simpyl_pastebin

A simple python pastebin
Python
8
star
39

zapier

Zapier API Rubygem
Ruby
8
star
40

excid3-wordpress-theme

My wordpress theme for excid3.com
PHP
7
star
41

animatedgifme

A ruby library for http://animatedgif.me
Ruby
7
star
42

weather_underground

A Ruby gem for the Weather Underground API
Ruby
7
star
43

wistia-player-speed

Add 2x, 1.5x, 0.5x speeds and skip 30 seconds controls to your Wistia video player
HTML
7
star
44

stimulus-js-2.0

A look at Stimulus JS 2.0's new Values and CSS Classes APIs
Ruby
6
star
45

railsconf-2021

Realtime Rails applications with Hotwire & ActionMailbox
Ruby
6
star
46

superleggera

Monitor your CSS in development to create a whitelist for PurgeCSS in Rails with Webpacker
Ruby
6
star
47

deploy_test

See how to deploy Rails to Ubuntu with Capistrano
Ruby
5
star
48

feedforward.io

Curating articles that inspire discussion
Ruby
5
star
49

particlesjs-rails-example

Ruby
5
star
50

clickable-simple_calendar

An example how to make the dates clickable with simple_calendar for creating new events
Ruby
5
star
51

webhookmonitor.com

A tool for recording and monitoring webhooks
Ruby
4
star
52

rpi-pms5003

Raspberry Pi air quality monitoring with PMS5003 and Ruby
Ruby
4
star
53

chef-rails-stack

Chef Solo recipes to setup a Rails stack on Ubuntu 12.04 LTS
Ruby
4
star
54

cfps

Conference talk submissions (CFPs)
4
star
55

react-todolist

A react todo list application with Ruby on Rails
Ruby
4
star
56

landing

My new website landing page
CSS
4
star
57

vuejs-rails-markdown-field

Use VueJS to add markdown previews to any Rails form input field or text area.
Ruby
4
star
58

better_resources

Resources routes that use the same for new and edit so users get less 404s
Ruby
3
star
59

rails-5.2-actiontext

Testing ActionText in Rails 5.2
Ruby
3
star
60

simple_calendar-example

A simple calendar gem example with Rails 6
Ruby
3
star
61

irc_bothost

A Django web application for maintaining python IRC bots
Python
3
star
62

sponsor-hub

A hub for your GitHub sponsors
Ruby
3
star
63

excid3

3
star
64

simple_calendar-full-day-example

Ruby
3
star
65

download_counter

A download counter implemented in Rails3 with an administration interface
Ruby
3
star
66

administrate-trix-editor

Episode 211: Adding the Trix Editor as an Administrate Custom Field
Ruby
3
star
67

gemspec-cheatsheet

An interactive GemSpec cheat sheet
3
star
68

excid3.com

Moving to Jekyll
HTML
3
star
69

reply_parser

Parser for email replies
Ruby
3
star
70

animatedgif.me

With Great Gifs Comes Great Responsibility
Ruby
3
star
71

turbolinks-3-example

Ruby
3
star
72

imageme

Ruby
2
star
73

unwrapt

A pure python layer for offline package managers
Python
2
star
74

chromium

cluster rendering
C
2
star
75

googleweather

Ruby client for the undocumented Google Weather API
Ruby
2
star
76

lithoslabs.com

2
star
77

cultivate

Ruby
2
star
78

blogspam

A Ruby and/or Rails client for Blogspam.net
Ruby
2
star
79

pastebot

An IRC bot that accepts pastes, uploads them, and notifies recipients
Python
2
star
80

excid3bot

A hubot
CoffeeScript
2
star
81

cdnjs-rails

A mechanized way to pull in one to many CDNJS libraries with fall back to local installed versions.
Ruby
2
star
82

keryx_profiler

A website to maintain user submitted profiles for Keryx
Ruby
2
star
83

rugby

A Ruby IRC bot
Ruby
2
star
84

trackler

A package tracker visualizing progress on Google Maps
Ruby
2
star
85

panopticode

Social Volt Contest Entry
Ruby
2
star
86

hatchbox-marketing

2
star
87

No-Net-Debs

List and download deb packages for updates (dist-upgrade included) or new package(s) for a computer without Internet. Hosting in case http://ubuntu.no.sapo.pt/nonetdebs/nonetdebs goes down.
2
star
88

fluttr-desktop

Fluttr desktop application in Python and GTK
Python
2
star
89

omniauth-stripeplatform

A strategy for omniauth and the new beta Stripe.com platofrm.
Ruby
1
star
90

simple_map

Ruby
1
star
91

hacker_rank

A Ruby interface to the HackerRank / InterviewStreet API
Ruby
1
star
92

anonykron

anonykron.com
Ruby
1
star
93

ipcommerce

Ruby
1
star
94

confab

A NodeJS IRC client in the browser
JavaScript
1
star
95

taip-parser

Ruby
1
star
96

omniauth-linode

An Omniauth Strategy for Linode's v4 API
Ruby
1
star
97

ice_rocket

A Rubygem for the IceRocket API
Ruby
1
star
98

combined_time_select-example

Ruby
1
star
99

resume

My resume!
1
star
100

shortbot

An IRC bot that shortens URLs via bit.ly
Python
1
star