has_public_id 1.1.0 → 1.1.5
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.
- data/README.md +8 -11
- data/lib/has_public_id/activerecord/mixin.rb +3 -0
- data/lib/has_public_id/util.rb +14 -6
- data/lib/has_public_id/version.rb +1 -1
- data/test/dummy/app/models/user.rb +1 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +3059 -0
- data/test/dummy/test/models/user_test.rb +49 -3
- data/test/has_public_id_test.rb +43 -31
- metadata +2 -2
data/README.md
CHANGED
@@ -2,20 +2,17 @@
|
|
2
2
|
|
3
3
|
## Description
|
4
4
|
|
5
|
-
Simplifies the generation and use of
|
5
|
+
Simplifies the generation and use of unique, random identifiers for ActiveRecord models.
|
6
6
|
|
7
|
-
|
8
|
-
base 64 encoded string with the following characters: a-z, A-Z, 0-10, "-" and "_".
|
7
|
+
We generate a string of random (using SecureRandom) alphanumeric characters (a-z, A-Z, 0-9) along with an optional prefix that defaults to the first 3 characters of the model name. The random string has a default length of 12, providing 62 ^ 12 possible strings.
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
Even so, the plugin is smart enough to discard ID's that are already in use for
|
14
|
-
a given model and try again.
|
9
|
+
On assignment to the model, if duplicate ID is detected in
|
10
|
+
the database, a new random identifier is generated.
|
15
11
|
|
16
12
|
## Installation
|
17
13
|
Add to Gemfile / etc
|
18
|
-
|
14
|
+
|
15
|
+
gem 'public_id'
|
19
16
|
|
20
17
|
## Usage
|
21
18
|
|
@@ -28,11 +25,11 @@ rails generate migration add_ident_to_users ident:string
|
|
28
25
|
Tell your activerecord object that ident is your new public identifier.
|
29
26
|
```ruby
|
30
27
|
class User < ActiveRecord::Base
|
31
|
-
has_public_id :ident
|
28
|
+
has_public_id :ident, prefix: 'user'
|
32
29
|
# Automatically defines to_param as :ident
|
33
30
|
end
|
34
31
|
User.new.ident
|
35
|
-
# => "
|
32
|
+
# => "user-ECNrdIzvCBh8"
|
36
33
|
|
37
34
|
```
|
38
35
|
|
@@ -36,6 +36,9 @@ module HasPublicId
|
|
36
36
|
def find_by_public_id(public_id)
|
37
37
|
where(self.public_id_attr => public_id).first
|
38
38
|
end
|
39
|
+
def find_by_public_id!(public_id)
|
40
|
+
where(self.public_id_attr => public_id).first!
|
41
|
+
end
|
39
42
|
def new_public_id
|
40
43
|
while(true)
|
41
44
|
new_id = ::HasPublicId::Util.new_public_id(self, self.public_id_options)
|
data/lib/has_public_id/util.rb
CHANGED
@@ -1,16 +1,24 @@
|
|
1
1
|
module HasPublicId
|
2
2
|
module Util
|
3
|
+
DEFAULT_CHAR_SET = (0..9).collect { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a
|
4
|
+
def self.char_set
|
5
|
+
@car_set ||= DEFAULT_CHAR_SET
|
6
|
+
end
|
3
7
|
def self.generate_random_suffix(length)
|
4
|
-
|
8
|
+
(0...length.to_i).map do |i|
|
9
|
+
char_set[SecureRandom.random_number(char_set.length)]
|
10
|
+
end.join
|
5
11
|
end
|
6
|
-
def self.generate_prefix(klass
|
7
|
-
klass.to_s.demodulize.underscore.first(3)
|
12
|
+
def self.generate_prefix(klass)
|
13
|
+
klass.to_s.demodulize.underscore.first(3)
|
8
14
|
end
|
9
15
|
def self.new_public_id(klass, options = {})
|
10
|
-
length = options[:length] ||
|
11
|
-
prefix = options.fetch(:prefix, generate_prefix(klass
|
16
|
+
length = options[:length] || 12
|
17
|
+
prefix = options.fetch(:prefix, generate_prefix(klass))
|
18
|
+
prefix = nil if !prefix || prefix.empty?
|
19
|
+
join_with = options.fetch(:join_with, '-')
|
12
20
|
suffix = generate_random_suffix(length)
|
13
|
-
|
21
|
+
[prefix, suffix].compact.join(join_with)
|
14
22
|
end
|
15
23
|
|
16
24
|
end
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|