Skip to content

tbhb/sqids-rails

Repository files navigation

sqids-rails

GitHub Actions Workflow Status Gem Version

Sqids (formerly Hashids) integration for Ruby on Rails.

From sqids-ruby:

Sqids (pronounced "squids") is a small library that lets you generate unique IDs from numbers. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.

Features:

  • Encode multiple numbers - generate short IDs from one or several non-negative numbers
  • Quick decoding - easily decode IDs back into numbers
  • Unique IDs - generate unique IDs by shuffling the alphabet once
  • ID padding - provide minimum length to make IDs more uniform
  • URL safe - auto-generated IDs do not contain common profanity
  • Randomized output - Sequential input provides nonconsecutive IDs
  • Many implementations - Support for 40+ programming languages

Getting started

Run bundle add sqids-rails or add this line to your application's Gemfile:

gem 'sqids-rails'

And then execute:

bundle

Usage

Add auto-generated sqids columns to your ActiveRecord models with has_sqid:

# Schema: users(sqid:string, long_sqid:string)
class User < ApplicationRecord
  include Sqids::Rails::Model

  has_sqid
  has_sqid :long_sqid, min_length: 24
end

user = User.new
user.save
user.sqid # => "lzNKgEb6ZuaU"
user.sqid_long # => "4y3SVm9M2aV8Olu6p4zZoGij"
user.regenerate_sqid
user.regenerate_long_sqid

has_sqid follows the same behavior as ActiveRecord's built-in has_secure_token

Use a custom attribute name (the default is sqid):

has_sqid :uid

Enforce a minimum length for generated IDs:

has_sqid min_length: 24

Provide a custom alphabet for generated IDs:

has_sqid alphabet: "FxnXM1kBN6cuhsAvjW3Co7l2RePyY8DwaU04Tzt9fHQrqSVKdpimLGIJOgb5ZE"

Prevent specific words from appearing anywhere in generated IDs:

has_sqid blocklist: Set.new(%w[86Rf07])

Performance considerations

  • Consider adding indexes (with uniqueness constraints) when finding records by Sqid.
  • The size of the alphabet can impact performance. A larger alphabet allows for shorter Sqids but slightly increase generation time.
  • The size of the blocklist can impact performance, as each generated ID is checked against the blocklist.

Roadmap

Contributing

Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

Acknowledgements

Thanks to Ivan Akimov (@4kimov) for creating and maintaining Sqids and sqids-ruby, and Roberto Miranda (@robertomiranda) for has_secure_token.

MIT License

Copyright (c) 2024 Anthony Burns

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.