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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a7b92c76f522b3336630120ac9d9b882eaef76c
4
- data.tar.gz: b5664b1d2c1bbb5942c5a94fbc89e66b8eb7c61d
3
+ metadata.gz: 5f1c4b031d065a8bec8f97393e82ad781fc33aee
4
+ data.tar.gz: 1e0fc4defa8a5dc2d6bef5f04739b23372ace1a0
5
5
  SHA512:
6
- metadata.gz: f7ccd53a8efdbc946946a1be8180bae4b5b62c1325591a129d5b1d1a9d636d2e8e2fffc5044230edb540cd05bd7b61deae7b5fc047663b9346cc42da3cc42647
7
- data.tar.gz: 5115455385496f828995823603bc4a5caa92d14e7688c65a8e28173579d1805e3836aab7d77936453b03eea96ad02377cb53e41f898493906202e2043e10de8d
6
+ metadata.gz: 76166e35364c68b99a623c9018efc28da9af99026d4a7f69a4886fcf61e1ce82604c59322f969c6d0c657a543bd2994afa004471dc720b43bd4391658a3c08e7
7
+ data.tar.gz: e32b66eebcec4dc4649153f2fb6be5235b8868a5dd3613426969feb29b7a8e7e484735cef743a3a465a6dedd7257bdda19cef60eda0e4c43d6d90b29a46ec643
@@ -2,6 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - "1.9.3"
4
4
  - "2.0.0"
5
+ - "2.1.1"
5
6
  gemfile:
6
7
  - gemfiles/rails_3_2.Gemfile
7
8
  - gemfiles/rails_4_0.Gemfile
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", "~> 4.0.0"
16
- gem "activerecord", "~> 4.0.0"
15
+ gem "activesupport", "~> 3.2.0"
16
+ gem "activerecord", "~> 3.2.0"
@@ -1,4 +1,4 @@
1
- Copyright © 2013, Watu
1
+ Copyright © 2013, 2014, Watu
2
2
 
3
3
  MIT License
4
4
 
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 an index on that
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
- The method to_param will be overridden to return the rid instead of the id.
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
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
- # Copyright © 2013, Watu
2
+ # Copyright © 2013, 2014, Watu
3
3
 
4
4
  require "bundler/gem_tasks"
5
5
 
@@ -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", "~> 2.5.1"
8
+ gem "minitest", "~> 4.7.5"
9
9
  gem "minitest-reporters"
10
10
  gem "mocha"
11
11
  gem "rake"
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
- # Copyright © 2013, Watu
2
+ # Copyright © 2013, 2014, Watu
3
3
 
4
4
  source "https://rubygems.org"
5
5
 
@@ -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")
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
- # Copyright © 2013, Watu
2
+ # Copyright © 2013, 2014, Watu
3
3
 
4
4
  module RandomUniqueId
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
- # Copyright © 2013, Watu
2
+ # Copyright © 2013, 2014, Watu
3
3
 
4
4
  lib = File.expand_path("../lib", __FILE__)
5
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
@@ -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
@@ -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
- require "coveralls"
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.1.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-01-06 00:00:00.000000000 Z
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.1.11
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: