public_uid 1.0.2 → 1.1.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 +29 -12
- data/lib/public_uid/model.rb +1 -1
- data/lib/public_uid/set_public_uid.rb +6 -6
- data/lib/public_uid/version.rb +1 -1
- data/test/lib/set_public_uid_test.rb +7 -4
- data/test/models/custom_public_uid_column_test.rb +16 -14
- data/test/models/model_with_custom_generator_test.rb +1 -1
- data/test/models/model_with_generator_defaults_test.rb +5 -5
- data/test/support/orm/active_record.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76143c57285dda08be346c203901c384bc273bf0
|
4
|
+
data.tar.gz: 1bf42db639233d908a3bd447c57d9be3582fa572
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd980096e936fa5816f1838aeb614d2d51dda724f66f046e45bceab9d072a0103e09a07325e92e800d45217352dc10cfe464c0897888b9711090c98d3a820428
|
7
|
+
data.tar.gz: 31f622b8165abae325f8c81921820fc18b91bf8dd0fe6c51a152c989834ef6a31b3841ef9d0a1e9ce40fec2ca57941e99e8f3c73b0591e0c8cad7d7a0cb7b8c3
|
data/README.md
CHANGED
@@ -19,17 +19,18 @@ against you.
|
|
19
19
|
|
20
20
|
This is bad:
|
21
21
|
|
22
|
-
http://eq8.eu/orders/12/edit
|
23
|
-
http://eq8.eu/orders/12-order-for-mouse-and-keyboard/edit
|
22
|
+
http://www.eq8.eu/orders/12/edit
|
23
|
+
http://www.eq8.eu/orders/12-order-for-mouse-and-keyboard/edit
|
24
24
|
|
25
25
|
However if you generate random unique identifier and use that as a public
|
26
26
|
identifier, you won't have to worry about that.
|
27
27
|
|
28
28
|
This is how it should be:
|
29
29
|
|
30
|
-
http://eq8.eu/orders/8395820/edit
|
31
|
-
http://eq8.eu/orders/
|
32
|
-
http://eq8.eu/orders/
|
30
|
+
http://www.eq8.eu/orders/8395820/edit
|
31
|
+
http://www.eq8.eu/orders/8395820-order-for-mouse-and-keyboard/edit
|
32
|
+
http://www.eq8.eu/orders/abaoeule/edit
|
33
|
+
http://www.eq8.eu/orders/aZc3/edit
|
33
34
|
|
34
35
|
So keep `record.id` for your internal relationships and show `public_id`
|
35
36
|
to the world :smile:
|
@@ -47,7 +48,7 @@ And then execute:
|
|
47
48
|
|
48
49
|
## Usage
|
49
50
|
|
50
|
-
Create database column for public unique
|
51
|
+
Create database column for public unique ID.
|
51
52
|
|
52
53
|
```ruby
|
53
54
|
class AddPublicUidToUsers < ActiveRecord::Migration
|
@@ -65,7 +66,7 @@ class User < ActiveRecord::Base
|
|
65
66
|
end
|
66
67
|
```
|
67
68
|
|
68
|
-
This will automatically generate
|
69
|
+
This will automatically generate unique 8 char downcase string for
|
69
70
|
column `public_uid`.
|
70
71
|
|
71
72
|
|
@@ -73,7 +74,7 @@ column `public_uid`.
|
|
73
74
|
u = User.new
|
74
75
|
u.public_uid #=> nil
|
75
76
|
u.save! #=> true
|
76
|
-
u.public_uid #=>
|
77
|
+
u.public_uid #=> "aeuhsthi"
|
77
78
|
```
|
78
79
|
|
79
80
|
If you want to use different column just specify column option:
|
@@ -88,15 +89,15 @@ end
|
|
88
89
|
u = User.new
|
89
90
|
u.guid #=> nil
|
90
91
|
u.save! #=> true
|
91
|
-
u.guid #=>
|
92
|
+
u.guid #=> "troxuroh"
|
92
93
|
```
|
93
94
|
|
94
|
-
If you want to generate random
|
95
|
+
If you want to generate random Integer you can use built-in number
|
95
96
|
generator:
|
96
97
|
|
97
98
|
```ruby
|
98
99
|
class User < ActiveRecord::Base
|
99
|
-
generate_public_uid generator: PublicUid::Generators::
|
100
|
+
generate_public_uid generator: PublicUid::Generators::NumberRandom.new
|
100
101
|
end
|
101
102
|
```
|
102
103
|
|
@@ -104,9 +105,14 @@ end
|
|
104
105
|
u = User.new
|
105
106
|
u.public_uid #=> nil
|
106
107
|
u.save! #=> true
|
107
|
-
u.public_uid #=>
|
108
|
+
u.public_uid #=> 4567123
|
108
109
|
```
|
109
110
|
|
111
|
+
**Note** Warning **PostgreSQL** have built in type safety meaning that this
|
112
|
+
generator wont work if your public uniq ID column is a String (as the
|
113
|
+
gem would try to set Integer on a String). If you really want a number
|
114
|
+
like string you can specify number range for Range String Generator
|
115
|
+
|
110
116
|
### Customizing generated string
|
111
117
|
|
112
118
|
```ruby
|
@@ -123,6 +129,17 @@ u.save! #=> true
|
|
123
129
|
u.public_uid #=> "aZ3e"
|
124
130
|
```
|
125
131
|
|
132
|
+
To generate number format String you can specify number range
|
133
|
+
|
134
|
+
```
|
135
|
+
class User < ActiveRecord::Base
|
136
|
+
UID_RANGE = ('1'..'9').to_a
|
137
|
+
generate_public_uid generator: PublicUid::Generators::RangeString.new(4, UID_RANGE)
|
138
|
+
end
|
139
|
+
|
140
|
+
# User.create.public_uid == "1234"
|
141
|
+
```
|
142
|
+
|
126
143
|
### Customizing randomized number
|
127
144
|
|
128
145
|
```ruby
|
data/lib/public_uid/model.rb
CHANGED
@@ -16,7 +16,7 @@ module PublicUid
|
|
16
16
|
|
17
17
|
def generate_public_uid(options={})
|
18
18
|
@public_uid_column = options[:column] || :public_uid
|
19
|
-
@public_uid_generator = options[:generator] || Generators::
|
19
|
+
@public_uid_generator = options[:generator] || Generators::RangeString.new
|
20
20
|
before_create :generate_uid, unless: @public_uid_column
|
21
21
|
end
|
22
22
|
end
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module PublicUid
|
2
2
|
class SetPublicUid
|
3
|
+
NewUidNotSetYet = Class.new(StandardError)
|
4
|
+
PublicUidColumnDoesNotExist = Class.new(StandardError)
|
5
|
+
NoPublicUidColumnSpecified = Class.new(StandardError)
|
6
|
+
NoRecordSpecified = Class.new(StandardError)
|
7
|
+
|
3
8
|
attr_reader :new_uid
|
4
9
|
|
5
10
|
def initialize(options)
|
@@ -22,16 +27,11 @@ module PublicUid
|
|
22
27
|
private
|
23
28
|
|
24
29
|
def similar_uid_exist?
|
25
|
-
@klass.where(public_uid: new_uid
|
30
|
+
@klass.where(public_uid: new_uid).count > 0
|
26
31
|
end
|
27
32
|
|
28
33
|
def check_column_existance
|
29
34
|
raise PublicUidColumnDoesNotExist if @klass.column_names.include?(@column)
|
30
35
|
end
|
31
|
-
|
32
|
-
class NewUidNotSetYet < StandardError; end
|
33
|
-
class PublicUidColumnDoesNotExist < StandardError; end
|
34
|
-
class NoPublicUidColumnSpecified < StandardError; end
|
35
|
-
class NoRecordSpecified < StandardError; end
|
36
36
|
end
|
37
37
|
end
|
data/lib/public_uid/version.rb
CHANGED
@@ -76,10 +76,13 @@ TestConf.orm_modules.each do |orm_module|
|
|
76
76
|
|
77
77
|
before { mock(instance).new_uid { 567 } }
|
78
78
|
|
79
|
-
#
|
80
|
-
|
81
|
-
|
82
|
-
|
79
|
+
# There is an issue with passing integer to PostgreSQL type check
|
80
|
+
# in previous version application deal with this issue by converting
|
81
|
+
# everything given by generator to string which is wrong. If the output
|
82
|
+
# of generator is not supported by DB don't use it.
|
83
|
+
it 'must pass exact type of generator to model' do
|
84
|
+
count_mock = stub(record_class).count { 10 }
|
85
|
+
stub(record_class).where( { public_uid: 567 } ) { count_mock }
|
83
86
|
|
84
87
|
trigger.must_equal true
|
85
88
|
end
|
@@ -2,23 +2,25 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
TestConf.orm_modules.each do |orm_module|
|
4
4
|
describe orm_module.description do
|
5
|
-
|
5
|
+
context 'Model with custom public uid column' do
|
6
|
+
let(:user) { "#{orm_module}::CustomPublicUidColumnModel".constantize.new }
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
describe '#custom_uid' do
|
9
|
+
subject{ user.custom_uid }
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'after save' do
|
15
|
-
before do
|
16
|
-
user.save
|
17
|
-
user.reload
|
11
|
+
context 'in new record' do
|
12
|
+
it{ subject.must_be_nil }
|
18
13
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
|
15
|
+
context 'after save' do
|
16
|
+
|
17
|
+
before do
|
18
|
+
user.save
|
19
|
+
user.reload
|
20
|
+
end
|
21
|
+
|
22
|
+
it{ subject.must_be_kind_of(String) }
|
23
|
+
it{ subject.length.must_equal(8) }
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
TestConf.orm_modules.each do |orm_module|
|
4
4
|
describe orm_module.description do
|
5
|
-
|
5
|
+
context 'Model with custom uid generator' do
|
6
6
|
let(:user) { "#{orm_module}::ModelWithCustomGererator".constantize.new }
|
7
7
|
|
8
8
|
describe '#public_uid' do
|
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
TestConf.orm_modules.each do |orm_module|
|
4
4
|
describe orm_module.description do
|
5
|
-
|
5
|
+
context 'Model with default generator' do
|
6
6
|
let(:user) { "#{orm_module}::ModelWithGeneratorDefaults".constantize.new }
|
7
7
|
|
8
8
|
describe '#public_uid' do
|
@@ -13,14 +13,14 @@ TestConf.orm_modules.each do |orm_module|
|
|
13
13
|
end
|
14
14
|
|
15
15
|
context 'after save' do
|
16
|
+
|
16
17
|
before do
|
17
18
|
user.save
|
18
19
|
user.reload
|
19
20
|
end
|
20
|
-
|
21
|
-
it
|
22
|
-
|
23
|
-
end
|
21
|
+
|
22
|
+
it{ subject.must_be_kind_of(String) }
|
23
|
+
it{ subject.length.must_equal(8) }
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
ActiveRecord::Base::establish_connection(adapter: 'sqlite3', database: ':memory:')
|
2
|
-
ActiveRecord::Base.connection.execute(%{CREATE TABLE users (id INTEGER PRIMARY KEY, public_uid VARCHAR, custom_uid
|
2
|
+
ActiveRecord::Base.connection.execute(%{CREATE TABLE users (id INTEGER PRIMARY KEY, public_uid VARCHAR, custom_uid VARCHAR);})
|
3
3
|
|
4
4
|
module ActRec
|
5
5
|
def self.description
|
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: 1.0
|
4
|
+
version: 1.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: 2014-06-
|
11
|
+
date: 2014-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: orm_adapter
|