cool_id 0.1.3 → 0.1.6
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/LICENSE +15 -0
- data/README.md +117 -16
- data/lib/cool_id/version.rb +1 -1
- data/lib/cool_id.rb +3 -3
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d44c3207248595af232a200cf0e809224d74033d97d3cac52c6d06d5416b682
|
4
|
+
data.tar.gz: d221baeb8e79ea8ea8ca2b9503685e4bfa7cb8d57b53a3ba0e4d301138a08300
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ed700560671d811b0678c4fbe8f9e086c3ca0c9e8d04a54b7d40d586b20864e2add65adb9e5e53feb7915e472974dbfa6b31313dd6a64f4008aaf3f31503f70
|
7
|
+
data.tar.gz: df1cdc3098d9b4761a220fedeca362eeaeb5677d92ca7da594dc946223d814c81bb59db757ff08292fe72e3ce499dd95e1cc002ccf27c436aa097617bd14f2a9
|
data/LICENSE
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ISC License
|
2
|
+
|
3
|
+
Copyright (c) Peter Schilling
|
4
|
+
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
data/README.md
CHANGED
@@ -1,31 +1,132 @@
|
|
1
|
-
#
|
1
|
+
# cool_id
|
2
2
|
|
3
|
-
|
3
|
+
a gem for rails that generates string primary key ids for active record models with a per-model prefix followed by a nanoid. this lets you create stripe style ids in your own rails app, and they'll be the same ids you see in your database.
|
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"
|
13
|
+
```
|
8
14
|
|
9
|
-
|
15
|
+
it can also lookup records similar to global id:
|
10
16
|
|
11
|
-
|
17
|
+
```ruby
|
18
|
+
CoolId.locate("usr_vktd1b5v84lr")
|
19
|
+
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
|
20
|
+
```
|
12
21
|
|
13
|
-
|
22
|
+
and parse ids
|
14
23
|
|
15
|
-
|
24
|
+
```ruby
|
25
|
+
parsed = CoolId.parse("usr_vktd1b5v84lr")
|
26
|
+
# => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>
|
16
27
|
|
17
|
-
|
28
|
+
parsed.model_class
|
29
|
+
# => User
|
30
|
+
```
|
18
31
|
|
19
|
-
|
32
|
+
and generate ids without creating a record
|
20
33
|
|
21
|
-
|
34
|
+
```ruby
|
35
|
+
# generate an id, e.g. for batch inserts or upserts
|
36
|
+
User.generate_cool_id
|
37
|
+
# => "usr_vktd1b5v84lr"
|
22
38
|
|
23
|
-
|
39
|
+
```
|
24
40
|
|
25
|
-
|
41
|
+
it takes parameters to change the alphabet or length
|
26
42
|
|
27
|
-
|
43
|
+
```ruby
|
44
|
+
class Customer < ActiveRecord::Base
|
45
|
+
include CoolId::Model
|
46
|
+
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
|
47
|
+
end
|
28
48
|
|
29
|
-
|
49
|
+
Customer.create!(name: "...").id
|
50
|
+
# => "cus_UHNYBINU"
|
51
|
+
```
|
30
52
|
|
31
|
-
|
53
|
+
and these can be configured globally
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
CoolId.configure do |config|
|
57
|
+
config.separator = "-"
|
58
|
+
config.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
59
|
+
config.length = 8
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
## installation
|
64
|
+
|
65
|
+
add cool_id to your Gemfile:
|
66
|
+
|
67
|
+
```bash
|
68
|
+
bundle add cool_id
|
69
|
+
```
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
gem "cool_id"
|
73
|
+
```
|
74
|
+
|
75
|
+
### using cool_id in one model
|
76
|
+
|
77
|
+
use string ids when creating a table
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
create_table :users, id: :string do |t|
|
81
|
+
t.string :name
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
include the `CoolId::Model` concern in the active record model and set up a prefix
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class User < ActiveRecord::Base
|
89
|
+
include CoolId::Model
|
90
|
+
cool_id prefix: "usr"
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
### using cool_id on all models
|
95
|
+
|
96
|
+
you have drank the coolaid. setup rails to use string ids on all new generated migrations
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
# config/initializers/generators.rb
|
100
|
+
Rails.application.config.generators do |g|
|
101
|
+
g.orm :active_record, primary_key_type: :string
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
then setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
# app/models/application_record.rb
|
109
|
+
class ApplicationRecord < ActiveRecord::Base
|
110
|
+
include CoolId::Model
|
111
|
+
primary_abstract_class
|
112
|
+
enforce_cool_id_for_descendants
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
### graphql
|
117
|
+
|
118
|
+
if you use the graphql ruby node interface, you can implement [object identification](https://graphql-ruby.org/schema/object_identification)
|
119
|
+
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# app/graphql/app_schema.rb
|
123
|
+
class AppSchema < GraphQL::Schema
|
124
|
+
def self.id_from_object(object, type_definition, query_ctx)
|
125
|
+
object.id
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.object_from_id(id, query_ctx)
|
129
|
+
CoolId.locate(id)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
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
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cool_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Schilling
|
@@ -76,13 +76,15 @@ files:
|
|
76
76
|
- ".rspec"
|
77
77
|
- ".standard.yml"
|
78
78
|
- CHANGELOG.md
|
79
|
+
- LICENSE
|
79
80
|
- README.md
|
80
81
|
- Rakefile
|
81
82
|
- lib/cool_id.rb
|
82
83
|
- lib/cool_id/version.rb
|
83
84
|
- sig/cool_id.rbs
|
84
85
|
homepage: https://github.com/schpet/cool_id
|
85
|
-
licenses:
|
86
|
+
licenses:
|
87
|
+
- ISC
|
86
88
|
metadata:
|
87
89
|
homepage_uri: https://github.com/schpet/cool_id
|
88
90
|
source_code_uri: https://github.com/schpet/cool_id
|