public_uid 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YmE5ZTQ4YjY0ZjYyYzBhZjQ3ODE1YWFkMGI3NTEwZGFmYTM2NmNiMg==
5
- data.tar.gz: !binary |-
6
- YTM1ZGMyMmNlMjJjMGIwOWJkNGIyMTc0NjRlNWU0OGI2ODJhZjA5OQ==
2
+ SHA1:
3
+ metadata.gz: 7323c6067a97bf3479759130f27f3ab6a2629084
4
+ data.tar.gz: 2a5aa34772776cc391d103d9f1ecb776899f0b0e
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZWQ5NjBlZDA3YWU5ODc4NzYzY2EzNTI3Y2Y2Y2I5Y2RhMDAxYWJiMTZkMzMz
10
- ZjI4OTgwNDhmZmY3ZDNhNGY3Y2EyNmZjZDMzMWQ2NDE5NDg1NGFlODhjMGQy
11
- N2ZiNmQwYTVlOGFmNGY1MjdkYmU4ZGY0NGE5Yjg5YTNjYjdmODU=
12
- data.tar.gz: !binary |-
13
- NGJjOGNlZDg2M2NlMDY1YTczN2JmZTA3MDQ4NjdmODE1OTM2MTZlNjY2Y2Fj
14
- OWNhODQwZjk3YTIzOGY0NGNkM2U0YjA5YTYzZWRkZTE0NTgwNGU1YTg2NTlh
15
- YjUyZmRjOTY5OTU3YzNhZmRlMzIyM2ZkMDgxZGI2NjE5Mzk5NWY=
6
+ metadata.gz: 51f04e7f8a1331cacee37033b013237a49cbd9946d9f564998705c4ba5be1a1a2e16960a09d9de298579178cc97bcb612d361301795aa80c21178974eb4ff4c9
7
+ data.tar.gz: 935f54f79fcd7b681e8ad76f28a5b98d45810236ed2fc86862a6a38dad9e1d1b0a30c13a6815a460d27ffc45c3411ac55b839236c043dc67c5ead9e1eee9d49e
data/.gitignore CHANGED
@@ -2,6 +2,7 @@
2
2
  *.rbc
3
3
  .bundle
4
4
  .config
5
+ .idea
5
6
  .yardoc
6
7
  Gemfile.lock
7
8
  InstalledFiles
@@ -15,5 +16,5 @@ spec/reports
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
18
- .ruby-version
19
- .ruby-gemset
19
+ .ruby-version*
20
+ .ruby-gemset*
@@ -1,8 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
5
4
  - 2.1.0
6
5
  - 2.1.1
7
6
  - 2.2.2
7
+ - 2.3.0
8
8
  - ruby-head
data/Gemfile CHANGED
File without changes
File without changes
data/README.md CHANGED
@@ -77,6 +77,34 @@ u.save! #=> true
77
77
  u.public_uid #=> "aeuhsthi"
78
78
  ```
79
79
 
80
+ Then you can do more clever things like having urls based on `public_uid` and `title` (http://blog.teamtreehouse.com/creating-vanity-urls-in-rails)
81
+
82
+ ```ruby
83
+ # app/models/user.rb
84
+ class User < ActiveRecord::Base
85
+ generate_public_uid
86
+
87
+ def self.find_param(param)
88
+ find_by! public_uid: param.split('-').first
89
+ end
90
+
91
+ def to_param
92
+ "#{public_uid}-#{tile.gsub(/\s/,'-')}"
93
+ end
94
+ end
95
+
96
+ # app/controllers/users_controller.rb
97
+ class UsersController < ActionController::Base
98
+ # ...
99
+ def show
100
+ @user = User.find_param(param[:id])
101
+ # ...
102
+ end
103
+ # ...
104
+ end
105
+ ```
106
+
107
+
80
108
  If you want to use different column just specify column option:
81
109
 
82
110
  ```ruby
@@ -177,6 +205,58 @@ Model ValidationType: generating public_uids for missing records
177
205
  * generating 0 public_uid(s)
178
206
  ```
179
207
 
208
+ ## Alternatives
209
+
210
+ There is a lot of reasons behind the existance of this gem, we are fully aware there are "built" in alternatives it's just they don't quite fit the purpouse of Developer to chose char range himself.
211
+
212
+ Anyway if you hate our guts for writing this gem try something like:
213
+
214
+ #### UUID
215
+
216
+ ```ruby
217
+ class Post < ActiveRecord::Base
218
+ before_create :generate_random_id
219
+
220
+ private
221
+ def generate_random_id
222
+ self.id = SecureRandom.uuid
223
+ end
224
+ end
225
+ ```
226
+
227
+ Or if you are using Rails >= 4 and PostgreSQL, you can have it generating them for you :
228
+
229
+ ```ruby
230
+ create_table :posts, id: :uuid do |t|
231
+ ...
232
+ end
233
+ ```
234
+
235
+ * [Source](http://stackoverflow.com/a/34679841)
236
+
237
+ #### SecureRandom.random_number
238
+
239
+ ```ruby
240
+ class MyModel < ActiveRecord::Base
241
+ before_create :randomize_id
242
+
243
+ private
244
+ def randomize_id
245
+ begin
246
+ self.id = SecureRandom.random_number(1_000_000)
247
+ end while Model.where(id: self.id).exists?
248
+ end
249
+ end
250
+
251
+ ```
252
+
253
+ Or you can use `SecureRandom.hex`
254
+
255
+ In future gem version we will actually introduce this two generators.
256
+
257
+ * [Source](http://stackoverflow.com/a/13680914)
258
+
259
+
180
260
  ## Contributing
181
261
 
182
262
  1. Fork it
data/Rakefile CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -27,7 +27,7 @@ module PublicUid
27
27
  private
28
28
 
29
29
  def similar_uid_exist?
30
- @klass.where(public_uid: new_uid).count > 0
30
+ @klass.where(@column => new_uid).count > 0
31
31
  end
32
32
 
33
33
  def check_column_existance
File without changes
@@ -1,20 +1,21 @@
1
+ require 'rake'
2
+
1
3
  namespace :public_uid do
2
- desc "Generate public_uid on Models that have public_uid column on records that have nil public_uid"
4
+ desc "Generate public_uid on Models that have public_uid column on records that have nil value"
3
5
  task :generate => :environment do
4
- Rails.application.eager_load!
5
6
  ActiveRecord::Base.descendants.each do |model|
6
- model.connection # establis conection
7
+ model.connection # establish conection
7
8
 
8
9
  if model.instance_methods.collect(&:to_s).include?('generate_uid')
9
- puts "Model #{model.name}: generating public_uids for missing records"
10
-
10
+ puts "Model #{model.name}:"
11
+ uid_column_name = model.public_uid_column
11
12
  model
12
- .where(public_uid: nil)
13
- .tap { |scope| puts " * generating #{scope.count} public_uid(s)" }
14
- .each do |record_without_public_uid|
15
- record_without_public_uid.generate_uid
16
- record_without_public_uid.save!
17
- end
13
+ .where(uid_column_name => nil)
14
+ .tap { |scope| puts " * generating #{scope.count} #{uid_column_name}(s) for #{model.table_name}" }
15
+ .find_each do |record_without_public_uid|
16
+ record_without_public_uid.generate_uid
17
+ record_without_public_uid.save!
18
+ end
18
19
  puts ''
19
20
  end
20
21
  end
@@ -1,3 +1,3 @@
1
1
  module PublicUid
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.2"
3
3
  end
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "minitest", "~> 5"
25
25
  spec.add_development_dependency "rr", "~> 1.1.2"
26
26
  spec.add_development_dependency "sqlite3"
27
- spec.add_development_dependency "activerecord", '~> 3.2'
27
+ spec.add_development_dependency "activerecord", '~> 4.2' # ensures compatibility for ruby 2.0.0+ to head
28
28
  end
File without changes
File without changes
@@ -21,7 +21,7 @@ TestConf.orm_modules.each do |orm_module|
21
21
  let(:options) { {record: record, column: :public_uid} }
22
22
  let(:instance) { PublicUid::SetPublicUid.new options }
23
23
  let(:record) { record_class.new }
24
- let(:record_class) { "#{orm_module}::User".constantize }
24
+ let(:record_class) { "#{orm_module}::ModelWithGeneratorDefaults".constantize }
25
25
 
26
26
  describe 'initialization' do
27
27
  context 'when column not specified' do
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+ require 'rake'
3
+
4
+ describe 'Generate' do
5
+ before :all do
6
+ # load File.expand_path('../../../../lib/public_uid/tasks/generate.rake', __FILE__)
7
+ Rake.application.rake_require 'public_uid/tasks/generate'
8
+ Rake::Task.define_task(:environment)
9
+ end
10
+
11
+ TestConf.orm_modules.each do |orm_module|
12
+ describe orm_module.description do
13
+ {
14
+ "#{orm_module}::ModelWithGeneratorDefaults".constantize => :public_uid,
15
+ "#{orm_module}::ModelWithCustomGererator".constantize => :public_uid,
16
+ "#{orm_module}::CustomPublicUidColumnModel".constantize => :custom_uid,
17
+ }.each_pair do |model, uid_column|
18
+
19
+ context "For #{model.name} with #{uid_column}" do
20
+ let(:run_generate_task) do
21
+ Rake::Task['public_uid:generate'].reenable
22
+ Rake.application.invoke_task 'public_uid:generate'
23
+ end
24
+
25
+ describe '#invoke' do
26
+ it 'generates uid' do
27
+ user = model.new
28
+ user.save!
29
+ initial_uid = user.send(uid_column)
30
+
31
+ # simulate an existing record with no uid value
32
+ user.update_attribute(uid_column, nil).must_equal true
33
+ user.send(uid_column).must_be_nil
34
+
35
+ # run the rake task
36
+ run_generate_task
37
+
38
+ user.reload
39
+ user.send(uid_column).must_be_kind_of String
40
+ user.send(uid_column).wont_be_empty
41
+ user.send(uid_column).wont_be_same_as initial_uid
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ end
@@ -10,6 +10,14 @@ TestConf.orm_modules.each do |orm_module|
10
10
 
11
11
  context 'in new record' do
12
12
  it{ subject.must_be_nil }
13
+
14
+ describe '#generate_uid' do
15
+ before do
16
+ user.generate_uid
17
+ end
18
+
19
+ it { subject.wont_be_nil }
20
+ end
13
21
  end
14
22
 
15
23
  context 'after save' do
@@ -10,6 +10,14 @@ TestConf.orm_modules.each do |orm_module|
10
10
 
11
11
  context 'in new record' do
12
12
  it{ subject.must_be_nil }
13
+
14
+ describe '#generate_uid' do
15
+ before do
16
+ user.generate_uid
17
+ end
18
+
19
+ it { subject.wont_be_nil }
20
+ end
13
21
  end
14
22
 
15
23
  context 'after save' do
@@ -10,6 +10,14 @@ TestConf.orm_modules.each do |orm_module|
10
10
 
11
11
  context 'in new record' do
12
12
  it{ subject.must_be_nil }
13
+
14
+ describe '#generate_uid' do
15
+ before do
16
+ user.generate_uid
17
+ end
18
+
19
+ it { subject.wont_be_nil }
20
+ end
13
21
  end
14
22
 
15
23
  context 'after save' do
@@ -1,5 +1,9 @@
1
1
  ActiveRecord::Base::establish_connection(adapter: 'sqlite3', database: ':memory:')
2
- ActiveRecord::Base.connection.execute(%{CREATE TABLE users (id INTEGER PRIMARY KEY, public_uid VARCHAR, custom_uid VARCHAR);})
2
+ # creates 3 `users` tables, with either `public_uid` or `custom_uid` (but not both), or no uid column,
3
+ # to expose potential `SQLite3::SQLException: no such column` error.
4
+ ActiveRecord::Base.connection.execute(%{CREATE TABLE users (id INTEGER PRIMARY KEY);})
5
+ ActiveRecord::Base.connection.execute(%{CREATE TABLE user_puids (id INTEGER PRIMARY KEY, public_uid VARCHAR);})
6
+ ActiveRecord::Base.connection.execute(%{CREATE TABLE user_cuids (id INTEGER PRIMARY KEY, custom_uid VARCHAR);})
3
7
 
4
8
  module ActRec
5
9
  def self.description
@@ -7,12 +11,12 @@ module ActRec
7
11
  end
8
12
 
9
13
  class CustomPublicUidColumnModel < ActiveRecord::Base
10
- self.table_name = "users"
14
+ self.table_name = 'user_cuids'
11
15
  generate_public_uid column: :custom_uid
12
16
  end
13
17
 
14
18
  class ModelWithCustomGererator < ActiveRecord::Base
15
- self.table_name = "users"
19
+ self.table_name = 'user_puids'
16
20
 
17
21
  gener_range = ('a'..'z').to_a+('A'..'Z').to_a
18
22
 
@@ -21,16 +25,12 @@ module ActRec
21
25
  end
22
26
 
23
27
  class ModelWithGeneratorDefaults < ActiveRecord::Base
24
- self.table_name = "users"
28
+ self.table_name = 'user_puids'
25
29
  generate_public_uid
26
30
  end
27
31
 
28
32
  class ModelWithoutGenaratePublicUid < ActiveRecord::Base
29
- self.table_name = "users"
30
- end
31
-
32
- class User < ActiveRecord::Base
33
- self.table_name = 'users'
33
+ self.table_name = 'users'
34
34
  end
35
35
  end
36
36
 
File without changes
File without changes
metadata CHANGED
@@ -1,113 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: public_uid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Valent
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-28 00:00:00.000000000 Z
11
+ date: 2017-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: orm_adapter
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.5'
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: '0.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.3'
34
34
  type: :development
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: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '5'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rr
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: 1.1.2
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.1.2
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: activerecord
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ~>
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '3.2'
103
+ version: '4.2'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ~>
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '3.2'
110
+ version: '4.2'
111
111
  description: Automatic generates public unique identifier for model
112
112
  email:
113
113
  - equivalent@eq8.eu
@@ -115,8 +115,8 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
- - .gitignore
119
- - .travis.yml
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
120
  - Gemfile
121
121
  - LICENSE.txt
122
122
  - README.md
@@ -134,6 +134,7 @@ files:
134
134
  - test/lib/generators/number_random_test.rb
135
135
  - test/lib/generators/range_string_test.rb
136
136
  - test/lib/set_public_uid_test.rb
137
+ - test/lib/tasks/generate_test.rb
137
138
  - test/models/custom_public_uid_column_test.rb
138
139
  - test/models/instance_methods_initialization_test.rb
139
140
  - test/models/model_with_custom_generator_test.rb
@@ -151,17 +152,17 @@ require_paths:
151
152
  - lib
152
153
  required_ruby_version: !ruby/object:Gem::Requirement
153
154
  requirements:
154
- - - ! '>='
155
+ - - ">="
155
156
  - !ruby/object:Gem::Version
156
157
  version: '0'
157
158
  required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  requirements:
159
- - - ! '>='
160
+ - - ">="
160
161
  - !ruby/object:Gem::Version
161
162
  version: '0'
162
163
  requirements: []
163
164
  rubyforge_project:
164
- rubygems_version: 2.4.3
165
+ rubygems_version: 2.6.11
165
166
  signing_key:
166
167
  specification_version: 4
167
168
  summary: Automatic generates public UID column
@@ -169,6 +170,7 @@ test_files:
169
170
  - test/lib/generators/number_random_test.rb
170
171
  - test/lib/generators/range_string_test.rb
171
172
  - test/lib/set_public_uid_test.rb
173
+ - test/lib/tasks/generate_test.rb
172
174
  - test/models/custom_public_uid_column_test.rb
173
175
  - test/models/instance_methods_initialization_test.rb
174
176
  - test/models/model_with_custom_generator_test.rb