-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3ad927a
Showing
19 changed files
with
663 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
name: Ruby ${{ matrix.ruby }} | ||
strategy: | ||
matrix: | ||
ruby: | ||
- '3.2.2' | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby }} | ||
bundler-cache: true | ||
- name: Run the default task | ||
run: bundle exec rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
AllCops: | ||
TargetRubyVersion: 3.2 | ||
|
||
Style/StringLiterals: | ||
Enabled: true | ||
EnforcedStyle: double_quotes | ||
|
||
Style/StringLiteralsInInterpolation: | ||
Enabled: true | ||
EnforcedStyle: double_quotes | ||
|
||
Style/FrozenStringLiteralComment: | ||
Enabled: false | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Style/NumericPredicate: | ||
Enabled: false | ||
|
||
Layout/LineLength: | ||
Max: 120 | ||
|
||
Naming/PredicateName: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# Specify your gem's dependencies in based_uuid.gemspec | ||
gemspec | ||
|
||
gem "rake", "~> 13.0" | ||
|
||
gem "minitest", "~> 5.0" | ||
|
||
gem "rubocop", "~> 1.21" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
based_uuid (0.5.0) | ||
activerecord (>= 7.0) | ||
activesupport (>= 7.0) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
activemodel (7.1.2) | ||
activesupport (= 7.1.2) | ||
activerecord (7.1.2) | ||
activemodel (= 7.1.2) | ||
activesupport (= 7.1.2) | ||
timeout (>= 0.4.0) | ||
activesupport (7.1.2) | ||
base64 | ||
bigdecimal | ||
concurrent-ruby (~> 1.0, >= 1.0.2) | ||
connection_pool (>= 2.2.5) | ||
drb | ||
i18n (>= 1.6, < 2) | ||
minitest (>= 5.1) | ||
mutex_m | ||
tzinfo (~> 2.0) | ||
ast (2.4.2) | ||
base64 (0.2.0) | ||
bigdecimal (3.1.4) | ||
concurrent-ruby (1.2.2) | ||
connection_pool (2.4.1) | ||
drb (2.2.0) | ||
ruby2_keywords | ||
i18n (1.14.1) | ||
concurrent-ruby (~> 1.0) | ||
json (2.7.0) | ||
language_server-protocol (3.17.0.3) | ||
minitest (5.20.0) | ||
mutex_m (0.2.0) | ||
parallel (1.23.0) | ||
parser (3.2.2.4) | ||
ast (~> 2.4.1) | ||
racc | ||
racc (1.7.3) | ||
rainbow (3.1.1) | ||
rake (13.1.0) | ||
regexp_parser (2.8.2) | ||
rexml (3.2.6) | ||
rubocop (1.58.0) | ||
json (~> 2.3) | ||
language_server-protocol (>= 3.17.0) | ||
parallel (~> 1.10) | ||
parser (>= 3.2.2.4) | ||
rainbow (>= 2.2.2, < 4.0) | ||
regexp_parser (>= 1.8, < 3.0) | ||
rexml (>= 3.2.5, < 4.0) | ||
rubocop-ast (>= 1.30.0, < 2.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 2.4.0, < 3.0) | ||
rubocop-ast (1.30.0) | ||
parser (>= 3.2.1.0) | ||
ruby-progressbar (1.13.0) | ||
ruby2_keywords (0.0.5) | ||
timeout (0.4.1) | ||
tzinfo (2.0.6) | ||
concurrent-ruby (~> 1.0) | ||
unicode-display_width (2.5.0) | ||
|
||
PLATFORMS | ||
arm64-darwin-22 | ||
x86_64-linux | ||
|
||
DEPENDENCIES | ||
based_uuid! | ||
minitest (~> 5.0) | ||
rake (~> 13.0) | ||
rubocop (~> 1.21) | ||
|
||
BUNDLED WITH | ||
2.4.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) Piotr Chmolowski | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# BasedUUID: double-clickable, URL-friendly UUIDs for your Rails models | ||
|
||
[![Build Status](https://github.com/pch/based_uuid/workflows/Tests/badge.svg)](https://github.com/pch/based_uuid/actions) [![Gem Version](https://badge.fury.io/rb/based_uuid.svg)](https://badge.fury.io/rb/based_uuid) | ||
|
||
|
||
Generate “double-clickable”, URL-friendly UUIDs with (optional) prefixes: | ||
|
||
``` | ||
user_4yoiald3wfbvpeu2sbsk7cssdi | ||
acc_ejwqg7b3gvaphiylb25xq545tm | ||
``` | ||
|
||
This gem works by encoding UUIDs into 26-character lowercase strings using [Crockford’s base32](https://www.crockford.com/base32.html) encoding. The optional prefix is prepended to help you identify the model it represents. | ||
|
||
BasedUUID doesn’t affect how your ActiveRecord primary key UUIDs are stored in the database. Prefix and base32 encoding are only used for presentation. | ||
|
||
## Installation | ||
|
||
Add this line to your `Gemfile`: | ||
|
||
```ruby | ||
gem "based_uuid" | ||
``` | ||
|
||
## Usage | ||
|
||
BasedUUID assumes that you have a [UUID primary key](https://guides.rubyonrails.org/v5.0/active_record_postgresql.html#uuid) in your ActiveRecord model. | ||
|
||
```ruby | ||
class BlogPost < ApplicationRecord | ||
has_based_uuid prefix: :bpo | ||
end | ||
|
||
post = BlogPost.last | ||
post.based_uuid #=> bpo_3ah4veflijgy7de6bflk7k4ld4 | ||
post.based_uuid(prefix: false) #=> 3ah4veflijgy7de6bflk7k4ld4 | ||
``` | ||
|
||
### Lookup | ||
|
||
BasedUUID includes a `find_by_based_uuid` model method to look up records: | ||
|
||
```ruby | ||
BlogPost.find_by_based_uuid("bpo_3ah4veflijgy7de6bflk7k4ld4") | ||
|
||
# or without the prefix: | ||
BlogPost.find_by_based_uuid("3ah4veflijgy7de6bflk7k4ld4") | ||
|
||
# there’s also the bang version: | ||
BlogPost.find_by_based_uuid!("3ah4veflijgy7de6bflk7k4ld4") | ||
``` | ||
|
||
### Generic lookup | ||
|
||
The gem provides a generic lookup method to help you find the correct model for the UUID, based on prefix: | ||
|
||
```ruby | ||
BasedUUID.find("bpo_3ah4veflijgy7de6bflk7k4ld4") | ||
#=> #<BlogPost> | ||
BasedUUID.find("acc_ejwqg7b3gvaphiylb25xq545tm") | ||
#=> #<Account> | ||
``` | ||
|
||
**⚠️ NOTE:** Rails lazy-loads models in the development environment, so this method won’t know about your models until you’ve referenced them at least once. If you’re using this method in a Rails console, you’ll need to run `BlogPost` (or any other model) before you can use it. | ||
|
||
### BasedUUID as default URL identifiers | ||
|
||
BasedUUID aims to be unintrusive and it doesn’t affect how Rails URLs are generated, so if you want to use it as default URL param, add this to your model: | ||
|
||
```ruby | ||
def to_param | ||
based_uuid | ||
end | ||
``` | ||
|
||
* * * | ||
|
||
## 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](https://rubygems.org). | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/pch/based_uuid. | ||
|
||
### Credits | ||
|
||
This gem is heavily inspired by [Stripe IDs](https://stripe.com/docs/api) and the [prefixed_ids](https://github.com/excid3/prefixed_ids/tree/master) gem by Chris Oliver. | ||
|
||
Parts of the base32 encoding code are borrowed from the [ulid gem](https://github.com/rafaelsales/ulid) by Rafael Sales. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rake/testtask" | ||
|
||
Rake::TestTask.new(:test) do |t| | ||
t.libs << "test" | ||
t.libs << "lib" | ||
t.test_files = FileList["test/**/test_*.rb"] | ||
end | ||
|
||
require "rubocop/rake_task" | ||
|
||
RuboCop::RakeTask.new | ||
|
||
task default: %i[test rubocop] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require_relative "lib/based_uuid/version" | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "based_uuid" | ||
spec.version = BasedUUID::VERSION | ||
spec.authors = ["Piotr Chmolowski"] | ||
spec.email = ["piotr@chmolowski.pl"] | ||
|
||
spec.summary = "URL-friendly, Base32-encoded UUIDs for Rails models" | ||
spec.homepage = "https://github.com/pchm/based_uuid" | ||
spec.required_ruby_version = ">= 3.2.0" | ||
spec.license = "MIT" | ||
|
||
spec.files = Dir.chdir(__dir__) do | ||
`git ls-files -z`.split("\x0").reject do |f| | ||
(File.expand_path(f) == __FILE__) || | ||
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile]) | ||
end | ||
end | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_dependency "activerecord", ">= 7.0" | ||
spec.add_dependency "activesupport", ">= 7.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "based_uuid" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
require "irb" | ||
IRB.start(__FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "active_support/core_ext/module/attribute_accessors" | ||
|
||
require_relative "based_uuid/version" | ||
require_relative "based_uuid/base32_uuid" | ||
require_relative "based_uuid/has_based_uuid" | ||
|
||
module BasedUUID | ||
class Error < StandardError; end | ||
|
||
mattr_accessor :delimiter, default: "_" | ||
|
||
class << self | ||
def find(token) | ||
prefix, = split(token) | ||
registered_models.fetch(prefix.to_sym).find_by_based_uuid(token) | ||
rescue KeyError | ||
raise Error, "Unable to find model for `#{prefix}`. Registered prefixes: #{registered_models.keys.join(", ")}" | ||
end | ||
|
||
def register_model_prefix(prefix, model) | ||
registered_models[prefix] = model | ||
end | ||
|
||
def registered_models | ||
@registered_models ||= {} | ||
end | ||
|
||
def split(token) | ||
prefix, _, uuid_base32 = token.to_s.rpartition(delimiter) | ||
[prefix.presence, uuid_base32] | ||
end | ||
|
||
def based_uuid(uuid:, prefix:) | ||
uuid_base32 = Base32UUID.encode(uuid) | ||
return uuid_base32 unless prefix | ||
|
||
"#{prefix}#{delimiter}#{uuid_base32}" | ||
end | ||
end | ||
end |
Oops, something went wrong.