pretty_id 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2adaf9801acbc841856a03333afaaddf61d1eba3
4
- data.tar.gz: 478f9e9224476be744f41cf237d8e774ed2af44a
3
+ metadata.gz: a7f1120896f3956cc8a5706975b38fcec7f4fdeb
4
+ data.tar.gz: 8d816ca3e1a09fff5db1550088b87ce5e3343f8b
5
5
  SHA512:
6
- metadata.gz: dcf2bbba6f40717818e2eecee56d154557b620313c9666d7643e3ed03e443c81e1dbd369e9d369b898e537fa03e8dc57df9dbe87b14d8cfd7751c19a971926e8
7
- data.tar.gz: da70deca1e75cb700629ae154d02358ad50185443644b2369ba39a9691bd9e7b1528ff45c73a7fe2fa9f1d88474ccbd330a0879c03b638ced897f4e9dbb872cd
6
+ metadata.gz: d80a89411bee051f8fc265ea1dbe676a98f455f1c78d5b960a6d18449b5c6c724787f8784738b3385ca65909f547961a5359473c3769dba0f1db26b73e8dafad
7
+ data.tar.gz: ab3657e910acdb234d8c1ba15c81ab6745c1d0d5823576ed6eafd745058143d53b2020d25addd7401638cb37fc66d3068b3beb7af1bcd253d874a70a4a0fea54
data/README.md CHANGED
@@ -28,19 +28,43 @@ class Book < ActiveRecord::Base
28
28
  end
29
29
  ```
30
30
 
31
+ ## Generation methods
32
+
33
+ #### :pretty (default)
34
+ ```ruby
35
+ chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
36
+ Array.new(options[:length]) { chars[rand(chars.length)] }.join
37
+ ```
38
+
39
+ #### :urlsafe_base64
40
+ ```ruby
41
+ SecureRandom.urlsafe_base64(options[:length] / 1.333)
42
+ ```
43
+
31
44
  ## Options
32
45
  ```ruby
33
46
  class Book < ActiveRecord::Base
34
- has_pretty_id column: :another_string_column, # default: :pretty_id
47
+ has_pretty_id method: :urlsafe_base64, # default: :pretty
48
+ column: :another_string_column, # default: :pretty_id
35
49
  length: 12 # default: 8
36
50
  end
37
51
  ```
38
52
 
39
- ## Warnings
53
+ ## Instance methods
54
+
55
+ #### `#regenerate_#{column_name}`
56
+ ```ruby
57
+ user = User.create
58
+ user.pretty_id # => 'a0923sdf'
59
+ user.regenerate_pretty_id
60
+ user.pretty_id # => 'lf91fs9s'
61
+ ```
62
+
63
+ #### `#regenerate_#{column_name}!`
64
+ Same as above, but also calls `save` on the record
40
65
 
41
- Pretty IDs are generated using the [Bignum#to_s](http://www.ruby-doc.org/core-2.1.1/Bignum.html#method-i-to_s) method, and since we're seeding with a random value, there's no guarantee that they'll always be 8 characters long. However, the IDs are always checked against existing values in the database to ensure uniqueness.
42
66
 
43
- [0]: http://img.shields.io/travis/dobtco/pretty_id.svg
44
- [1]: http://img.shields.io/codeclimate/github/dobtco/pretty_id.svg
45
- [2]: http://img.shields.io/coveralls/dobtco/pretty_id.svg
46
- [3]: http://img.shields.io/gem/v/pretty_id.svg
67
+ [0]: https://img.shields.io/travis/dobtco/pretty_id.svg
68
+ [1]: https://img.shields.io/codeclimate/github/dobtco/pretty_id.svg
69
+ [2]: https://img.shields.io/coveralls/dobtco/pretty_id.svg
70
+ [3]: https://img.shields.io/gem/v/pretty_id.svg
@@ -8,14 +8,30 @@ module PrettyId
8
8
  def has_pretty_id(options = {})
9
9
  default_options = {
10
10
  column: :pretty_id,
11
- length: 8
11
+ method: :pretty
12
12
  }
13
13
 
14
14
  options = default_options.merge(options)
15
15
 
16
+ case options[:method]
17
+ when :pretty
18
+ options[:length] ||= 8
19
+ options[:generate_proc] = -> {
20
+ chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
21
+ Array.new(options[:length]) { chars[rand(chars.length)] }.join
22
+ }
23
+ when :urlsafe_base64
24
+ options[:length] ||= 21
25
+ options[:generate_proc] = -> {
26
+ SecureRandom.urlsafe_base64(options[:length] / 1.333)
27
+ }
28
+ else
29
+ fail 'Unknown :method passed to has_pretty_id'
30
+ end
31
+
16
32
  define_method(:"generate_#{options[:column]}") do
17
33
  new_pretty_id = loop do
18
- random_pretty_id = rand(36**options[:length]).to_s(36)
34
+ random_pretty_id = options[:generate_proc].call
19
35
  exists_hash = {}
20
36
  exists_hash[options[:column]] = random_pretty_id
21
37
  break random_pretty_id unless self.class.exists?(exists_hash)
@@ -24,6 +40,15 @@ module PrettyId
24
40
  self.send "#{options[:column]}=", new_pretty_id
25
41
  end
26
42
 
43
+ define_method(:"regenerate_#{options[:column]}") do
44
+ self.send("generate_#{options[:column]}")
45
+ end
46
+
47
+ define_method(:"regenerate_#{options[:column]}!") do
48
+ self.send("generate_#{options[:column]}")
49
+ self.save
50
+ end
51
+
27
52
  before_create :"generate_#{options[:column]}"
28
53
  end
29
54
  end
@@ -1,3 +1,3 @@
1
1
  module PrettyId
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -5,6 +5,21 @@ class UserWithPrettyId < ActiveRecord::Base
5
5
  has_pretty_id
6
6
  end
7
7
 
8
+ class UserWithLongerPrettyId < ActiveRecord::Base
9
+ self.table_name = 'users'
10
+ has_pretty_id length: 16
11
+ end
12
+
13
+ class UserWithUrlsafeBase64PrettyId < ActiveRecord::Base
14
+ self.table_name = 'users'
15
+ has_pretty_id method: :urlsafe_base64
16
+ end
17
+
18
+ class UserWithLongerUrlsafeBase64PrettyId < ActiveRecord::Base
19
+ self.table_name = 'users'
20
+ has_pretty_id method: :urlsafe_base64, length: 32
21
+ end
22
+
8
23
  class UserWithPrettyIdAlt < ActiveRecord::Base
9
24
  self.table_name = 'users'
10
25
  has_pretty_id column: :pretty_id_alt
@@ -34,12 +49,60 @@ describe PrettyId do
34
49
  describe 'preventing duplicates' do
35
50
  before do
36
51
  UserWithPrettyId.stub(:exists?).and_return(true, false)
37
- UserWithPrettyId.any_instance.stub(:rand).and_return(1, 2)
52
+ UserWithPrettyId.stub(:rand).and_return(1, 2)
38
53
  end
39
54
 
40
55
  it 'prevents duplicates' do
41
56
  u = UserWithPrettyId.create
42
- expect(u.pretty_id).to eq '2'
57
+ expect(u.pretty_id).to eq 'cccccccc'
58
+ end
59
+ end
60
+
61
+ describe 'method = pretty' do
62
+ it 'assigns 8 characters' do
63
+ u = UserWithPrettyId.new
64
+ u.generate_pretty_id
65
+ expect(u.pretty_id.length).to eq 8
66
+ end
67
+
68
+ it 'assigns longer ids' do
69
+ u = UserWithLongerPrettyId.new
70
+ u.generate_pretty_id
71
+ expect(u.pretty_id.length).to eq 16
72
+ end
73
+ end
74
+
75
+ describe 'method = urlsafe_base64' do
76
+ it 'assigns ~21 characters' do
77
+ u = UserWithUrlsafeBase64PrettyId.new
78
+ u.generate_pretty_id
79
+ expect(u.pretty_id.length).to eq be_within(1).of(21)
80
+ end
81
+
82
+ it 'assigns more characters' do
83
+ u = UserWithLongerUrlsafeBase64PrettyId.new
84
+ u.generate_pretty_id
85
+ expect(u.pretty_id.length).to eq be_within(1).of(32)
86
+ end
87
+ end
88
+
89
+ describe '#regenerate_pretty_id' do
90
+ let(:user) { UserWithPrettyId.create }
91
+
92
+ it 'regenerates' do
93
+ expect {
94
+ user.regenerate_pretty_id
95
+ }.to change { user.pretty_id }
96
+ end
97
+
98
+ it 'does not call save' do
99
+ expect(user).to_not receive(:save)
100
+ user.regenerate_pretty_id
101
+ end
102
+
103
+ it 'has a dangerous counterpart' do
104
+ expect(user).to receive(:save)
105
+ user.regenerate_pretty_id!
43
106
  end
44
107
  end
45
108
  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.1
4
+ version: 0.0.2
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-05-14 00:00:00.000000000 Z
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails