prefixed_ids 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -3
- data/lib/prefixed_ids/version.rb +1 -1
- data/lib/prefixed_ids.rb +8 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5a5f802c0838ffb2538b43212061d569d0920356aed91e313865799c570d573
|
4
|
+
data.tar.gz: 6488ec773ddb3f15dfb48a9bc07ce4d39262feabb89eb8d3a5168e5ffdf52017
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b62b64c158bd670e814fbe92b7306a046b9f4e7933f494e5312ddb44f7a068fe9baaa579a11ad45b635aed0ec1dc83bec5b1699471ad550ed5e846a9125e446
|
7
|
+
data.tar.gz: a5233cee0e3fb432b1ff4e01bc60f6c33cc670df756bb09e315f26fd5d22c6b7325bc2fde0eb1369be21196d4b23e65850dadf10f0fc2ee927aa636b8067670b
|
data/README.md
CHANGED
@@ -13,6 +13,8 @@ user_12345abcd
|
|
13
13
|
acct_23lksjdg3
|
14
14
|
```
|
15
15
|
|
16
|
+
This gem works by hashing the record's original `:id` attribute using [`Hashids`](https://hashids.org/ruby/), 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`.
|
17
|
+
|
16
18
|
Inspired by [Stripe's prefixed IDs](https://stripe.com/docs/api) in their API.
|
17
19
|
|
18
20
|
## 🚀 Installation
|
@@ -50,13 +52,16 @@ User.prefix_id
|
|
50
52
|
|
51
53
|
##### Query by Prefixed ID
|
52
54
|
|
53
|
-
To query using the prefixed ID, you can use either `find` or `find_by_prefix_id
|
55
|
+
To query using the prefixed ID, you can use either `find`, `find_by_prefix_id`, or `find_by_prefix_id!`:
|
54
56
|
|
55
57
|
```ruby
|
56
58
|
User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
|
57
59
|
User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
|
58
60
|
```
|
59
61
|
|
62
|
+
⚠️ Note that `find` still finds records by the primary key. Eg. `localhost/users/1` still works.
|
63
|
+
If you're targeting security issues by masking the ID, make sure to use `find_by_prefix_id` and [add a salt](#salt).
|
64
|
+
|
60
65
|
We also override `to_param` by default so it'll be used in URLs automatically.
|
61
66
|
|
62
67
|
To disable find and to_param overrides, simply pass in the options:
|
@@ -75,7 +80,7 @@ A salt is a secret value that makes it impossible to reverse engineer IDs. We re
|
|
75
80
|
|
76
81
|
```ruby
|
77
82
|
# config/initializers/prefixed_ids.rb
|
78
|
-
|
83
|
+
PrefixedIds.salt = "salt"
|
79
84
|
```
|
80
85
|
|
81
86
|
###### Per Model Salt
|
@@ -103,7 +108,7 @@ You can customize the prefix, length, and attribute name for PrefixedIds.
|
|
103
108
|
|
104
109
|
```ruby
|
105
110
|
class Account < ApplicationRecord
|
106
|
-
has_prefix_id :acct, minimum_length: 32, override_find: false,
|
111
|
+
has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: ""
|
107
112
|
end
|
108
113
|
```
|
109
114
|
|
data/lib/prefixed_ids/version.rb
CHANGED
data/lib/prefixed_ids.rb
CHANGED
@@ -61,6 +61,14 @@ module PrefixedIds
|
|
61
61
|
def find_by_prefix_id!(id)
|
62
62
|
find_by!(id: _prefix_id.decode(id))
|
63
63
|
end
|
64
|
+
|
65
|
+
def decode_prefix_id(id)
|
66
|
+
_prefix_id.decode(id)
|
67
|
+
end
|
68
|
+
|
69
|
+
def decode_prefix_ids(ids)
|
70
|
+
ids.map { |id| decode_prefix_id(id) }
|
71
|
+
end
|
64
72
|
end
|
65
73
|
|
66
74
|
def prefix_id
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prefixed_ids
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
|
-
rubygems_version: 3.
|
111
|
+
rubygems_version: 3.4.7
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Prefixed IDs generates IDs with friendly prefixes for your models
|