random_unique_id 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/Gemfile +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +41 -3
- data/Rakefile +1 -1
- data/gemfiles/rails_3_2.Gemfile +2 -2
- data/gemfiles/rails_4_0.Gemfile +1 -1
- data/lib/random_unique_id.rb +39 -1
- data/lib/random_unique_id/version.rb +2 -2
- data/random_unique_id.gemspec +1 -1
- data/test/random_unique_id_test.rb +17 -1
- data/test/test_helper.rb +7 -4
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f1c4b031d065a8bec8f97393e82ad781fc33aee
|
4
|
+
data.tar.gz: 1e0fc4defa8a5dc2d6bef5f04739b23372ace1a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76166e35364c68b99a623c9018efc28da9af99026d4a7f69a4886fcf61e1ce82604c59322f969c6d0c657a543bd2994afa004471dc720b43bd4391658a3c08e7
|
7
|
+
data.tar.gz: e32b66eebcec4dc4649153f2fb6be5235b8868a5dd3613426969feb29b7a8e7e484735cef743a3a465a6dedd7257bdda19cef60eda0e4c43d6d90b29a46ec643
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
# Copyright © 2013, Watu
|
2
|
+
# Copyright © 2013, 2014, Watu
|
3
3
|
|
4
4
|
source "https://rubygems.org"
|
5
5
|
|
6
6
|
gem "bundler", "~> 1.3"
|
7
7
|
gem "coveralls", require: false
|
8
|
-
gem "minitest"
|
8
|
+
gem "minitest", "~> 4.7.5"
|
9
9
|
gem "minitest-reporters"
|
10
10
|
gem "mocha"
|
11
11
|
gem "rake"
|
12
12
|
gem "simplecov"
|
13
13
|
gem "shoulda"
|
14
14
|
gem "sqlite3"
|
15
|
-
gem "activesupport", "~>
|
16
|
-
gem "activerecord", "~>
|
15
|
+
gem "activesupport", "~> 3.2.0"
|
16
|
+
gem "activerecord", "~> 3.2.0"
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -2,11 +2,16 @@
|
|
2
2
|
|
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
|
+
[![Code Climate](https://codeclimate.com/github/watu/random_unique_id.png)](https://codeclimate.com/github/watu/random_unique_id)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/random_unique_id.png)](http://badge.fury.io/rb/random_unique_id)
|
5
7
|
|
6
8
|
This gem will generate a random unique id for your active record records that you can use instead of their actual ID for
|
7
9
|
all external interactions with users. The goal is for you to be able to hide how many records you have, for business
|
8
10
|
purposes, but also to make IDs non-predictable.
|
9
11
|
|
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
|
+
|
10
15
|
## Installation
|
11
16
|
|
12
17
|
Add this line to your application's Gemfile:
|
@@ -34,10 +39,43 @@ For example:
|
|
34
39
|
has_random_unique_id
|
35
40
|
end
|
36
41
|
|
37
|
-
You need to also add a column, called `rid` of type string/varchar. It is recommended that you also add
|
38
|
-
column
|
42
|
+
You need to also add a column, called `rid` of type string/varchar. It is recommended that you also add a unique index
|
43
|
+
on that column, for example:
|
44
|
+
|
45
|
+
def up
|
46
|
+
add_column :posts, :rid, :string
|
47
|
+
add_index :posts, :rid, :unique
|
48
|
+
end
|
49
|
+
|
50
|
+
The method `to_param` will be overridden to return the `rid` instead of the `id`. The method `belongs_to` gets extended
|
51
|
+
to define `_rid` methods similar to the `_id` method, like: `blog_rid` and `blog_rid=`. If you don't want to define
|
52
|
+
those pass `define_rid_method` as false, for example:
|
53
|
+
|
54
|
+
class Post
|
55
|
+
belongs_to :blog, define_rid_method: false
|
56
|
+
end
|
57
|
+
|
58
|
+
Classes that have rids also get a method called `populate_random_unique_ids` to help you populate the rid of existing
|
59
|
+
records. For example:
|
60
|
+
|
61
|
+
def up
|
62
|
+
add_column :posts, :rid, :string
|
63
|
+
add_index :posts, :rid, :unique
|
64
|
+
say_with_time "Post.populate_random_unique_ids" do
|
65
|
+
Post.reset_column_information
|
66
|
+
Post.populate_random_unique_ids { print "."}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
## Changelog
|
71
|
+
|
72
|
+
### Version 0.2.0
|
73
|
+
- Added method populate_random_unique_ids.
|
74
|
+
- Improved documentation
|
75
|
+
- Started testing with Ruby 2.1.
|
39
76
|
|
40
|
-
|
77
|
+
### Version 0.1.0
|
78
|
+
- Initial release of the code extracted from [Watu](http://github.com/watu).
|
41
79
|
|
42
80
|
## Contributing
|
43
81
|
|
data/Rakefile
CHANGED
data/gemfiles/rails_3_2.Gemfile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
# Copyright © 2013, Watu
|
2
|
+
# Copyright © 2013, 2014, Watu
|
3
3
|
|
4
4
|
source "https://rubygems.org"
|
5
5
|
|
6
6
|
gem "bundler", "~> 1.3"
|
7
7
|
gem "coveralls", require: false
|
8
|
-
gem "minitest", "~>
|
8
|
+
gem "minitest", "~> 4.7.5"
|
9
9
|
gem "minitest-reporters"
|
10
10
|
gem "mocha"
|
11
11
|
gem "rake"
|
data/gemfiles/rails_4_0.Gemfile
CHANGED
data/lib/random_unique_id.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
# Copyright © 2011, 2012, 2013, Watu
|
2
|
+
# Copyright © 2011, 2012, 2013, 2014, Watu
|
3
3
|
|
4
4
|
require "random_unique_id/version"
|
5
5
|
require "securerandom"
|
@@ -10,6 +10,18 @@ module RandomUniqueId
|
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
+
# Mark a model as containing a random unique id. A field called rid of type string is required. It's recommended
|
14
|
+
# that it's indexed and unique. For example, you could add it to a migration like this:
|
15
|
+
# def up
|
16
|
+
# add_column :posts, :rid, :string
|
17
|
+
# add_index :posts, :rid, :unique
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# and then to the model like this:
|
21
|
+
# class Post
|
22
|
+
# has_random_unique_id
|
23
|
+
# # ... other stuff
|
24
|
+
# end
|
13
25
|
def has_random_unique_id
|
14
26
|
validates :rid, presence: true, uniqueness: true
|
15
27
|
before_validation :generate_random_unique_id, if: Proc.new { |r| r.rid.blank? }
|
@@ -42,6 +54,32 @@ module RandomUniqueId
|
|
42
54
|
end
|
43
55
|
end
|
44
56
|
end
|
57
|
+
|
58
|
+
# Populate all the blank rids in a table. This is useful when adding rids to a table that already has data in it.
|
59
|
+
# For example:
|
60
|
+
# def up
|
61
|
+
# add_column :posts, :rid, :string
|
62
|
+
# add_index :posts, :rid, :unique
|
63
|
+
# say_with_time "Post.populate_random_unique_ids" do
|
64
|
+
# Post.reset_column_information
|
65
|
+
# Post.populate_random_unique_ids { print "."}
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# This method uses update_column to avoid running validations and callbacks. It will not change existing rids, so
|
70
|
+
# it's safe to call several times and a failure (even without a transaction) is not catastrophic.
|
71
|
+
def populate_random_unique_ids
|
72
|
+
find_each do |record|
|
73
|
+
rid_just_populated = false
|
74
|
+
if record.rid.blank?
|
75
|
+
record.generate_random_unique_id
|
76
|
+
record.update_column(:rid, record.rid)
|
77
|
+
rid_just_populated = true
|
78
|
+
end
|
79
|
+
yield(record, rid_just_populated) if block_given?
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
45
83
|
end
|
46
84
|
|
47
85
|
def generate_random_unique_id(n=5, field="rid")
|
data/random_unique_id.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
# Copyright © 2011, 2012, 2013, Watu
|
2
|
+
# Copyright © 2011, 2012, 2013, 2014, Watu
|
3
3
|
|
4
4
|
require_relative "test_helper"
|
5
5
|
|
@@ -8,6 +8,7 @@ require "random_unique_id"
|
|
8
8
|
ActiveRecord::Schema.define(version: 0) do
|
9
9
|
create_table :blogs do |t|
|
10
10
|
t.string :rid
|
11
|
+
t.string :name
|
11
12
|
end
|
12
13
|
add_index :blogs, :rid, unique: true
|
13
14
|
|
@@ -77,5 +78,20 @@ class RandomUniqueIdTest < MiniTest::Unit::TestCase
|
|
77
78
|
assert_equal blog, @text_post.blog
|
78
79
|
assert_equal blog.rid, @text_post.blog_rid
|
79
80
|
end
|
81
|
+
|
82
|
+
should "populate a table with rids" do
|
83
|
+
# Create a bunch of blogs without rid by manually inserting them into the talbe.
|
84
|
+
rid_less_records = 10
|
85
|
+
5.times { Blog.create! }
|
86
|
+
existing_rids = Blog.all.map(&:rid).compact
|
87
|
+
rid_less_records.times { Blog.connection.execute("INSERT INTO blogs (name) VALUES ('Blag')") }
|
88
|
+
assert_equal rid_less_records, Blog.where(:rid => nil).count # Just to be sure this test is being effective.
|
89
|
+
|
90
|
+
rids_populated = 0
|
91
|
+
Blog.populate_random_unique_ids { |_, rid_just_populated| rids_populated += 1 if rid_just_populated }
|
92
|
+
assert_equal rid_less_records, rids_populated
|
93
|
+
assert_equal 0, Blog.where(:rid => nil).count
|
94
|
+
assert_equal existing_rids.count, Blog.where(:rid => existing_rids).count # Make sure the existing rids where not touched.
|
95
|
+
end
|
80
96
|
end
|
81
97
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
# Copyright © 2013, Watu
|
2
|
+
# Copyright © 2013, 2014, Watu
|
3
3
|
|
4
4
|
require "rubygems"
|
5
5
|
|
6
|
+
# Test coverage
|
6
7
|
require "simplecov"
|
8
|
+
require "coveralls"
|
7
9
|
SimpleCov.start do
|
8
10
|
add_filter "/test/"
|
9
11
|
end
|
12
|
+
Coveralls.wear! # Comment out this line to have the local coverage generated.
|
10
13
|
|
11
14
|
require "minitest/autorun"
|
12
15
|
require "minitest/reporters"
|
@@ -23,14 +26,14 @@ require "shoulda-matchers"
|
|
23
26
|
|
24
27
|
require "mocha/setup"
|
25
28
|
|
29
|
+
# Database setup
|
26
30
|
require "active_record"
|
31
|
+
require "logger"
|
27
32
|
ActiveRecord::Base.logger = Logger.new(STDERR)
|
28
33
|
ActiveRecord::Base.logger.level = Logger::WARN
|
29
34
|
ActiveRecord::Base.configurations = {"sqlite3" => {adapter: "sqlite3", database: ":memory:"}}
|
30
35
|
ActiveRecord::Base.establish_connection("sqlite3")
|
31
36
|
|
32
|
-
|
33
|
-
Coveralls.wear!
|
34
|
-
|
37
|
+
# Make the code to be tested easy to load.
|
35
38
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
36
39
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_unique_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2014-04-05 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
41
|
description: Generate random but unique ids for your active record records.
|
@@ -45,8 +45,8 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
-
- .gitignore
|
49
|
-
- .travis.yml
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
50
|
- Gemfile
|
51
51
|
- LICENSE.txt
|
52
52
|
- README.md
|
@@ -68,20 +68,21 @@ require_paths:
|
|
68
68
|
- lib
|
69
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
70
|
requirements:
|
71
|
-
- -
|
71
|
+
- - ">="
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
75
|
requirements:
|
76
|
-
- -
|
76
|
+
- - ">="
|
77
77
|
- !ruby/object:Gem::Version
|
78
78
|
version: '0'
|
79
79
|
requirements: []
|
80
80
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.
|
81
|
+
rubygems_version: 2.2.2
|
82
82
|
signing_key:
|
83
83
|
specification_version: 4
|
84
84
|
summary: Generate random but unique ids for your active record records.
|
85
85
|
test_files:
|
86
86
|
- test/random_unique_id_test.rb
|
87
87
|
- test/test_helper.rb
|
88
|
+
has_rdoc:
|