pretty_id 0.0.2 → 0.0.3
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 +2 -1
- data/lib/pretty_id/core.rb +3 -1
- data/lib/pretty_id/version.rb +1 -1
- data/spec/pretty_id_spec.rb +51 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b63bbfa09c064af3e67d8d99b8d371c29382341
|
4
|
+
data.tar.gz: 14a2cd3a898fa3fef57c5ea6b8e69e7562869c22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5eabd575be2823eb3b84e30cc6c41f264fa1cf2ec9bbe8cbf813e87721feb5b5f356c08d41393f6a9af1ddf50b89da1f145d012a4f251114eca4c8f027a3e205
|
7
|
+
data.tar.gz: a754a9a7ac417eba6bfbc8f47c92583417bc88e40fad35a0321626389a09a419716cba03af9e4071a7f791f6d3c44ba750139ed60aab2e5832d748a0836fe83b
|
data/README.md
CHANGED
@@ -46,7 +46,8 @@ SecureRandom.urlsafe_base64(options[:length] / 1.333)
|
|
46
46
|
class Book < ActiveRecord::Base
|
47
47
|
has_pretty_id method: :urlsafe_base64, # default: :pretty
|
48
48
|
column: :another_string_column, # default: :pretty_id
|
49
|
-
length: 12
|
49
|
+
length: 12, # default: 8
|
50
|
+
uniq: false # default: true
|
50
51
|
end
|
51
52
|
```
|
52
53
|
|
data/lib/pretty_id/core.rb
CHANGED
@@ -8,7 +8,8 @@ module PrettyId
|
|
8
8
|
def has_pretty_id(options = {})
|
9
9
|
default_options = {
|
10
10
|
column: :pretty_id,
|
11
|
-
method: :pretty
|
11
|
+
method: :pretty,
|
12
|
+
uniq: true
|
12
13
|
}
|
13
14
|
|
14
15
|
options = default_options.merge(options)
|
@@ -32,6 +33,7 @@ module PrettyId
|
|
32
33
|
define_method(:"generate_#{options[:column]}") do
|
33
34
|
new_pretty_id = loop do
|
34
35
|
random_pretty_id = options[:generate_proc].call
|
36
|
+
break random_pretty_id if !options[:uniq]
|
35
37
|
exists_hash = {}
|
36
38
|
exists_hash[options[:column]] = random_pretty_id
|
37
39
|
break random_pretty_id unless self.class.exists?(exists_hash)
|
data/lib/pretty_id/version.rb
CHANGED
data/spec/pretty_id_spec.rb
CHANGED
@@ -5,6 +5,11 @@ class UserWithPrettyId < ActiveRecord::Base
|
|
5
5
|
has_pretty_id
|
6
6
|
end
|
7
7
|
|
8
|
+
class UserWithNonUniquePrettyId < ActiveRecord::Base
|
9
|
+
self.table_name = 'users'
|
10
|
+
has_pretty_id uniq: false
|
11
|
+
end
|
12
|
+
|
8
13
|
class UserWithLongerPrettyId < ActiveRecord::Base
|
9
14
|
self.table_name = 'users'
|
10
15
|
has_pretty_id length: 16
|
@@ -25,6 +30,12 @@ class UserWithPrettyIdAlt < ActiveRecord::Base
|
|
25
30
|
has_pretty_id column: :pretty_id_alt
|
26
31
|
end
|
27
32
|
|
33
|
+
class UserWithTwoPrettyIds < ActiveRecord::Base
|
34
|
+
self.table_name = 'users'
|
35
|
+
has_pretty_id
|
36
|
+
has_pretty_id column: :pretty_id_alt
|
37
|
+
end
|
38
|
+
|
28
39
|
describe PrettyId do
|
29
40
|
it 'assigns before creation' do
|
30
41
|
u = UserWithPrettyId.new
|
@@ -46,6 +57,12 @@ describe PrettyId do
|
|
46
57
|
expect(u.pretty_id_alt).to be_present
|
47
58
|
end
|
48
59
|
|
60
|
+
it 'fails on unknown method' do
|
61
|
+
expect {
|
62
|
+
UserWithPrettyId.send(:has_pretty_id, method: :foobar)
|
63
|
+
}.to raise_error(/Unknown \:method/)
|
64
|
+
end
|
65
|
+
|
49
66
|
describe 'preventing duplicates' do
|
50
67
|
before do
|
51
68
|
UserWithPrettyId.stub(:exists?).and_return(true, false)
|
@@ -56,6 +73,18 @@ describe PrettyId do
|
|
56
73
|
u = UserWithPrettyId.create
|
57
74
|
expect(u.pretty_id).to eq 'cccccccc'
|
58
75
|
end
|
76
|
+
|
77
|
+
context 'uniq = false' do
|
78
|
+
before do
|
79
|
+
UserWithNonUniquePrettyId.stub(:exists?).and_return(true, false)
|
80
|
+
UserWithNonUniquePrettyId.stub(:rand).and_return(1, 2)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'prevents duplicates' do
|
84
|
+
u = UserWithNonUniquePrettyId.create
|
85
|
+
expect(u.pretty_id).to eq 'bccccccc'
|
86
|
+
end
|
87
|
+
end
|
59
88
|
end
|
60
89
|
|
61
90
|
describe 'method = pretty' do
|
@@ -76,13 +105,13 @@ describe PrettyId do
|
|
76
105
|
it 'assigns ~21 characters' do
|
77
106
|
u = UserWithUrlsafeBase64PrettyId.new
|
78
107
|
u.generate_pretty_id
|
79
|
-
expect(u.pretty_id.length).to
|
108
|
+
expect(u.pretty_id.length).to be_within(1).of(21)
|
80
109
|
end
|
81
110
|
|
82
111
|
it 'assigns more characters' do
|
83
112
|
u = UserWithLongerUrlsafeBase64PrettyId.new
|
84
113
|
u.generate_pretty_id
|
85
|
-
expect(u.pretty_id.length).to
|
114
|
+
expect(u.pretty_id.length).to be_within(1).of(32)
|
86
115
|
end
|
87
116
|
end
|
88
117
|
|
@@ -105,4 +134,24 @@ describe PrettyId do
|
|
105
134
|
user.regenerate_pretty_id!
|
106
135
|
end
|
107
136
|
end
|
137
|
+
|
138
|
+
describe 'two pretty_ids' do
|
139
|
+
let(:user) { UserWithTwoPrettyIds.create }
|
140
|
+
|
141
|
+
it 'assigns both' do
|
142
|
+
expect(user.pretty_id).to be_present
|
143
|
+
expect(user.pretty_id_alt).to be_present
|
144
|
+
expect(user.pretty_id).to_not eq user.pretty_id_alt
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'can regenerate one but not the other' do
|
148
|
+
expect {
|
149
|
+
user.regenerate_pretty_id
|
150
|
+
}.to change { user.pretty_id }
|
151
|
+
|
152
|
+
expect {
|
153
|
+
user.regenerate_pretty_id_alt
|
154
|
+
}.to_not change { user.pretty_id }
|
155
|
+
end
|
156
|
+
end
|
108
157
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretty_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Becker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -132,3 +132,4 @@ test_files:
|
|
132
132
|
- spec/pretty_id_spec.rb
|
133
133
|
- spec/setup.rb
|
134
134
|
- spec/spec_helper.rb
|
135
|
+
has_rdoc:
|