cool_id 0.1.3 → 0.1.5
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 +74 -16
- data/lib/cool_id/version.rb +1 -1
- data/lib/cool_id.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5284982eed15e82c55f1e8a1c853c83feb97d76d7e04fb247b74e6edcbfdcf0e
|
4
|
+
data.tar.gz: 21f44586d37a4a27474dcc984179cc6b8e927ae43da371e5d80874c82a314d43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f8a012d217a3435e87f9df231fce5e850e92cc7ded853fff79cca4f721058f0175912aab2a25c8671defb208453e137884c59a9ec8041a8d1d2aad2f032cb74
|
7
|
+
data.tar.gz: ad7af3d92a6f4b5a067fe535020d9cdd44d53ddd6584ba89555a74bcc83a4cc6a959f7e4cfb857c1961d85dbfe55bd5d0395afe1aed65680d2ae3dab34ab5688
|
data/README.md
CHANGED
@@ -1,31 +1,89 @@
|
|
1
|
-
#
|
1
|
+
# cool_id
|
2
2
|
|
3
|
-
|
3
|
+
a gem for rails that generates string ids for active record models with a per-model prefix followed by a nanoid.
|
4
4
|
|
5
|
-
|
5
|
+
```ruby
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
include CoolId::Model
|
8
|
+
cool_id prefix: "usr"
|
9
|
+
end
|
6
10
|
|
7
|
-
|
11
|
+
User.create!(name: "...").id
|
12
|
+
# => "usr_vktd1b5v84lr"
|
8
13
|
|
9
|
-
|
14
|
+
class Customer < ActiveRecord::Base
|
15
|
+
include CoolId::Model
|
16
|
+
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
|
17
|
+
end
|
10
18
|
|
11
|
-
|
19
|
+
Customer.create!(name: "...").id
|
20
|
+
# => "cus_UHNYBINU"
|
21
|
+
```
|
12
22
|
|
13
|
-
|
23
|
+
it can also lookup records by ids, similar to global id:
|
14
24
|
|
15
|
-
|
25
|
+
```ruby
|
26
|
+
user = User.create!(name: "John Doe")
|
27
|
+
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
|
16
28
|
|
17
|
-
|
29
|
+
CoolId.locate("usr_vktd1b5v84lr")
|
30
|
+
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
|
18
31
|
|
19
|
-
|
32
|
+
# You can also parse the id without fetching the record
|
33
|
+
parsed = CoolId.parse("usr_vktd1b5v84lr")
|
34
|
+
# => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>
|
20
35
|
|
21
|
-
|
36
|
+
parsed.model_class
|
37
|
+
# => User
|
38
|
+
```
|
22
39
|
|
23
|
-
##
|
40
|
+
## installation
|
24
41
|
|
25
|
-
|
42
|
+
```bash
|
43
|
+
bundle add cool_id
|
44
|
+
```
|
26
45
|
|
27
|
-
|
46
|
+
```ruby
|
47
|
+
gem "cool_id"
|
48
|
+
```
|
28
49
|
|
29
|
-
|
50
|
+
### per-model
|
30
51
|
|
31
|
-
|
52
|
+
use string ids when creating a table
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
create_table :users, id: :string do |t|
|
56
|
+
t.string :name
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
include the `CoolId::Model` concern in the active record model and set up a prefix
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class User < ActiveRecord::Base
|
64
|
+
include CoolId::Model
|
65
|
+
cool_id prefix: "usr"
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
### all models
|
70
|
+
|
71
|
+
use string ids on all new generated migrations
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# config/initializers/generators.rb
|
75
|
+
Rails.application.config.generators do |g|
|
76
|
+
g.orm :active_record, primary_key_type: :string
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
# app/models/application_record.rb
|
84
|
+
class ApplicationRecord < ActiveRecord::Base
|
85
|
+
include CoolId::Model
|
86
|
+
primary_abstract_class
|
87
|
+
enforce_cool_id_for_descendants
|
88
|
+
end
|
89
|
+
```
|
data/lib/cool_id/version.rb
CHANGED
data/lib/cool_id.rb
CHANGED
@@ -106,11 +106,11 @@ module CoolId
|
|
106
106
|
CoolId.generate_id(@cool_id_config)
|
107
107
|
end
|
108
108
|
|
109
|
-
def
|
109
|
+
def enforce_cool_id_for_descendants
|
110
110
|
@cool_id_setup_required = true
|
111
111
|
end
|
112
112
|
|
113
|
-
def
|
113
|
+
def skip_enforce_cool_id_for_descendants
|
114
114
|
@cool_id_setup_required = false
|
115
115
|
end
|
116
116
|
|
@@ -134,7 +134,7 @@ module CoolId
|
|
134
134
|
|
135
135
|
def ensure_cool_id_configured
|
136
136
|
if self.class.cool_id_setup_required && self.class.cool_id_config.nil?
|
137
|
-
raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or '
|
137
|
+
raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_enforce_cool_id_for_descendants' to opt out."
|
138
138
|
end
|
139
139
|
end
|
140
140
|
end
|