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 CHANGED
@@ -2,20 +2,17 @@
2
2
 
3
3
  ## Description
4
4
 
5
- Simplifies the generation and use of random, secure ID's in your activerecord models.
5
+ Simplifies the generation and use of unique, random identifiers for ActiveRecord models.
6
6
 
7
- Uses SecureRandom.urlsafe_base64(10) by default, which generates a 14 character
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
- Assuming an even distribution, this allows for 64 ^ 14 different possible encodings
11
- with the default settings.
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
- gem 'public_id'
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
- # => "use-ECNrdIzvCBh8jg"
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)
@@ -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
- SecureRandom.urlsafe_base64(length)
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, joiner)
7
- klass.to_s.demodulize.underscore.first(3) + joiner
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] || 10
11
- prefix = options.fetch(:prefix, generate_prefix(klass, options.fetch(:joiner, '-') ))
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
- "#{prefix ? prefix : ''}#{suffix}"
21
+ [prefix, suffix].compact.join(join_with)
14
22
  end
15
23
 
16
24
  end
@@ -1,3 +1,3 @@
1
1
  module HasPublicId
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.5"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  class User < ActiveRecord::Base
2
- has_public_id :ident
2
+ has_public_id :ident, prefix: 'user'
3
3
  end
Binary file