prefixed_ids 1.2.1 → 1.3.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 +39 -4
- data/lib/prefixed_ids/prefix_id.rb +2 -2
- data/lib/prefixed_ids/version.rb +1 -1
- data/lib/prefixed_ids.rb +11 -0
- metadata +4 -5
- data/lib/tasks/prefixed_ids_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e306f564334470e61f4181b498ace781d12d7d06d1763dfa72a1b8a5f464ecaf
|
4
|
+
data.tar.gz: 79118c8868ffd9244eccc1929aef505c73d79d85df8de9e1a3a07b07225e4c1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66299e3e87f3495ddc7cb973da3cfa60104a6b1d0418f1a9c66e5ae9a2c0311689fa40aa0376e239ac8489a034a2b9fcb503e5aa11def29d818660615fbed958
|
7
|
+
data.tar.gz: 13f65f23972ca2a38038f00007802ce4d65b163e15b990b15d4b1fdb8f9421d032167722bf181810cfaa281527b321cd4fec0317855f68ca8726aad8461e3244
|
data/README.md
CHANGED
@@ -34,7 +34,23 @@ end
|
|
34
34
|
|
35
35
|
This will generate a value like `user_1234abcd`.
|
36
36
|
|
37
|
-
|
37
|
+
##### Prefix ID Param
|
38
|
+
|
39
|
+
To retrieve the prefix ID, simply call:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
User.to_param
|
43
|
+
```
|
44
|
+
|
45
|
+
If `to_param` override is disabled:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
User.prefix_id
|
49
|
+
```
|
50
|
+
|
51
|
+
##### Query by Prefixed ID
|
52
|
+
|
53
|
+
To query using the prefixed ID, you can use either `find` or `find_by_prefix_id`:
|
38
54
|
|
39
55
|
```ruby
|
40
56
|
User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
|
@@ -51,7 +67,26 @@ class User < ApplicationRecord
|
|
51
67
|
end
|
52
68
|
```
|
53
69
|
|
54
|
-
|
70
|
+
##### Salt
|
71
|
+
|
72
|
+
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.
|
73
|
+
|
74
|
+
###### Global Salt
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# config/initializers/prefixed_ids.rb
|
78
|
+
PrefixID.salt = "salt"
|
79
|
+
```
|
80
|
+
|
81
|
+
###### Per Model Salt
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class User
|
85
|
+
has_prefix_id :user, salt: "usersalt"
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
### Generic Lookup By Prefix ID
|
55
90
|
|
56
91
|
Imagine you have a prefixed ID but you don't know which model it belongs to.
|
57
92
|
|
@@ -62,13 +97,13 @@ PrefixedIds.find("acct_2iAnOP0xGDYk6dpe")
|
|
62
97
|
#=> #<Account>
|
63
98
|
```
|
64
99
|
|
65
|
-
### Customizing
|
100
|
+
### Customizing Prefix IDs
|
66
101
|
|
67
102
|
You can customize the prefix, length, and attribute name for PrefixedIds.
|
68
103
|
|
69
104
|
```ruby
|
70
105
|
class Account < ApplicationRecord
|
71
|
-
has_prefix_id :acct, minimum_length: 32, override_find: false, to_param: false
|
106
|
+
has_prefix_id :acct, minimum_length: 32, override_find: false, to_param: false, salt: ""
|
72
107
|
end
|
73
108
|
```
|
74
109
|
|
@@ -4,10 +4,10 @@ module PrefixedIds
|
|
4
4
|
|
5
5
|
TOKEN = 123
|
6
6
|
|
7
|
-
def initialize(model, prefix, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options)
|
7
|
+
def initialize(model, prefix, salt: PrefixedIds.salt, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options)
|
8
8
|
@prefix = prefix.to_s
|
9
9
|
@delimiter = delimiter.to_s
|
10
|
-
@hashids = Hashids.new(model.table_name, minimum_length, alphabet)
|
10
|
+
@hashids = Hashids.new("#{model.table_name}#{salt}", minimum_length, alphabet)
|
11
11
|
end
|
12
12
|
|
13
13
|
def encode(id)
|
data/lib/prefixed_ids/version.rb
CHANGED
data/lib/prefixed_ids.rb
CHANGED
@@ -11,6 +11,7 @@ module PrefixedIds
|
|
11
11
|
mattr_accessor :delimiter, default: "_"
|
12
12
|
mattr_accessor :alphabet, default: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
13
13
|
mattr_accessor :minimum_length, default: 24
|
14
|
+
mattr_accessor :salt, default: ""
|
14
15
|
|
15
16
|
mattr_accessor :models, default: {}
|
16
17
|
|
@@ -74,6 +75,16 @@ module PrefixedIds
|
|
74
75
|
def find(*ids)
|
75
76
|
super(*ids.map { |id| _prefix_id.decode(id, fallback: true) })
|
76
77
|
end
|
78
|
+
|
79
|
+
def relation
|
80
|
+
super.tap { |r| r.extend ClassMethods }
|
81
|
+
end
|
82
|
+
|
83
|
+
def has_many(*args, &block)
|
84
|
+
options = args.extract_options!
|
85
|
+
options[:extend] = Array(options[:extend]).push(ClassMethods)
|
86
|
+
super(*args, **options, &block)
|
87
|
+
end
|
77
88
|
end
|
78
89
|
end
|
79
90
|
|
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.3.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: 2022-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -45,7 +45,7 @@ dependencies:
|
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: 2.0.0
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
48
|
+
name: standard
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - ">="
|
@@ -86,7 +86,6 @@ files:
|
|
86
86
|
- lib/prefixed_ids/engine.rb
|
87
87
|
- lib/prefixed_ids/prefix_id.rb
|
88
88
|
- lib/prefixed_ids/version.rb
|
89
|
-
- lib/tasks/prefixed_ids_tasks.rake
|
90
89
|
homepage: https://github.com/excid3/prefixed_ids
|
91
90
|
licenses:
|
92
91
|
- MIT
|
@@ -109,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
108
|
- !ruby/object:Gem::Version
|
110
109
|
version: '0'
|
111
110
|
requirements: []
|
112
|
-
rubygems_version: 3.
|
111
|
+
rubygems_version: 3.3.7
|
113
112
|
signing_key:
|
114
113
|
specification_version: 4
|
115
114
|
summary: Prefixed IDs generates IDs with friendly prefixes for your models
|