public_uid 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -0
- data/lib/public_uid.rb +5 -0
- data/lib/public_uid/model_concern.rb +29 -0
- data/lib/public_uid/version.rb +1 -1
- data/public_uid.gemspec +2 -0
- data/test/models/model_with_public_uid_concern_test.rb +49 -0
- data/test/support/orm/active_record.rb +6 -0
- metadata +33 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2feee63566bac287b2b37df241fe9f6d921815a25fa9d8ffda0ec2e51e79da88
|
4
|
+
data.tar.gz: '06879a851089f031f9729d7b9651e32ce5340ab2096db896c9001547b26be866'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 592b580707bcb87cee3c82405b838011524d4197573f7ba3982c7b8cd3982fbd9605123e5b24a05f1c0b11be1bd54083fba0eaec27c47c157c361fb808ff1b90
|
7
|
+
data.tar.gz: c4b47726fd86694bd4213e72749841ba398dd408ed2d239fb749108df89ad93b4601a02eca1eac765b1eb0e17e16d79b9e69146ac552fecde9f237f89f667043
|
data/README.md
CHANGED
@@ -48,6 +48,9 @@ And then execute:
|
|
48
48
|
|
49
49
|
## Usage
|
50
50
|
|
51
|
+
|
52
|
+
### Step 1 - db column
|
53
|
+
|
51
54
|
Create database column for public unique ID.
|
52
55
|
|
53
56
|
```ruby
|
@@ -59,6 +62,29 @@ class AddPublicUidToUsers < ActiveRecord::Migration
|
|
59
62
|
end
|
60
63
|
```
|
61
64
|
|
65
|
+
### Step 2a - Using Rails concern
|
66
|
+
|
67
|
+
> a.k.a - the easy way for Rails
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class User < ActiveRecord::Base
|
71
|
+
include PublicUid::ModelConcern
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
This will automatically:
|
76
|
+
|
77
|
+
* assumes your model has `public_uid` column and generate public_uid value for you
|
78
|
+
* will automatically tell model to use `public_uid` as `to_param` method ffectively turning `user.public_uid` the attribute passed in routes when you do routes (instead of `id`). Example `user_path(@user)` => `/users/xxxxxxx`
|
79
|
+
* provides `User.find_puid('xxxxxx')` and `User.find_puid!('xxxxxx')` class methods as more convenient replacement for `find_by!(public_uid: 'xxxxxxx')` to find records in controllers.
|
80
|
+
* `User.find_puid!('xxxxxx')` will raise `PublicUid::RecordNotFound` instead of `ActiveRecord::RecordNotFound` for more accurate error handling in Rails controller (check [Rails rescue_from](https://apidock.com/rails/ActiveSupport/Rescuable/ClassMethods/rescue_from) for inspiration)
|
81
|
+
|
82
|
+
> more info check > [source](https://github.com/equivalent/public_uid/blob/master/lib/public_uid/model_concern.rb)
|
83
|
+
|
84
|
+
If you need more customization please follow **Step 2b** instead
|
85
|
+
|
86
|
+
### Step 2b - Using manual generate method
|
87
|
+
|
62
88
|
Tell your model to generate the public identifier
|
63
89
|
|
64
90
|
```ruby
|
data/lib/public_uid.rb
CHANGED
@@ -6,6 +6,11 @@ require "public_uid/generators/number_random"
|
|
6
6
|
require "public_uid/generators/range_string"
|
7
7
|
require "public_uid/generators/number_secure_random"
|
8
8
|
require "public_uid/generators/hex_string_secure_random"
|
9
|
+
require 'public_uid/model_concern'
|
9
10
|
require 'public_uid/tasks' if defined?(Rails)
|
10
11
|
|
12
|
+
module PublicUid
|
13
|
+
RecordNotFound = Class.new(StandardError)
|
14
|
+
end
|
15
|
+
|
11
16
|
ActiveRecord::Base.send(:include, PublicUid::Model)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module PublicUid
|
2
|
+
module ModelConcern
|
3
|
+
RecordNotFound = Class.new(StandardError)
|
4
|
+
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
generate_public_uid
|
9
|
+
end
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
def find_puid!(public_uid)
|
13
|
+
find_by!(public_uid: public_uid)
|
14
|
+
rescue ActiveRecord::RecordNotFound
|
15
|
+
raise PublicUid::RecordNotFound, "#{self.name} '#{public_uid}' not found"
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_puid(public_uid)
|
19
|
+
find_puid!(public_uid)
|
20
|
+
rescue PublicUid::RecordNotFound
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_param
|
26
|
+
public_uid
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/public_uid/version.rb
CHANGED
data/public_uid.gemspec
CHANGED
@@ -20,8 +20,10 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_dependency "activerecord", '> 4.2' # ensures compatibility for ruby 2.0.0+ to head
|
22
22
|
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "pry"
|
23
24
|
spec.add_development_dependency "rake"
|
24
25
|
spec.add_development_dependency "minitest", "~> 5"
|
25
26
|
spec.add_development_dependency "rr", "~> 1.1.2"
|
26
27
|
spec.add_development_dependency "sqlite3", "~> 1.4.1"
|
28
|
+
spec.add_development_dependency "activesupport", '> 4.2'
|
27
29
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
TestConf.orm_modules.each do |orm_module|
|
4
|
+
describe orm_module.description do
|
5
|
+
context 'Model with default generator' do
|
6
|
+
let(:model_class) { "#{orm_module}::ModelWithPublicUidConcern".constantize }
|
7
|
+
let(:user) { model_class.new }
|
8
|
+
|
9
|
+
describe '#public_uid' do
|
10
|
+
subject{ user.public_uid }
|
11
|
+
|
12
|
+
context 'in new record' do
|
13
|
+
it{ expect(subject).must_be_nil }
|
14
|
+
|
15
|
+
describe '#generate_uid' do
|
16
|
+
before do
|
17
|
+
user.generate_uid
|
18
|
+
end
|
19
|
+
|
20
|
+
it { expect(subject).wont_be_nil }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'after save' do
|
25
|
+
|
26
|
+
before do
|
27
|
+
user.save
|
28
|
+
user.reload
|
29
|
+
end
|
30
|
+
|
31
|
+
it{ expect(subject).must_be_kind_of(String) }
|
32
|
+
it{ expect(subject.length).must_equal(8) }
|
33
|
+
|
34
|
+
it{ expect(user.to_param).must_equal(subject) }
|
35
|
+
it{ expect(model_class.find_puid(subject)).must_equal(user) }
|
36
|
+
it{ expect(model_class.find_puid!(subject)).must_equal(user) }
|
37
|
+
|
38
|
+
|
39
|
+
it{ expect(model_class.find_puid('nonexisting')).must_be_nil }
|
40
|
+
it do
|
41
|
+
assert_raises PublicUid::RecordNotFound do
|
42
|
+
model_class.find_puid!('nonexisting')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -29,6 +29,12 @@ module ActRec
|
|
29
29
|
generate_public_uid
|
30
30
|
end
|
31
31
|
|
32
|
+
class ModelWithPublicUidConcern < ActiveRecord::Base
|
33
|
+
include PublicUid::ModelConcern
|
34
|
+
self.table_name = 'user_puids'
|
35
|
+
generate_public_uid
|
36
|
+
end
|
37
|
+
|
32
38
|
class ModelWithoutGenaratePublicUid < ActiveRecord::Base
|
33
39
|
self.table_name = 'users'
|
34
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: public_uid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Valent
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +108,20 @@ dependencies:
|
|
94
108
|
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
110
|
version: 1.4.1
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activesupport
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.2'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.2'
|
97
125
|
description: Automatic generates public unique identifier for model
|
98
126
|
email:
|
99
127
|
- equivalent@eq8.eu
|
@@ -113,6 +141,7 @@ files:
|
|
113
141
|
- lib/public_uid/generators/number_secure_random.rb
|
114
142
|
- lib/public_uid/generators/range_string.rb
|
115
143
|
- lib/public_uid/model.rb
|
144
|
+
- lib/public_uid/model_concern.rb
|
116
145
|
- lib/public_uid/set_public_uid.rb
|
117
146
|
- lib/public_uid/tasks.rb
|
118
147
|
- lib/public_uid/tasks/generate.rake
|
@@ -128,6 +157,7 @@ files:
|
|
128
157
|
- test/models/instance_methods_initialization_test.rb
|
129
158
|
- test/models/model_with_custom_generator_test.rb
|
130
159
|
- test/models/model_with_generator_defaults_test.rb
|
160
|
+
- test/models/model_with_public_uid_concern_test.rb
|
131
161
|
- test/support/orm/active_record.rb
|
132
162
|
- test/support/test_conf.rb
|
133
163
|
- test/test_helper.rb
|
@@ -165,6 +195,7 @@ test_files:
|
|
165
195
|
- test/models/instance_methods_initialization_test.rb
|
166
196
|
- test/models/model_with_custom_generator_test.rb
|
167
197
|
- test/models/model_with_generator_defaults_test.rb
|
198
|
+
- test/models/model_with_public_uid_concern_test.rb
|
168
199
|
- test/support/orm/active_record.rb
|
169
200
|
- test/support/test_conf.rb
|
170
201
|
- test/test_helper.rb
|