random_unique_id 0.2.1 → 1.0.0

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: ee955fa4eb84ebac6b2096ecc5c4324088e1fc3f
4
- data.tar.gz: 00a2a57af285376857ead45e3c1e9445f93bbbbf
3
+ metadata.gz: 7483d54993b6187363e8a30e6bdfeaa203e82aae
4
+ data.tar.gz: 620a1b73ab14efcd5ad0203c9730f29e0710de40
5
5
  SHA512:
6
- metadata.gz: 81c9840b8ae51892e18205b0e80a7e9e2f7e8d372aaa68308107d553ec8d1458c065a2822285856d000634827767c0a81fe82220441568aa24797aaa18cb10ea
7
- data.tar.gz: 644037903b77dbd52c50bda634e3dcddd0393ec3eb6a9db7d64a6a0da72fc3c43ef5bf856060fbf305b47600054414762652441e3365776d475188c6aeca7f72
6
+ metadata.gz: 434ad3202151c28c8ae1233aa679baa46b1fa15212b1bc6f36b95522f489a8c58c23a28a269bc3485f90ad5b78acbc36a0e62b8e00c3a452d74d0af090eaca04
7
+ data.tar.gz: 4abf60bc4843a771d17f4082ec280ba1681d7f2efa0b0ab66418bc50f6e855ece38ae0676e34bf6d58b58417e4534f8c910f22fe535a0c6cd6549544a7d02c4a
@@ -3,6 +3,9 @@ rvm:
3
3
  - "1.9.3"
4
4
  - "2.0.0"
5
5
  - "2.1.1"
6
+ - "2.1.2"
7
+ - "2.1.3"
6
8
  gemfile:
7
- - gemfiles/rails_3_2.Gemfile
8
- - gemfiles/rails_4_0.Gemfile
9
+ - gemfiles/rails_3_2.gemfile
10
+ - gemfiles/rails_4_0.gemfile
11
+ - gemfiles/rails_4_1.gemfile
@@ -0,0 +1,13 @@
1
+ # Copyright © 2014, Watu
2
+
3
+ appraise "rails-3_2" do
4
+ gem "actionmailer", "~> 3.2.0"
5
+ end
6
+
7
+ appraise "rails-4_0" do
8
+ gem "actionmailer", "~> 4.0.0"
9
+ end
10
+
11
+ appraise "rails-4_1" do
12
+ gem "actionmailer", "~> 4.1.0"
13
+ end
@@ -0,0 +1,18 @@
1
+ Authoritative changelog in README.md.
2
+
3
+ ## Version 1.0.0 (Oct 28, 2014)
4
+ - Added RandomUniqueId::Util.set_initial_ids.
5
+ - Started testing Ruby 2.1.2 and 2.1.3.
6
+ - Started testing Rails 4.1 (fixed some deprecation warnings).
7
+ - Improved documentation.
8
+
9
+ ## Version 0.2.1 (May 27, 2014)
10
+ - Internal refactorings.
11
+
12
+ ## Version 0.2.0 (Apr 5, 2014)
13
+ - Added method populate_random_unique_ids.
14
+ - Improved documentation
15
+ - Started testing with Ruby 2.1.
16
+
17
+ ## Version 0.1.0 (Jan 6, 2014)
18
+ - Initial release of the code extracted from [Watu](http://github.com/watu).
data/Gemfile CHANGED
@@ -3,14 +3,4 @@
3
3
 
4
4
  source "https://rubygems.org"
5
5
 
6
- gem "bundler", "~> 1.3"
7
- gem "coveralls", require: false
8
- gem "minitest", "~> 4.7.5"
9
- gem "minitest-reporters"
10
- gem "mocha"
11
- gem "rake"
12
- gem "simplecov"
13
- gem "shoulda"
14
- gem "sqlite3"
15
- gem "activesupport", "~> 3.2.0"
16
- gem "activerecord", "~> 3.2.0"
6
+ gemspec
data/README.md CHANGED
@@ -3,14 +3,16 @@
3
3
  [![Build Status](https://travis-ci.org/watu/random_unique_id.png?branch=master)](https://travis-ci.org/watu/random_unique_id)
4
4
  [![Coverage Status](https://coveralls.io/repos/watu/random_unique_id/badge.png?branch=master)](https://coveralls.io/r/watu/random_unique_id?branch=master)
5
5
  [![Code Climate](https://codeclimate.com/github/watu/random_unique_id.png)](https://codeclimate.com/github/watu/random_unique_id)
6
+ [![Inline docs](http://inch-ci.org/github/watu/random_unique_id.png)](http://inch-ci.org/github/watu/random_unique_id)
6
7
  [![Gem Version](https://badge.fury.io/rb/random_unique_id.png)](http://badge.fury.io/rb/random_unique_id)
8
+ [![Dependency Status](https://gemnasium.com/watu/random_unique_id.svg)](https://gemnasium.com/watu/random_unique_id)
7
9
 
8
10
  This gem will generate a random unique id for your active record records that you can use instead of their actual ID for
9
11
  all external interactions with users. The goal is for you to be able to hide how many records you have, for business
10
12
  purposes, but also to make IDs non-predictable.
11
13
 
12
- This gem is built to work with Ruby 1.9, 2.0, 2.1 as well as with Rails 3.2 and 4.0. All of these cases are
13
- [continuously tested for](https://travis-ci.org/watu/random_unique_id.png?branch=master).
14
+ This gem is built to work with Ruby 1.9, 2.0, 2.1 as well as with Rails 3.2, 4.0 and 4.1. All of these cases are
15
+ [continuously tested for](https://travis-ci.org/watu/random_unique_id).
14
16
 
15
17
  ## Installation
16
18
 
@@ -69,12 +71,21 @@ records. For example:
69
71
 
70
72
  ## Changelog
71
73
 
72
- ### Version 0.2.0
74
+ ### Version 1.0.0 (Oct 28, 2014)
75
+ - Added RandomUniqueId::Util.set_initial_ids.
76
+ - Started testing Ruby 2.1.2 and 2.1.3.
77
+ - Started testing Rails 4.1 (fixed some deprecation warnings).
78
+ - Improved documentation.
79
+
80
+ ### Version 0.2.1 (May 27, 2014)
81
+ - Internal refactorings.
82
+
83
+ ### Version 0.2.0 (Apr 5, 2014)
73
84
  - Added method populate_random_unique_ids.
74
85
  - Improved documentation
75
86
  - Started testing with Ruby 2.1.
76
87
 
77
- ### Version 0.1.0
88
+ ### Version 0.1.0 (Jan 6, 2014)
78
89
  - Initial release of the code extracted from [Watu](http://github.com/watu).
79
90
 
80
91
  ## Contributing
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  # encoding: UTF-8
2
2
  # Copyright © 2013, 2014, Watu
3
3
 
4
+ require "rubygems"
5
+ require "bundler/setup"
4
6
  require "bundler/gem_tasks"
5
7
 
6
8
  require "rake/testtask"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "actionmailer", "~> 3.2.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,143 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ random_unique_id (0.2.1)
5
+ activerecord (>= 3.2.0)
6
+ activesupport (>= 3.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (3.2.19)
12
+ actionpack (= 3.2.19)
13
+ mail (~> 2.5.4)
14
+ actionpack (3.2.19)
15
+ activemodel (= 3.2.19)
16
+ activesupport (= 3.2.19)
17
+ builder (~> 3.0.0)
18
+ erubis (~> 2.7.0)
19
+ journey (~> 1.0.4)
20
+ rack (~> 1.4.5)
21
+ rack-cache (~> 1.2)
22
+ rack-test (~> 0.6.1)
23
+ sprockets (~> 2.2.1)
24
+ activemodel (3.2.19)
25
+ activesupport (= 3.2.19)
26
+ builder (~> 3.0.0)
27
+ activerecord (3.2.19)
28
+ activemodel (= 3.2.19)
29
+ activesupport (= 3.2.19)
30
+ arel (~> 3.0.2)
31
+ tzinfo (~> 0.3.29)
32
+ activesupport (3.2.19)
33
+ i18n (~> 0.6, >= 0.6.4)
34
+ multi_json (~> 1.0)
35
+ ansi (1.4.3)
36
+ appraisal (1.0.2)
37
+ bundler
38
+ rake
39
+ thor (>= 0.14.0)
40
+ arel (3.0.3)
41
+ builder (3.0.4)
42
+ coveralls (0.7.1)
43
+ multi_json (~> 1.3)
44
+ rest-client
45
+ simplecov (>= 0.7)
46
+ term-ansicolor
47
+ thor
48
+ docile (1.1.5)
49
+ erubis (2.7.0)
50
+ hashie (3.3.1)
51
+ hike (1.2.3)
52
+ i18n (0.6.11)
53
+ journey (1.0.4)
54
+ json (1.8.1)
55
+ mail (2.5.4)
56
+ mime-types (~> 1.16)
57
+ treetop (~> 1.4.8)
58
+ metaclass (0.0.4)
59
+ mime-types (1.25.1)
60
+ minitest (4.7.5)
61
+ minitest-rails (1.0.1)
62
+ minitest (~> 4.7)
63
+ minitest-test (~> 1.0)
64
+ railties (>= 3.0, < 4.1)
65
+ minitest-reporters (0.14.24)
66
+ ansi
67
+ builder
68
+ minitest (>= 2.12, < 5.0)
69
+ powerbar
70
+ minitest-test (1.1.0)
71
+ minitest (~> 4.0)
72
+ mocha (1.1.0)
73
+ metaclass (~> 0.0.1)
74
+ multi_json (1.10.1)
75
+ netrc (0.7.7)
76
+ polyglot (0.3.5)
77
+ powerbar (1.0.11)
78
+ ansi (~> 1.4.0)
79
+ hashie (>= 1.1.0)
80
+ rack (1.4.5)
81
+ rack-cache (1.2)
82
+ rack (>= 0.4)
83
+ rack-ssl (1.3.4)
84
+ rack
85
+ rack-test (0.6.2)
86
+ rack (>= 1.0)
87
+ railties (3.2.19)
88
+ actionpack (= 3.2.19)
89
+ activesupport (= 3.2.19)
90
+ rack-ssl (~> 1.3.2)
91
+ rake (>= 0.8.7)
92
+ rdoc (~> 3.4)
93
+ thor (>= 0.14.6, < 2.0)
94
+ rake (10.3.2)
95
+ rdoc (3.12.2)
96
+ json (~> 1.4)
97
+ rest-client (1.7.2)
98
+ mime-types (>= 1.16, < 3.0)
99
+ netrc (~> 0.7)
100
+ shoulda (3.5.0)
101
+ shoulda-context (~> 1.0, >= 1.0.1)
102
+ shoulda-matchers (>= 1.4.1, < 3.0)
103
+ shoulda-context (1.2.1)
104
+ shoulda-matchers (2.7.0)
105
+ activesupport (>= 3.0.0)
106
+ simplecov (0.9.0)
107
+ docile (~> 1.1.0)
108
+ multi_json
109
+ simplecov-html (~> 0.8.0)
110
+ simplecov-html (0.8.0)
111
+ sprockets (2.2.2)
112
+ hike (~> 1.2)
113
+ multi_json (~> 1.0)
114
+ rack (~> 1.0)
115
+ tilt (~> 1.1, != 1.3.0)
116
+ sqlite3 (1.3.9)
117
+ term-ansicolor (1.3.0)
118
+ tins (~> 1.0)
119
+ thor (0.19.1)
120
+ tilt (1.4.1)
121
+ tins (1.3.2)
122
+ treetop (1.4.15)
123
+ polyglot
124
+ polyglot (>= 0.3.1)
125
+ tzinfo (0.3.41)
126
+
127
+ PLATFORMS
128
+ ruby
129
+
130
+ DEPENDENCIES
131
+ actionmailer (~> 3.2.0)
132
+ appraisal
133
+ bundler
134
+ coveralls
135
+ minitest
136
+ minitest-rails
137
+ minitest-reporters
138
+ mocha
139
+ rake
140
+ random_unique_id!
141
+ shoulda
142
+ simplecov
143
+ sqlite3
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "actionmailer", "~> 4.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,127 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ random_unique_id (0.2.1)
5
+ activerecord (>= 3.2.0)
6
+ activesupport (>= 3.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.0.9)
12
+ actionpack (= 4.0.9)
13
+ mail (~> 2.5.4)
14
+ actionpack (4.0.9)
15
+ activesupport (= 4.0.9)
16
+ builder (~> 3.1.0)
17
+ erubis (~> 2.7.0)
18
+ rack (~> 1.5.2)
19
+ rack-test (~> 0.6.2)
20
+ activemodel (4.0.9)
21
+ activesupport (= 4.0.9)
22
+ builder (~> 3.1.0)
23
+ activerecord (4.0.9)
24
+ activemodel (= 4.0.9)
25
+ activerecord-deprecated_finders (~> 1.0.2)
26
+ activesupport (= 4.0.9)
27
+ arel (~> 4.0.0)
28
+ activerecord-deprecated_finders (1.0.3)
29
+ activesupport (4.0.9)
30
+ i18n (~> 0.6, >= 0.6.9)
31
+ minitest (~> 4.2)
32
+ multi_json (~> 1.3)
33
+ thread_safe (~> 0.1)
34
+ tzinfo (~> 0.3.37)
35
+ ansi (1.4.3)
36
+ appraisal (1.0.2)
37
+ bundler
38
+ rake
39
+ thor (>= 0.14.0)
40
+ arel (4.0.2)
41
+ builder (3.1.4)
42
+ coveralls (0.7.1)
43
+ multi_json (~> 1.3)
44
+ rest-client
45
+ simplecov (>= 0.7)
46
+ term-ansicolor
47
+ thor
48
+ docile (1.1.5)
49
+ erubis (2.7.0)
50
+ hashie (3.3.1)
51
+ i18n (0.6.11)
52
+ mail (2.5.4)
53
+ mime-types (~> 1.16)
54
+ treetop (~> 1.4.8)
55
+ metaclass (0.0.4)
56
+ mime-types (1.25.1)
57
+ minitest (4.7.5)
58
+ minitest-rails (1.0.1)
59
+ minitest (~> 4.7)
60
+ minitest-test (~> 1.0)
61
+ railties (>= 3.0, < 4.1)
62
+ minitest-reporters (0.14.24)
63
+ ansi
64
+ builder
65
+ minitest (>= 2.12, < 5.0)
66
+ powerbar
67
+ minitest-test (1.1.0)
68
+ minitest (~> 4.0)
69
+ mocha (1.1.0)
70
+ metaclass (~> 0.0.1)
71
+ multi_json (1.10.1)
72
+ netrc (0.7.7)
73
+ polyglot (0.3.5)
74
+ powerbar (1.0.11)
75
+ ansi (~> 1.4.0)
76
+ hashie (>= 1.1.0)
77
+ rack (1.5.2)
78
+ rack-test (0.6.2)
79
+ rack (>= 1.0)
80
+ railties (4.0.9)
81
+ actionpack (= 4.0.9)
82
+ activesupport (= 4.0.9)
83
+ rake (>= 0.8.7)
84
+ thor (>= 0.18.1, < 2.0)
85
+ rake (10.3.2)
86
+ rest-client (1.7.2)
87
+ mime-types (>= 1.16, < 3.0)
88
+ netrc (~> 0.7)
89
+ shoulda (3.5.0)
90
+ shoulda-context (~> 1.0, >= 1.0.1)
91
+ shoulda-matchers (>= 1.4.1, < 3.0)
92
+ shoulda-context (1.2.1)
93
+ shoulda-matchers (2.7.0)
94
+ activesupport (>= 3.0.0)
95
+ simplecov (0.9.0)
96
+ docile (~> 1.1.0)
97
+ multi_json
98
+ simplecov-html (~> 0.8.0)
99
+ simplecov-html (0.8.0)
100
+ sqlite3 (1.3.9)
101
+ term-ansicolor (1.3.0)
102
+ tins (~> 1.0)
103
+ thor (0.19.1)
104
+ thread_safe (0.3.4)
105
+ tins (1.3.2)
106
+ treetop (1.4.15)
107
+ polyglot
108
+ polyglot (>= 0.3.1)
109
+ tzinfo (0.3.41)
110
+
111
+ PLATFORMS
112
+ ruby
113
+
114
+ DEPENDENCIES
115
+ actionmailer (~> 4.0.0)
116
+ appraisal
117
+ bundler
118
+ coveralls
119
+ minitest
120
+ minitest-rails
121
+ minitest-reporters
122
+ mocha
123
+ rake
124
+ random_unique_id!
125
+ shoulda
126
+ simplecov
127
+ sqlite3
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "actionmailer", "~> 4.1.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,125 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ random_unique_id (0.2.1)
5
+ activerecord (>= 3.2.0)
6
+ activesupport (>= 3.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.1.5)
12
+ actionpack (= 4.1.5)
13
+ actionview (= 4.1.5)
14
+ mail (~> 2.5.4)
15
+ actionpack (4.1.5)
16
+ actionview (= 4.1.5)
17
+ activesupport (= 4.1.5)
18
+ rack (~> 1.5.2)
19
+ rack-test (~> 0.6.2)
20
+ actionview (4.1.5)
21
+ activesupport (= 4.1.5)
22
+ builder (~> 3.1)
23
+ erubis (~> 2.7.0)
24
+ activemodel (4.1.5)
25
+ activesupport (= 4.1.5)
26
+ builder (~> 3.1)
27
+ activerecord (4.1.5)
28
+ activemodel (= 4.1.5)
29
+ activesupport (= 4.1.5)
30
+ arel (~> 5.0.0)
31
+ activesupport (4.1.5)
32
+ i18n (~> 0.6, >= 0.6.9)
33
+ json (~> 1.7, >= 1.7.7)
34
+ minitest (~> 5.1)
35
+ thread_safe (~> 0.1)
36
+ tzinfo (~> 1.1)
37
+ ansi (1.4.3)
38
+ appraisal (1.0.2)
39
+ bundler
40
+ rake
41
+ thor (>= 0.14.0)
42
+ arel (5.0.1.20140414130214)
43
+ builder (3.2.2)
44
+ coveralls (0.7.1)
45
+ multi_json (~> 1.3)
46
+ rest-client
47
+ simplecov (>= 0.7)
48
+ term-ansicolor
49
+ thor
50
+ docile (1.1.5)
51
+ erubis (2.7.0)
52
+ i18n (0.6.11)
53
+ json (1.8.1)
54
+ mail (2.5.4)
55
+ mime-types (~> 1.16)
56
+ treetop (~> 1.4.8)
57
+ metaclass (0.0.4)
58
+ mime-types (1.25.1)
59
+ minitest (5.4.1)
60
+ minitest-rails (2.1.0)
61
+ minitest (~> 5.4)
62
+ railties (~> 4.1)
63
+ minitest-reporters (1.0.5)
64
+ ansi
65
+ builder
66
+ minitest (>= 5.0)
67
+ ruby-progressbar
68
+ mocha (1.1.0)
69
+ metaclass (~> 0.0.1)
70
+ multi_json (1.10.1)
71
+ netrc (0.7.7)
72
+ polyglot (0.3.5)
73
+ rack (1.5.2)
74
+ rack-test (0.6.2)
75
+ rack (>= 1.0)
76
+ railties (4.1.5)
77
+ actionpack (= 4.1.5)
78
+ activesupport (= 4.1.5)
79
+ rake (>= 0.8.7)
80
+ thor (>= 0.18.1, < 2.0)
81
+ rake (10.3.2)
82
+ rest-client (1.7.2)
83
+ mime-types (>= 1.16, < 3.0)
84
+ netrc (~> 0.7)
85
+ ruby-progressbar (1.5.1)
86
+ shoulda (3.5.0)
87
+ shoulda-context (~> 1.0, >= 1.0.1)
88
+ shoulda-matchers (>= 1.4.1, < 3.0)
89
+ shoulda-context (1.2.1)
90
+ shoulda-matchers (2.7.0)
91
+ activesupport (>= 3.0.0)
92
+ simplecov (0.9.0)
93
+ docile (~> 1.1.0)
94
+ multi_json
95
+ simplecov-html (~> 0.8.0)
96
+ simplecov-html (0.8.0)
97
+ sqlite3 (1.3.9)
98
+ term-ansicolor (1.3.0)
99
+ tins (~> 1.0)
100
+ thor (0.19.1)
101
+ thread_safe (0.3.4)
102
+ tins (1.3.2)
103
+ treetop (1.4.15)
104
+ polyglot
105
+ polyglot (>= 0.3.1)
106
+ tzinfo (1.2.2)
107
+ thread_safe (~> 0.1)
108
+
109
+ PLATFORMS
110
+ ruby
111
+
112
+ DEPENDENCIES
113
+ actionmailer (~> 4.1.0)
114
+ appraisal
115
+ bundler
116
+ coveralls
117
+ minitest
118
+ minitest-rails
119
+ minitest-reporters
120
+ mocha
121
+ rake
122
+ random_unique_id!
123
+ shoulda
124
+ simplecov
125
+ sqlite3
@@ -9,6 +9,9 @@ require "active_record"
9
9
  module RandomUniqueId
10
10
  extend ActiveSupport::Concern
11
11
 
12
+ # Collection of methods that will end as class methods of ActiveRecord::Base.
13
+ #
14
+ # @see ActiveSupport::Concern
12
15
  module ClassMethods
13
16
  # Mark a model as containing a random unique id. A field called rid of type string is required. It's recommended
14
17
  # that it's indexed and unique. For example, you could add it to a migration like this:
@@ -28,6 +31,13 @@ module RandomUniqueId
28
31
  define_method(:to_param) { rid }
29
32
  end
30
33
 
34
+ # Augment the ActiveRecord belongs_to to also define rid accessors. For example: if you blog post belongs_to an
35
+ # author, on top of the methods #author, #author=, #author_id and #author_id=, it'll also have #author_rid and
36
+ # #author_rid= that allow you to retrieve the RID of the author or set another author by using the RID.
37
+ #
38
+ # @param attrs [Array] same as the parameters for ActiveRecord::Associations::ClassMethods.belongs_to except that
39
+ # passing rid: false will prevent the rid accessors from beign defined.
40
+ # @see ActiveRecord::Associations::ClassMethods.belongs_to
31
41
  def belongs_to(*attrs)
32
42
  define_rid_method = attrs[1].try(:delete, :rid)
33
43
  super.tap do
@@ -71,6 +81,11 @@ module RandomUniqueId
71
81
 
72
82
  private
73
83
 
84
+ # Defines the setter and getter for the RID of a relationship.
85
+ #
86
+ # @param related_class [Class] class in which the RID methods are going to be defined.
87
+ # @param relationship_name [String] name of the relationship for which the RID methods are going to be defined.
88
+ # @see RandomUniqueId::ClassMethods.belongs_to
74
89
  def define_rid_accessors(related_class, relationship_name)
75
90
  define_method("#{relationship_name}_rid") do
76
91
  self.send(relationship_name).try(:rid)
@@ -84,6 +99,13 @@ module RandomUniqueId
84
99
  end
85
100
  end
86
101
 
102
+ # Generate and store the random unique id for the object.
103
+ #
104
+ # @param n [Integer] how long should the random string be.
105
+ # @param field [String] name of the field that contains the rid.
106
+ # @return [String] the random string.
107
+ # @see RandomUniqueId::ClassMethods#has_random_unique_id
108
+ # @see RandomUniqueId.generate_random_id
87
109
  def generate_random_unique_id(n=5, field="rid")
88
110
  # Find the topmost class before ActiveRecord::Base so that when we do queries, we don't end up with type=Whatever in the where clause.
89
111
  klass = self.class
@@ -102,6 +124,11 @@ module RandomUniqueId
102
124
  end while klass.unscoped.where(field => self.send(field)).exists?
103
125
  end
104
126
 
127
+ # By a cunning use of SecureRandom.urlsafe_base64, quickly generate an alphanumeric random string.
128
+ #
129
+ # @param n [Integer] how long should the random string be.
130
+ # @return [String] the random string.
131
+ # @see RandomUniqueId#generate_random_unique_id
105
132
  def self.generate_random_id(n=10)
106
133
  # IMPORTANT: don't ever generate dashes or underscores in the RIDs as they are likely to end up in the UI in Rails
107
134
  # and they'll be converted to something else by jquery ujs or something like that.
@@ -0,0 +1,38 @@
1
+ # Copyright © 2014, Watu
2
+
3
+ # Some utilities that might be useful if you are using RandomUniqueId. This file needs to be explicitely included:
4
+ #
5
+ # require "random_unique_id/util"
6
+ module RandomUniqueId::Util
7
+ # Set the initial automatic ids on the database (also known as sequences) to a higher number than 1. The reason to use
8
+ # this is in case you want to test for id leakage in HTML. For example, you create a blog post, display it, and search
9
+ # the HTML for the id of the blog post, author, featured image, and so on. If these ids are low, like 1 or 10, the
10
+ # likelihood of false positives is very high, but if they are larger numbers, like 10001, then it becomes very
11
+ # unlikely (but not impossible).
12
+ #
13
+ # @param [Hash] options
14
+ # @option options [Boolean] :verbose whether to print messages to the console about what's going on or not.
15
+ # @option options [Integer] :initial_id initial id at which the sequences will be set.
16
+ def self.set_initial_ids(options={})
17
+ options.reverse_merge!(verbose: false, initial_id: 10000)
18
+ verbose = options[:verbose]
19
+ initial_id = options[:initial_id]
20
+
21
+ sql_connection = ActiveRecord::Base.connection
22
+ case sql_connection.adapter_name
23
+ when "PostgreSQL"
24
+ puts "== Setting initial ids to #{initial_id} or the next available one".ljust(79, "=") if verbose
25
+ sequences = sql_connection.select_values("SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'")
26
+ sequences.each do |sequence|
27
+ print "-- Setting initial id for #{sequence} to..." if verbose
28
+ next_id = [initial_id, sql_connection.select_values("SELECT nextval('#{sequence}')").first.to_i].max
29
+ print "#{next_id}..." if verbose
30
+ sql_connection.execute("SELECT setval('#{sequence}', #{next_id})")
31
+ puts " done." if verbose
32
+ end
33
+ puts "== Setting initial ids to #{initial_id} (done) ".ljust(79, "=") if verbose
34
+ else
35
+ raise "Don't know how to set initial ids for #{sql_connection.adapter_name}. Would you like to contribute? https://github.com/watu/random_unique_id"
36
+ end
37
+ end
38
+ end
@@ -2,5 +2,5 @@
2
2
  # Copyright © 2013, 2014, Watu
3
3
 
4
4
  module RandomUniqueId
5
- VERSION = "0.2.1"
5
+ VERSION = "1.0.0"
6
6
  end
@@ -20,6 +20,19 @@ Gem::Specification.new do |spec|
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_dependency "activesupport", "> 3.2.0"
24
- spec.add_dependency "activerecord", "> 3.2.0"
23
+ spec.required_ruby_version = ">= 1.9.3"
24
+ spec.add_dependency "activesupport", ">= 3.2.0"
25
+ spec.add_dependency "activerecord", ">= 3.2.0"
26
+
27
+ spec.add_development_dependency "appraisal"
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "coveralls" #, require: false
30
+ spec.add_development_dependency "minitest"
31
+ spec.add_development_dependency "minitest-reporters"
32
+ spec.add_development_dependency "minitest-rails"
33
+ spec.add_development_dependency "mocha"
34
+ spec.add_development_dependency "rake"
35
+ spec.add_development_dependency "simplecov"
36
+ spec.add_development_dependency "shoulda"
37
+ spec.add_development_dependency "sqlite3"
25
38
  end
@@ -0,0 +1,28 @@
1
+ # Copyright © 2014, Watu
2
+
3
+ require_relative "../test_helper"
4
+
5
+ require "random_unique_id/util"
6
+
7
+ class RandomUniqueId::UtilTest < MiniTest::Unit::TestCase
8
+ should "raise an exception for unknown adapters" do
9
+ sql_connection = stub("connection", adapter_name: "dBASE")
10
+ ActiveRecord::Base.expects(:connection).returns(sql_connection)
11
+ assert_raises RuntimeError do
12
+ RandomUniqueId::Util.set_initial_ids
13
+ end
14
+ end
15
+
16
+ should "set initial ids in PostgreSQL" do
17
+ sql_connection = mock("connection")
18
+ sql_connection.stubs(:adapter_name).returns("PostgreSQL")
19
+ sequences = ["sequence_1", "sequence_2"]
20
+ sql_connection.expects(:select_values).with("SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'").returns(sequences)
21
+ sql_connection.expects(:select_values).with("SELECT nextval('sequence_1')").returns(["1"])
22
+ sql_connection.expects(:execute).with("SELECT setval('sequence_1', 10000)")
23
+ sql_connection.expects(:select_values).with("SELECT nextval('sequence_2')").returns(["20000"])
24
+ sql_connection.expects(:execute).with("SELECT setval('sequence_2', 20000)")
25
+ ActiveRecord::Base.expects(:connection).returns(sql_connection)
26
+ RandomUniqueId::Util.set_initial_ids
27
+ end
28
+ end
@@ -14,26 +14,23 @@ Coveralls.wear! # Comment out this line to have the local coverage generated.
14
14
  require "minitest/autorun"
15
15
  require "minitest/reporters"
16
16
  MiniTest::Reporters.use!
17
-
18
- # This class is here only to trick shoulda into attaching itself to MiniTest due to: https://github.com/thoughtbot/shoulda-context/issues/38
19
- module ActiveSupport
20
- class TestCase < MiniTest::Unit::TestCase
21
- end
22
- end
17
+ require "active_support/test_case"
23
18
  require "shoulda"
24
19
  require "shoulda-context"
25
20
  require "shoulda-matchers"
26
-
27
21
  require "mocha/setup"
28
22
 
23
+ # Make the code to be tested easy to load.
24
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
25
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
26
+
29
27
  # Database setup
30
28
  require "active_record"
31
29
  require "logger"
32
30
  ActiveRecord::Base.logger = Logger.new(STDERR)
33
31
  ActiveRecord::Base.logger.level = Logger::WARN
34
32
  ActiveRecord::Base.configurations = {"sqlite3" => {adapter: "sqlite3", database: ":memory:"}}
35
- ActiveRecord::Base.establish_connection("sqlite3")
33
+ ActiveRecord::Base.establish_connection(:sqlite3)
36
34
 
37
- # Make the code to be tested easy to load.
38
- $LOAD_PATH.unshift(File.dirname(__FILE__))
39
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
35
+ # Shutup.
36
+ I18n.enforce_available_locales = false # TODO: remove this line when it's not needed anymore.
metadata CHANGED
@@ -1,43 +1,197 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_unique_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Pablo Fernández
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-27 00:00:00.000000000 Z
11
+ date: 2014-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: appraisal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: mocha
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rake
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: shoulda
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: sqlite3
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
41
195
  description: Generate random but unique ids for your active record records.
42
196
  email:
43
197
  - pupeno@watuapp.com
@@ -47,15 +201,23 @@ extra_rdoc_files: []
47
201
  files:
48
202
  - ".gitignore"
49
203
  - ".travis.yml"
204
+ - Appraisals
205
+ - CHANGELOG.md
50
206
  - Gemfile
51
207
  - LICENSE.txt
52
208
  - README.md
53
209
  - Rakefile
54
- - gemfiles/rails_3_2.Gemfile
55
- - gemfiles/rails_4_0.Gemfile
210
+ - gemfiles/rails_3_2.gemfile
211
+ - gemfiles/rails_3_2.gemfile.lock
212
+ - gemfiles/rails_4_0.gemfile
213
+ - gemfiles/rails_4_0.gemfile.lock
214
+ - gemfiles/rails_4_1.gemfile
215
+ - gemfiles/rails_4_1.gemfile.lock
56
216
  - lib/random_unique_id.rb
217
+ - lib/random_unique_id/util.rb
57
218
  - lib/random_unique_id/version.rb
58
219
  - random_unique_id.gemspec
220
+ - test/random_unique_id/util_test.rb
59
221
  - test/random_unique_id_test.rb
60
222
  - test/test_helper.rb
61
223
  homepage: https://github.com/watu/random_unique_id
@@ -70,7 +232,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
232
  requirements:
71
233
  - - ">="
72
234
  - !ruby/object:Gem::Version
73
- version: '0'
235
+ version: 1.9.3
74
236
  required_rubygems_version: !ruby/object:Gem::Requirement
75
237
  requirements:
76
238
  - - ">="
@@ -83,5 +245,6 @@ signing_key:
83
245
  specification_version: 4
84
246
  summary: Generate random but unique ids for your active record records.
85
247
  test_files:
248
+ - test/random_unique_id/util_test.rb
86
249
  - test/random_unique_id_test.rb
87
250
  - test/test_helper.rb
@@ -1,16 +0,0 @@
1
- # encoding: UTF-8
2
- # Copyright © 2013, 2014, Watu
3
-
4
- source "https://rubygems.org"
5
-
6
- gem "bundler", "~> 1.3"
7
- gem "coveralls", require: false
8
- gem "minitest", "~> 4.7.5"
9
- gem "minitest-reporters"
10
- gem "mocha"
11
- gem "rake"
12
- gem "simplecov"
13
- gem "shoulda"
14
- gem "sqlite3"
15
- gem "activesupport", "~> 3.2.0"
16
- gem "activerecord", "~> 3.2.0"
@@ -1,16 +0,0 @@
1
- # encoding: UTF-8
2
- # Copyright © 2013, 2014, Watu
3
-
4
- source "https://rubygems.org"
5
-
6
- gem "bundler", "~> 1.3"
7
- gem "coveralls", require: false
8
- gem "minitest"
9
- gem "minitest-reporters"
10
- gem "mocha"
11
- gem "rake"
12
- gem "simplecov"
13
- gem "shoulda"
14
- gem "sqlite3"
15
- gem "activesupport", "~> 4.0.0"
16
- gem "activerecord", "~> 4.0.0"