based_uuid 0.5.2 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/README.md +45 -16
- data/lib/based_uuid/has_based_uuid.rb +20 -11
- data/lib/based_uuid/version.rb +1 -1
- data/lib/based_uuid.rb +6 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5499342189f8b46811d3d620d6074dbd6620bc3cbc8882f39261b428a5fbcc41
|
4
|
+
data.tar.gz: 4ed71b83bf2f7271466256c5a215f217ba4c300665aeb895e6fa2da2b1308ea9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edaf5ab4385cb65dff10739d12801b98c0085fb9d54d9841c480783c014ff47d2359b16acaf03b426a7a17049968f0bea1d54e0cc5063d4679105bb5813a9655
|
7
|
+
data.tar.gz: c81d9c3aa7c122516c846cb0470b45d2352f2db8bcc051a22c6da6d5d7f9a33892dab095f719677512cfd9d29d8f891f971c1d084f212cbe4283ea76425a87c5
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# BasedUUID:
|
1
|
+
# BasedUUID: URL-friendly UUIDs for Rails models
|
2
2
|
|
3
3
|
[![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)
|
4
4
|
|
@@ -6,13 +6,13 @@
|
|
6
6
|
Generate “double-clickable”, URL-friendly UUIDs with (optional) prefixes:
|
7
7
|
|
8
8
|
```
|
9
|
-
|
10
|
-
|
9
|
+
user_763j02ryxh8dbs56mgcjqrmmgt #=> e61c802c-7bb1-4357-929a-9064af8a521a
|
10
|
+
bpo_12dm1qresn83st62reqdw7f7cv #=> 226d037c-3b35-40f3-a30b-0ebb78779d9b
|
11
11
|
```
|
12
12
|
|
13
|
-
This gem
|
13
|
+
This gem encodes UUID primary keys into 26-character lowercase strings using [Crockford’s base32](https://www.crockford.com/base32.html) encoding. The optional prefix helps you identify the model it represents.
|
14
14
|
|
15
|
-
BasedUUID doesn’t affect how your
|
15
|
+
BasedUUID assumes that you have a [UUID primary key](https://guides.rubyonrails.org/v5.0/active_record_postgresql.html#uuid) (`id`) in your ActiveRecord model. It doesn’t affect how your primary key UUIDs are stored in the database. Prefixes and base32-encoded strings are only used for presentation.
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
@@ -24,7 +24,7 @@ gem "based_uuid"
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
|
27
|
+
Add the following line to your model class:
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
class BlogPost < ApplicationRecord
|
@@ -32,8 +32,8 @@ class BlogPost < ApplicationRecord
|
|
32
32
|
end
|
33
33
|
|
34
34
|
post = BlogPost.last
|
35
|
-
post.based_uuid #=>
|
36
|
-
post.based_uuid(prefix: false) #=>
|
35
|
+
post.based_uuid #=> bpo_12dm1qresn83st62reqdw7f7cv
|
36
|
+
post.based_uuid(prefix: false) #=> 12dm1qresn83st62reqdw7f7cv
|
37
37
|
```
|
38
38
|
|
39
39
|
### Lookup
|
@@ -41,13 +41,13 @@ post.based_uuid(prefix: false) #=> 3ah4veflijgy7de6bflk7k4ld4
|
|
41
41
|
BasedUUID includes a `find_by_based_uuid` model method to look up records:
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
BlogPost.find_by_based_uuid("
|
44
|
+
BlogPost.find_by_based_uuid("bpo_12dm1qresn83st62reqdw7f7cv")
|
45
45
|
|
46
46
|
# or without the prefix:
|
47
|
-
BlogPost.find_by_based_uuid("
|
47
|
+
BlogPost.find_by_based_uuid("12dm1qresn83st62reqdw7f7cv")
|
48
48
|
|
49
49
|
# there’s also the bang version:
|
50
|
-
BlogPost.find_by_based_uuid!("
|
50
|
+
BlogPost.find_by_based_uuid!("12dm1qresn83st62reqdw7f7cv")
|
51
51
|
```
|
52
52
|
|
53
53
|
### Generic lookup
|
@@ -55,17 +55,17 @@ BlogPost.find_by_based_uuid!("3ah4veflijgy7de6bflk7k4ld4")
|
|
55
55
|
The gem provides a generic lookup method to help you find the correct model for the UUID, based on prefix:
|
56
56
|
|
57
57
|
```ruby
|
58
|
-
BasedUUID.find("
|
58
|
+
BasedUUID.find("bpo_12dm1qresn83st62reqdw7f7cv")
|
59
59
|
#=> #<BlogPost>
|
60
|
-
BasedUUID.find("
|
61
|
-
#=> #<
|
60
|
+
BasedUUID.find("user_763j02ryxh8dbs56mgcjqrmmgt")
|
61
|
+
#=> #<User>
|
62
62
|
```
|
63
63
|
|
64
64
|
**⚠️ 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.
|
65
65
|
|
66
66
|
### BasedUUID as default URL identifiers
|
67
67
|
|
68
|
-
BasedUUID aims to be
|
68
|
+
BasedUUID aims to be non-intrusive 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:
|
69
69
|
|
70
70
|
```ruby
|
71
71
|
def to_param
|
@@ -73,6 +73,35 @@ def to_param
|
|
73
73
|
end
|
74
74
|
```
|
75
75
|
|
76
|
+
### Custom UUID column name
|
77
|
+
|
78
|
+
`BasedUUID` will respect the value of `Model.primary_key`, so it supports custom primary key names:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Transaction < ApplicationRecord
|
82
|
+
self.primary_key = "txid"
|
83
|
+
|
84
|
+
has_based_uuid prefix: :tx
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
If you want to use a different column, other than the primary key, you can pass it as an option to `has_based_uuid`:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
class Session < ApplicationRecord
|
92
|
+
has_based_uuid prefix: :sid, uuid_column: :session_id
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
### Use outside ActiveRecord
|
97
|
+
|
98
|
+
BasedUUID can be used outside ActiveRecord, too. You can encode any UUID with it:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
BasedUUID.encode(uuid: "226d037c-3b35-40f3-a30b-0ebb78779d9b", prefix: :bpo)
|
102
|
+
BasedUUID.decode("bpo_12dm1qresn83st62reqdw7f7cv")
|
103
|
+
```
|
104
|
+
|
76
105
|
* * *
|
77
106
|
|
78
107
|
## Development
|
@@ -89,4 +118,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/pch/ba
|
|
89
118
|
|
90
119
|
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.
|
91
120
|
|
92
|
-
Parts of the base32 encoding code are borrowed from the [ulid
|
121
|
+
Parts of the base32 encoding code are borrowed from the [ulid](https://github.com/rafaelsales/ulid) gem by Rafael Sales.
|
@@ -2,7 +2,6 @@ require "active_support/lazy_load_hooks"
|
|
2
2
|
require "active_support/concern"
|
3
3
|
require "active_support/core_ext/object/blank"
|
4
4
|
require "active_support/core_ext/class/attribute"
|
5
|
-
require "active_record"
|
6
5
|
|
7
6
|
module BasedUUID
|
8
7
|
module HasBasedUUID
|
@@ -10,13 +9,16 @@ module BasedUUID
|
|
10
9
|
|
11
10
|
included do
|
12
11
|
class_attribute :_based_uuid_prefix
|
12
|
+
class_attribute :_based_uuid_column
|
13
13
|
end
|
14
14
|
|
15
15
|
class_methods do
|
16
|
-
def has_based_uuid(prefix: nil)
|
16
|
+
def has_based_uuid(prefix: nil, uuid_column: primary_key)
|
17
17
|
include ModelExtensions
|
18
18
|
|
19
19
|
self._based_uuid_prefix = prefix
|
20
|
+
self._based_uuid_column = uuid_column
|
21
|
+
|
20
22
|
BasedUUID.register_model_prefix(prefix, self) if prefix
|
21
23
|
end
|
22
24
|
end
|
@@ -27,27 +29,34 @@ module BasedUUID
|
|
27
29
|
|
28
30
|
class_methods do
|
29
31
|
def find_by_based_uuid(token)
|
30
|
-
|
31
|
-
raise ArgumentError, "Invalid prefix" if prefix && prefix.to_sym != _based_uuid_prefix
|
32
|
-
|
33
|
-
find_by(primary_key => Base32UUID.decode(uuid_base32))
|
32
|
+
decode_and_find_based_uuid(token:)
|
34
33
|
end
|
35
34
|
|
36
35
|
def find_by_based_uuid!(token)
|
37
|
-
|
36
|
+
decode_and_find_based_uuid(token:, raise_error: true)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def decode_and_find_based_uuid(token:, raise_error: false)
|
42
|
+
prefix, uuid_base32 = BasedUUID.split(token)
|
43
|
+
raise ArgumentError, "Invalid prefix" if prefix && prefix.to_sym != _based_uuid_prefix
|
44
|
+
|
45
|
+
method_name = raise_error ? :find_by! : :find_by
|
46
|
+
send(method_name, _based_uuid_column => Base32UUID.decode(uuid_base32))
|
38
47
|
end
|
39
48
|
end
|
40
49
|
|
41
50
|
def based_uuid(prefix: true)
|
42
|
-
raise ArgumentError, "UUID is empty" if
|
51
|
+
raise ArgumentError, "UUID is empty" if _uuid_column_value.blank?
|
43
52
|
|
44
|
-
BasedUUID.
|
53
|
+
BasedUUID.encode(uuid: _uuid_column_value, prefix: prefix ? self.class._based_uuid_prefix : nil)
|
45
54
|
end
|
46
55
|
|
47
56
|
private
|
48
57
|
|
49
|
-
def
|
50
|
-
self[self.class.
|
58
|
+
def _uuid_column_value
|
59
|
+
self[self.class._based_uuid_column]
|
51
60
|
end
|
52
61
|
end
|
53
62
|
end
|
data/lib/based_uuid/version.rb
CHANGED
data/lib/based_uuid.rb
CHANGED
@@ -30,11 +30,16 @@ module BasedUUID
|
|
30
30
|
[prefix.presence, uuid_base32]
|
31
31
|
end
|
32
32
|
|
33
|
-
def
|
33
|
+
def encode(uuid:, prefix:)
|
34
34
|
uuid_base32 = Base32UUID.encode(uuid)
|
35
35
|
return uuid_base32 unless prefix
|
36
36
|
|
37
37
|
"#{prefix}#{delimiter}#{uuid_base32}"
|
38
38
|
end
|
39
|
+
|
40
|
+
def decode(token)
|
41
|
+
_, uuid_base32 = split(token)
|
42
|
+
Base32UUID.decode(uuid_base32)
|
43
|
+
end
|
39
44
|
end
|
40
45
|
end
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: based_uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Chmolowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activerecord
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '7.0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '7.0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: activesupport
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|