public_uid 1.2.2 → 2.1.1
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 +5 -5
- data/.travis.yml +5 -6
- data/README.md +119 -2
- data/lib/public_uid.rb +9 -3
- data/lib/public_uid/generators/hex_string_secure_random.rb +20 -0
- data/lib/public_uid/generators/number_secure_random.rb +16 -0
- data/lib/public_uid/model.rb +1 -1
- data/lib/public_uid/model_concern.rb +29 -0
- data/lib/public_uid/set_public_uid.rb +1 -1
- data/lib/public_uid/tasks/generate.rake +2 -1
- data/lib/public_uid/version.rb +1 -1
- data/public_uid.gemspec +5 -4
- data/test/lib/generators/hex_string_secure_random_test.rb +34 -0
- data/test/lib/generators/number_random_test.rb +6 -6
- data/test/lib/generators/number_secure_random_test.rb +40 -0
- data/test/lib/generators/range_string_test.rb +5 -5
- data/test/lib/set_public_uid_test.rb +19 -7
- data/test/lib/tasks/generate_test.rb +5 -8
- data/test/models/custom_public_uid_column_test.rb +4 -4
- data/test/models/instance_methods_initialization_test.rb +2 -2
- data/test/models/model_with_custom_generator_test.rb +5 -5
- data/test/models/model_with_generator_defaults_test.rb +4 -4
- data/test/models/model_with_public_uid_concern_test.rb +49 -0
- data/test/support/orm/active_record.rb +6 -0
- metadata +42 -21
- data/lib/orm/active_record.rb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 789c1469292aaeac0682a128fa3edcb9a4e2adcb792c94acef5c41fa20a56160
|
4
|
+
data.tar.gz: 9fe04f567ebd19658ea950585fadccf98a42f0414a2d35451b23d7245f301287
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26d3176ede91d962cf2a3debfcbf967c50e20cff478ea67176d6ea4f60ebbdf2a086c4941b823a49717fbf724d342d725291a92979bcd35bf987db21845d80ce
|
7
|
+
data.tar.gz: f55e66e2fb2a868e28c6b2bf209a39d13a6cb68635c5af308623e3f0939442b3df9c85e581e5c3667e932e8de056271b0d820907fecb657c2c8ccee654252242
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -48,16 +48,67 @@ 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
|
54
57
|
class AddPublicUidToUsers < ActiveRecord::Migration
|
55
58
|
def change
|
56
59
|
add_column :users, :public_uid, :string
|
60
|
+
add_index :users, :public_uid
|
57
61
|
end
|
58
62
|
end
|
59
63
|
```
|
60
64
|
|
65
|
+
### Step 2a - Using Rails concern
|
66
|
+
|
67
|
+
> a.k.a - the easy way for Rails (availible since version 2.1.0)
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class User < ActiveRecord::Base
|
71
|
+
include PublicUid::ModelConcern
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
Example:
|
76
|
+
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
user = User.create
|
80
|
+
user.public_uid
|
81
|
+
# => "xxxxxxxx"
|
82
|
+
|
83
|
+
user.to_param
|
84
|
+
# => "xxxxxxxx"
|
85
|
+
|
86
|
+
User.find_puid('xxxxxxxx')
|
87
|
+
# => <#User ...>
|
88
|
+
User.find_puid('not_existing')
|
89
|
+
# => nil
|
90
|
+
|
91
|
+
User.find_puid!('xxxxxxxx')
|
92
|
+
# => <#User ...>
|
93
|
+
User.find_puid!('not_existing')
|
94
|
+
# PublicUid::RecordNotFound (User 'not_existing' not found)
|
95
|
+
```
|
96
|
+
|
97
|
+
This will automatically:
|
98
|
+
|
99
|
+
* assumes your model has `public_uid` column and generate public_uid value for you
|
100
|
+
* 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`
|
101
|
+
* 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.
|
102
|
+
* `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)
|
103
|
+
|
104
|
+
> more info check > [source](https://github.com/equivalent/public_uid/blob/master/lib/public_uid/model_concern.rb)
|
105
|
+
|
106
|
+
If you need more customization please follow **Step 2b** instead
|
107
|
+
|
108
|
+
### Step 2b - Using manual generate method
|
109
|
+
|
110
|
+
> a.k.a bit harder than `2.a` but more flexible way for Rails
|
111
|
+
|
61
112
|
Tell your model to generate the public identifier
|
62
113
|
|
63
114
|
```ruby
|
@@ -84,7 +135,7 @@ Then you can do more clever things like having urls based on `public_uid` and `t
|
|
84
135
|
class User < ActiveRecord::Base
|
85
136
|
generate_public_uid
|
86
137
|
|
87
|
-
def self.
|
138
|
+
def self.find_puid(param)
|
88
139
|
find_by! public_uid: param.split('-').first
|
89
140
|
end
|
90
141
|
|
@@ -97,7 +148,7 @@ end
|
|
97
148
|
class UsersController < ActionController::Base
|
98
149
|
# ...
|
99
150
|
def show
|
100
|
-
@user = User.
|
151
|
+
@user = User.find_puid(param[:id])
|
101
152
|
# ...
|
102
153
|
end
|
103
154
|
# ...
|
@@ -141,6 +192,36 @@ generator wont work if your public uniq ID column is a String (as the
|
|
141
192
|
gem would try to set Integer on a String). If you really want a number
|
142
193
|
like string you can specify number range for Range String Generator
|
143
194
|
|
195
|
+
If you want to generate random Integer using SecureRandom ruby library you can use built-in number secure generator:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
class User < ActiveRecord::Base
|
199
|
+
generate_public_uid generator: PublicUid::Generators::NumberSecureRandom.new
|
200
|
+
end
|
201
|
+
```
|
202
|
+
|
203
|
+
```irb
|
204
|
+
u = User.new
|
205
|
+
u.public_uid #=> nil
|
206
|
+
u.save! #=> true
|
207
|
+
u.public_uid #=> 4567123
|
208
|
+
```
|
209
|
+
|
210
|
+
If you want to generate random Hexadecimal String using SecureRandom ruby library you can use built-in hexadecimal string secure generator:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
class User < ActiveRecord::Base
|
214
|
+
generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new
|
215
|
+
end
|
216
|
+
```
|
217
|
+
|
218
|
+
```irb
|
219
|
+
u = User.new
|
220
|
+
u.public_uid #=> nil
|
221
|
+
u.save! #=> true
|
222
|
+
u.public_uid #=> 0b30ffbc7de3b362
|
223
|
+
```
|
224
|
+
|
144
225
|
### Customizing generated string
|
145
226
|
|
146
227
|
```ruby
|
@@ -150,6 +231,14 @@ class User < ActiveRecord::Base
|
|
150
231
|
end
|
151
232
|
```
|
152
233
|
|
234
|
+
or in case you are using SecureRandom ruby library:
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
class User < ActiveRecord::Base
|
238
|
+
generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new(4) #4 is length of hexadecimal string. If this argument is not set, length of hexadecimal string will be 8 characters.
|
239
|
+
end
|
240
|
+
```
|
241
|
+
|
153
242
|
```irb
|
154
243
|
u = User.new
|
155
244
|
u.public_uid #=> nil
|
@@ -177,6 +266,14 @@ class User < ActiveRecord::Base
|
|
177
266
|
end
|
178
267
|
```
|
179
268
|
|
269
|
+
or in case you are using SecureRandom ruby library:
|
270
|
+
```ruby
|
271
|
+
class User < ActiveRecord::Base
|
272
|
+
UID_RANGE = 1_000..4_000
|
273
|
+
generate_public_uid generator: PublicUid::Generators::NumberSecureRandom.new(UID_RANGE)
|
274
|
+
end
|
275
|
+
```
|
276
|
+
|
180
277
|
```irb
|
181
278
|
u = User.new
|
182
279
|
u.public_uid #=> nil
|
@@ -264,3 +361,23 @@ In future gem version we will actually introduce this two generators.
|
|
264
361
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
265
362
|
4. Push to the branch (`git push origin my-new-feature`)
|
266
363
|
5. Create new Pull Request
|
364
|
+
|
365
|
+
## Changelog
|
366
|
+
|
367
|
+
##### 2019-11-29
|
368
|
+
|
369
|
+
Version 2.0.0 released
|
370
|
+
|
371
|
+
Till version [public_uid 1.3.1](https://github.com/equivalent/public_uid/tree/version-1.3.1) gem used [orm_adapter](https://github.com/ianwhite/orm_adapter) which mean
|
372
|
+
that you could use ActiveRecord or any other data mapping adapter (e.g. Mongoid) supported by orm adapter.
|
373
|
+
|
374
|
+
Problem is that orm_adapter is not maintained for 6 years now and cause some
|
375
|
+
gem conflicts with latest ActiveRecord development environment. That's
|
376
|
+
why I've decided to remove the ORM adapter ([commit](https://github.com/equivalent/public_uid/commit/e66b5dbf659fcdddc6b284b3eb2051a9b8a31968))
|
377
|
+
and use `ActiveRecord::Base` directly.
|
378
|
+
|
379
|
+
That means any Rails application using `public_uid` gem ActiveRecord will **not** be affected by the 2.0 release.
|
380
|
+
If anyone want to see public_uid v 2.0 to support other data mappers (e.g. mongo) please open an issue or create PR with the fix.
|
381
|
+
|
382
|
+
Sorry for any inconvenience
|
383
|
+
|
data/lib/public_uid.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
-
require '
|
1
|
+
require 'active_record'
|
2
2
|
require "public_uid/version"
|
3
3
|
require "public_uid/set_public_uid"
|
4
4
|
require "public_uid/model"
|
5
5
|
require "public_uid/generators/number_random"
|
6
6
|
require "public_uid/generators/range_string"
|
7
|
-
|
7
|
+
require "public_uid/generators/number_secure_random"
|
8
|
+
require "public_uid/generators/hex_string_secure_random"
|
9
|
+
require 'public_uid/model_concern'
|
8
10
|
require 'public_uid/tasks' if defined?(Rails)
|
9
11
|
|
10
|
-
|
12
|
+
module PublicUid
|
13
|
+
RecordNotFound = Class.new(StandardError)
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Base.send(:include, PublicUid::Model)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module PublicUid
|
4
|
+
module Generators
|
5
|
+
class HexStringSecureRandom
|
6
|
+
def initialize(length=8)
|
7
|
+
@length = length
|
8
|
+
end
|
9
|
+
|
10
|
+
def generate
|
11
|
+
if @length.odd?
|
12
|
+
result = SecureRandom.hex( (@length+1)/2 ) #because in "SecureRandom.hex(@length)" @length means length in bytes = 2 hexadecimal characters
|
13
|
+
return result[0...-1]
|
14
|
+
else
|
15
|
+
SecureRandom.hex(@length/2)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module PublicUid
|
4
|
+
module Generators
|
5
|
+
class NumberSecureRandom
|
6
|
+
def initialize(scale = 1_000_000..9_999_999)
|
7
|
+
@scale = scale
|
8
|
+
end
|
9
|
+
|
10
|
+
def generate()
|
11
|
+
generated_number = SecureRandom.random_number( (@scale.max - @scale.min) ) #because SecureRandom.random_number can have only one argument = max value.
|
12
|
+
return (generated_number + @scale.min)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/public_uid/model.rb
CHANGED
@@ -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
|
@@ -3,6 +3,7 @@ require 'rake'
|
|
3
3
|
namespace :public_uid do
|
4
4
|
desc "Generate public_uid on Models that have public_uid column on records that have nil value"
|
5
5
|
task :generate => :environment do
|
6
|
+
Rails.application.eager_load! if defined?(Rails)
|
6
7
|
ActiveRecord::Base.descendants.each do |model|
|
7
8
|
model.connection # establish conection
|
8
9
|
|
@@ -14,7 +15,7 @@ namespace :public_uid do
|
|
14
15
|
.tap { |scope| puts " * generating #{scope.count} #{uid_column_name}(s) for #{model.table_name}" }
|
15
16
|
.find_each do |record_without_public_uid|
|
16
17
|
record_without_public_uid.generate_uid
|
17
|
-
record_without_public_uid.save!
|
18
|
+
record_without_public_uid.save!(validate: false)
|
18
19
|
end
|
19
20
|
puts ''
|
20
21
|
end
|
data/lib/public_uid/version.rb
CHANGED
data/public_uid.gemspec
CHANGED
@@ -18,11 +18,12 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "
|
22
|
-
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_dependency "activerecord", '> 4.2' # ensures compatibility for ruby 2.0.0+ to head
|
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
|
-
spec.add_development_dependency "sqlite3"
|
27
|
-
spec.add_development_dependency "
|
27
|
+
spec.add_development_dependency "sqlite3", "~> 1.4.1"
|
28
|
+
spec.add_development_dependency "activesupport", '> 4.2'
|
28
29
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'HexStringSecureRandom' do
|
4
|
+
|
5
|
+
describe "#generate" do
|
6
|
+
subject{ instance.generate }
|
7
|
+
|
8
|
+
context 'by default' do
|
9
|
+
let(:instance){ PublicUid::Generators::HexStringSecureRandom.new }
|
10
|
+
it 'generates 8 chars hexa string' do
|
11
|
+
expect(subject.length).must_equal 8
|
12
|
+
expect(subject).must_be_kind_of String
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'generates hexadecimal chars' do
|
16
|
+
expect(subject).must_match(/^[a-f0-9]*$/)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when 10 hexa chars' do
|
21
|
+
let(:instance){ PublicUid::Generators::HexStringSecureRandom.new(10) }
|
22
|
+
it 'generates 10 chars string' do
|
23
|
+
expect(subject.length).must_equal 10
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when 11 hexa chars' do
|
28
|
+
let(:instance){ PublicUid::Generators::HexStringSecureRandom.new(11) }
|
29
|
+
it 'generates 11 chars string' do
|
30
|
+
expect(subject.length).must_equal 11
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -8,20 +8,20 @@ describe 'NumberRandom' do
|
|
8
8
|
context 'by default' do
|
9
9
|
let(:instance){ PublicUid::Generators::NumberRandom.new }
|
10
10
|
it 'generates 7 digits' do
|
11
|
-
subject.to_i.to_s.length.must_equal 7
|
12
|
-
subject.must_be_kind_of Integer
|
11
|
+
expect(subject.to_i.to_s.length).must_equal 7
|
12
|
+
expect(subject).must_be_kind_of Integer
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
context 'when specifying number between 20 - 21' do
|
17
17
|
let(:instance){ PublicUid::Generators::NumberRandom.new(20..21) }
|
18
18
|
it 'generates 2 digits' do
|
19
|
-
subject.to_i.to_s.length.must_equal 2
|
20
|
-
subject.must_be_kind_of Integer
|
19
|
+
expect(subject.to_i.to_s.length).must_equal 2
|
20
|
+
expect(subject).must_be_kind_of Integer
|
21
21
|
end
|
22
22
|
|
23
|
-
it 'generates
|
24
|
-
[20, 21].must_include subject
|
23
|
+
it 'generates integer has to be 20 or 21' do
|
24
|
+
expect([20, 21]).must_include subject
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'NumberSecureRandom' do
|
4
|
+
|
5
|
+
describe "#generate" do
|
6
|
+
subject{ instance.generate }
|
7
|
+
|
8
|
+
context 'by default' do
|
9
|
+
let(:instance){ PublicUid::Generators::NumberSecureRandom.new }
|
10
|
+
it 'generates 7 digits' do
|
11
|
+
expect(subject.to_i.to_s.length).must_equal 7
|
12
|
+
expect(subject).must_be_kind_of Integer
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when specifying number between 20 - 21' do
|
17
|
+
let(:instance){ PublicUid::Generators::NumberSecureRandom.new(20..21) }
|
18
|
+
it 'generates 2 digits' do
|
19
|
+
expect(subject.to_i.to_s.length).must_equal 2
|
20
|
+
expect(subject).must_be_kind_of Integer
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'generates integer has to be 20 or 21' do
|
24
|
+
expect([20, 21]).must_include subject
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when specifying number between 1000 - 9999' do
|
29
|
+
let(:instance){ PublicUid::Generators::NumberSecureRandom.new(1000..9999) }
|
30
|
+
it 'generates 4 digits' do
|
31
|
+
expect(subject.to_i.to_s.length).must_equal 4
|
32
|
+
expect(subject).must_be_kind_of Integer
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'generates integer has to be between 1000 and 9999' do
|
36
|
+
expect((1000..9999)).must_include subject
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -8,23 +8,23 @@ describe 'RangeString' do
|
|
8
8
|
context 'by default' do
|
9
9
|
let(:instance){ PublicUid::Generators::RangeString.new }
|
10
10
|
it 'generates 8 chars string' do
|
11
|
-
subject.length.must_equal 8
|
12
|
-
subject.must_be_kind_of String
|
11
|
+
expect(subject.length).must_equal 8
|
12
|
+
expect(subject).must_be_kind_of String
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'generates downcase chars' do
|
16
|
-
subject.must_match(/^[a-z]*$/)
|
16
|
+
expect(subject).must_match(/^[a-z]*$/)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
context 'when 10 chars x-y' do
|
21
21
|
let(:instance){ PublicUid::Generators::RangeString.new(3, ('x'..'z')) }
|
22
22
|
it 'generates 10 chars string' do
|
23
|
-
subject.length.must_equal 3
|
23
|
+
expect(subject.length).must_equal 3
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'generates string containing chars x,y,z' do
|
27
|
-
subject.must_match(/^[x-z]*$/)
|
27
|
+
expect(subject).must_match(/^[x-z]*$/)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -26,12 +26,20 @@ TestConf.orm_modules.each do |orm_module|
|
|
26
26
|
describe 'initialization' do
|
27
27
|
context 'when column not specified' do
|
28
28
|
let(:options) { { record: record } }
|
29
|
-
it
|
29
|
+
it do
|
30
|
+
assert_raises PublicUid::SetPublicUid::NoPublicUidColumnSpecified do
|
31
|
+
instance
|
32
|
+
end
|
33
|
+
end
|
30
34
|
end
|
31
35
|
|
32
36
|
context 'when record not specified' do
|
33
37
|
let(:options) { {column: :foo} }
|
34
|
-
it
|
38
|
+
it do
|
39
|
+
assert_raises PublicUid::SetPublicUid::NoRecordSpecified do
|
40
|
+
instance
|
41
|
+
end
|
42
|
+
end
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
@@ -40,7 +48,7 @@ TestConf.orm_modules.each do |orm_module|
|
|
40
48
|
|
41
49
|
it "should ask generator to generate random string" do
|
42
50
|
instance.generate(DummyGenerator.new)
|
43
|
-
subject.must_equal 'first try'
|
51
|
+
expect(subject).must_equal 'first try'
|
44
52
|
end
|
45
53
|
|
46
54
|
context 'when record match random' do
|
@@ -49,7 +57,7 @@ TestConf.orm_modules.each do |orm_module|
|
|
49
57
|
|
50
58
|
it "should generate string once again" do
|
51
59
|
instance.generate(DummyGenerator.new)
|
52
|
-
subject.must_equal 'second try'
|
60
|
+
expect(subject).must_equal 'second try'
|
53
61
|
end
|
54
62
|
end
|
55
63
|
end
|
@@ -58,7 +66,11 @@ TestConf.orm_modules.each do |orm_module|
|
|
58
66
|
subject { instance.new_uid }
|
59
67
|
|
60
68
|
context 'when @new id is not set' do
|
61
|
-
it
|
69
|
+
it do
|
70
|
+
assert_raises PublicUid::SetPublicUid::NewUidNotSetYet do
|
71
|
+
instance.set
|
72
|
+
end
|
73
|
+
end
|
62
74
|
end
|
63
75
|
|
64
76
|
context 'when @new id is set' do
|
@@ -66,7 +78,7 @@ TestConf.orm_modules.each do |orm_module|
|
|
66
78
|
|
67
79
|
it 'must set new_uid for record pubilc_uid column' do
|
68
80
|
instance.set
|
69
|
-
subject.must_equal '123'
|
81
|
+
expect(subject).must_equal '123'
|
70
82
|
end
|
71
83
|
end
|
72
84
|
end
|
@@ -84,7 +96,7 @@ TestConf.orm_modules.each do |orm_module|
|
|
84
96
|
count_mock = stub(record_class).count { 10 }
|
85
97
|
stub(record_class).where( { public_uid: 567 } ) { count_mock }
|
86
98
|
|
87
|
-
trigger.must_equal true
|
99
|
+
expect(trigger).must_equal true
|
88
100
|
end
|
89
101
|
end
|
90
102
|
end
|
@@ -29,23 +29,20 @@ describe 'Generate' do
|
|
29
29
|
initial_uid = user.send(uid_column)
|
30
30
|
|
31
31
|
# simulate an existing record with no uid value
|
32
|
-
user.update_attribute(uid_column, nil).must_equal true
|
33
|
-
user.send(uid_column).must_be_nil
|
32
|
+
expect(user.update_attribute(uid_column, nil)).must_equal true
|
33
|
+
expect(user.send(uid_column)).must_be_nil
|
34
34
|
|
35
35
|
# run the rake task
|
36
36
|
run_generate_task
|
37
37
|
|
38
38
|
user.reload
|
39
|
-
user.send(uid_column).must_be_kind_of String
|
40
|
-
user.send(uid_column).wont_be_empty
|
41
|
-
user.send(uid_column).wont_be_same_as initial_uid
|
39
|
+
expect(user.send(uid_column)).must_be_kind_of String
|
40
|
+
expect(user.send(uid_column)).wont_be_empty
|
41
|
+
expect(user.send(uid_column)).wont_be_same_as initial_uid
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
46
45
|
end
|
47
|
-
|
48
46
|
end
|
49
47
|
end
|
50
|
-
|
51
48
|
end
|
@@ -9,14 +9,14 @@ TestConf.orm_modules.each do |orm_module|
|
|
9
9
|
subject{ user.custom_uid }
|
10
10
|
|
11
11
|
context 'in new record' do
|
12
|
-
it{ subject.must_be_nil }
|
12
|
+
it{ expect(subject).must_be_nil }
|
13
13
|
|
14
14
|
describe '#generate_uid' do
|
15
15
|
before do
|
16
16
|
user.generate_uid
|
17
17
|
end
|
18
18
|
|
19
|
-
it { subject.wont_be_nil }
|
19
|
+
it { expect(subject).wont_be_nil }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -27,8 +27,8 @@ TestConf.orm_modules.each do |orm_module|
|
|
27
27
|
user.reload
|
28
28
|
end
|
29
29
|
|
30
|
-
it{ subject.must_be_kind_of(String) }
|
31
|
-
it{ subject.length.must_equal(8) }
|
30
|
+
it{ expect(subject).must_be_kind_of(String) }
|
31
|
+
it{ expect(subject.length).must_equal(8) }
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -9,7 +9,7 @@ TestConf.orm_modules.each do |orm_module|
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'model should not expose :generate_uid public instance method' do
|
12
|
-
model.instance_methods.wont_include :generate_uid
|
12
|
+
expect(model.instance_methods).wont_include :generate_uid
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -19,7 +19,7 @@ TestConf.orm_modules.each do |orm_module|
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'model should expose :generate_uid public instance method' do
|
22
|
-
model.instance_methods.must_include :generate_uid
|
22
|
+
expect(model.instance_methods).must_include :generate_uid
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -9,14 +9,14 @@ TestConf.orm_modules.each do |orm_module|
|
|
9
9
|
subject{ user.public_uid }
|
10
10
|
|
11
11
|
context 'in new record' do
|
12
|
-
it{ subject.must_be_nil }
|
12
|
+
it{ expect(subject).must_be_nil }
|
13
13
|
|
14
14
|
describe '#generate_uid' do
|
15
15
|
before do
|
16
16
|
user.generate_uid
|
17
17
|
end
|
18
18
|
|
19
|
-
it { subject.wont_be_nil }
|
19
|
+
it { expect(subject).wont_be_nil }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -27,12 +27,12 @@ TestConf.orm_modules.each do |orm_module|
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'should generate 10 chars' do
|
30
|
-
subject.must_be_kind_of String
|
31
|
-
subject.length.must_equal(10)
|
30
|
+
expect(subject).must_be_kind_of String
|
31
|
+
expect(subject.length).must_equal(10)
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'string including up & down case' do
|
35
|
-
subject.must_match(/^[a-zA-Z]+$/)
|
35
|
+
expect(subject).must_match(/^[a-zA-Z]+$/)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -9,14 +9,14 @@ TestConf.orm_modules.each do |orm_module|
|
|
9
9
|
subject{ user.public_uid }
|
10
10
|
|
11
11
|
context 'in new record' do
|
12
|
-
it{ subject.must_be_nil }
|
12
|
+
it{ expect(subject).must_be_nil }
|
13
13
|
|
14
14
|
describe '#generate_uid' do
|
15
15
|
before do
|
16
16
|
user.generate_uid
|
17
17
|
end
|
18
18
|
|
19
|
-
it { subject.wont_be_nil }
|
19
|
+
it { expect(subject).wont_be_nil }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -27,8 +27,8 @@ TestConf.orm_modules.each do |orm_module|
|
|
27
27
|
user.reload
|
28
28
|
end
|
29
29
|
|
30
|
-
it{ subject.must_be_kind_of(String) }
|
31
|
-
it{ subject.length.must_equal(8) }
|
30
|
+
it{ expect(subject).must_be_kind_of(String) }
|
31
|
+
it{ expect(subject.length).must_equal(8) }
|
32
32
|
end
|
33
33
|
end
|
34
34
|
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,43 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: public_uid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 2.1.1
|
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-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
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
|
@@ -84,28 +98,28 @@ dependencies:
|
|
84
98
|
name: sqlite3
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- - "
|
101
|
+
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
103
|
+
version: 1.4.1
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
|
-
- - "
|
108
|
+
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
110
|
+
version: 1.4.1
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
112
|
+
name: activesupport
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
|
-
- - "
|
115
|
+
- - ">"
|
102
116
|
- !ruby/object:Gem::Version
|
103
117
|
version: '4.2'
|
104
118
|
type: :development
|
105
119
|
prerelease: false
|
106
120
|
version_requirements: !ruby/object:Gem::Requirement
|
107
121
|
requirements:
|
108
|
-
- - "
|
122
|
+
- - ">"
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '4.2'
|
111
125
|
description: Automatic generates public unique identifier for model
|
@@ -121,17 +135,21 @@ files:
|
|
121
135
|
- LICENSE.txt
|
122
136
|
- README.md
|
123
137
|
- Rakefile
|
124
|
-
- lib/orm/active_record.rb
|
125
138
|
- lib/public_uid.rb
|
139
|
+
- lib/public_uid/generators/hex_string_secure_random.rb
|
126
140
|
- lib/public_uid/generators/number_random.rb
|
141
|
+
- lib/public_uid/generators/number_secure_random.rb
|
127
142
|
- lib/public_uid/generators/range_string.rb
|
128
143
|
- lib/public_uid/model.rb
|
144
|
+
- lib/public_uid/model_concern.rb
|
129
145
|
- lib/public_uid/set_public_uid.rb
|
130
146
|
- lib/public_uid/tasks.rb
|
131
147
|
- lib/public_uid/tasks/generate.rake
|
132
148
|
- lib/public_uid/version.rb
|
133
149
|
- public_uid.gemspec
|
150
|
+
- test/lib/generators/hex_string_secure_random_test.rb
|
134
151
|
- test/lib/generators/number_random_test.rb
|
152
|
+
- test/lib/generators/number_secure_random_test.rb
|
135
153
|
- test/lib/generators/range_string_test.rb
|
136
154
|
- test/lib/set_public_uid_test.rb
|
137
155
|
- test/lib/tasks/generate_test.rb
|
@@ -139,6 +157,7 @@ files:
|
|
139
157
|
- test/models/instance_methods_initialization_test.rb
|
140
158
|
- test/models/model_with_custom_generator_test.rb
|
141
159
|
- test/models/model_with_generator_defaults_test.rb
|
160
|
+
- test/models/model_with_public_uid_concern_test.rb
|
142
161
|
- test/support/orm/active_record.rb
|
143
162
|
- test/support/test_conf.rb
|
144
163
|
- test/test_helper.rb
|
@@ -161,13 +180,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
180
|
- !ruby/object:Gem::Version
|
162
181
|
version: '0'
|
163
182
|
requirements: []
|
164
|
-
|
165
|
-
rubygems_version: 2.6.11
|
183
|
+
rubygems_version: 3.0.3
|
166
184
|
signing_key:
|
167
185
|
specification_version: 4
|
168
186
|
summary: Automatic generates public UID column
|
169
187
|
test_files:
|
188
|
+
- test/lib/generators/hex_string_secure_random_test.rb
|
170
189
|
- test/lib/generators/number_random_test.rb
|
190
|
+
- test/lib/generators/number_secure_random_test.rb
|
171
191
|
- test/lib/generators/range_string_test.rb
|
172
192
|
- test/lib/set_public_uid_test.rb
|
173
193
|
- test/lib/tasks/generate_test.rb
|
@@ -175,6 +195,7 @@ test_files:
|
|
175
195
|
- test/models/instance_methods_initialization_test.rb
|
176
196
|
- test/models/model_with_custom_generator_test.rb
|
177
197
|
- test/models/model_with_generator_defaults_test.rb
|
198
|
+
- test/models/model_with_public_uid_concern_test.rb
|
178
199
|
- test/support/orm/active_record.rb
|
179
200
|
- test/support/test_conf.rb
|
180
201
|
- test/test_helper.rb
|
data/lib/orm/active_record.rb
DELETED