public_uid 2.1.0 → 2.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
  SHA256:
3
- metadata.gz: 2feee63566bac287b2b37df241fe9f6d921815a25fa9d8ffda0ec2e51e79da88
4
- data.tar.gz: '06879a851089f031f9729d7b9651e32ce5340ab2096db896c9001547b26be866'
3
+ metadata.gz: b9f535dd58ffefeceb1c162b838cd6bfc152d12d5d0c3036ccf3377d15a5d25f
4
+ data.tar.gz: a65e4856004a370828706d678a55be3a5940ff7f5a288ba8a4e5849060bc4da4
5
5
  SHA512:
6
- metadata.gz: 592b580707bcb87cee3c82405b838011524d4197573f7ba3982c7b8cd3982fbd9605123e5b24a05f1c0b11be1bd54083fba0eaec27c47c157c361fb808ff1b90
7
- data.tar.gz: c4b47726fd86694bd4213e72749841ba398dd408ed2d239fb749108df89ad93b4601a02eca1eac765b1eb0e17e16d79b9e69146ac552fecde9f237f89f667043
6
+ metadata.gz: f7ae3ce54c0dfaa21d192937c9c6aec75da1aaae789a2261a55dcfabcfc3e740c43b7ae646f2ca601d9139008108e69d6635eef058c35c18b135496d68a79d07
7
+ data.tar.gz: b4e1daa515802166ea148c14a1fa3352f871f7a7725e77e74af37ea9d726e365545ad398e23c4e08a7dce06c38f0741513ed12a7ca4c8fdc53b37cdcfe2cf1ed
data/.travis.yml CHANGED
@@ -1,8 +1,12 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.8
4
3
  - 2.4.5
5
4
  - 2.5.3
6
5
  - 2.6.5
6
+ - 2.7.0
7
+ - 2.7.3
8
+ - 2.7.5
9
+ - 3.0.0
10
+ - 3.1.0
7
11
  before_install:
8
12
  - gem install bundler
data/README.md CHANGED
@@ -57,14 +57,28 @@ Create database column for public unique ID.
57
57
  class AddPublicUidToUsers < ActiveRecord::Migration
58
58
  def change
59
59
  add_column :users, :public_uid, :string
60
- add_index :users, :public_uid
60
+ add_index :users, :public_uid, unique: true
61
+ end
62
+ end
63
+ ```
64
+
65
+ or if you are creating table
66
+
67
+ ```ruby
68
+ class CreateEntries < ActiveRecord::Migration[7.1]
69
+ def change
70
+ create_table :entries do |t|
71
+ # ...
72
+ t.string :public_uid, index: { unique: true }
73
+ # ...
74
+ end
61
75
  end
62
76
  end
63
77
  ```
64
78
 
65
79
  ### Step 2a - Using Rails concern
66
80
 
67
- > a.k.a - the easy way for Rails
81
+ > a.k.a - the easy way for Rails (availible since version 2.1.0)
68
82
 
69
83
  ```ruby
70
84
  class User < ActiveRecord::Base
@@ -72,6 +86,28 @@ class User < ActiveRecord::Base
72
86
  end
73
87
  ```
74
88
 
89
+ Example:
90
+
91
+
92
+ ```ruby
93
+ user = User.create
94
+ user.public_uid
95
+ # => "xxxxxxxx"
96
+
97
+ user.to_param
98
+ # => "xxxxxxxx"
99
+
100
+ User.find_puid('xxxxxxxx')
101
+ # => <#User ...>
102
+ User.find_puid('not_existing')
103
+ # => nil
104
+
105
+ User.find_puid!('xxxxxxxx')
106
+ # => <#User ...>
107
+ User.find_puid!('not_existing')
108
+ # PublicUid::RecordNotFound (User 'not_existing' not found)
109
+ ```
110
+
75
111
  This will automatically:
76
112
 
77
113
  * assumes your model has `public_uid` column and generate public_uid value for you
@@ -85,6 +121,8 @@ If you need more customization please follow **Step 2b** instead
85
121
 
86
122
  ### Step 2b - Using manual generate method
87
123
 
124
+ > a.k.a bit harder than `2.a` but more flexible way for Rails
125
+
88
126
  Tell your model to generate the public identifier
89
127
 
90
128
  ```ruby
@@ -111,7 +149,7 @@ Then you can do more clever things like having urls based on `public_uid` and `t
111
149
  class User < ActiveRecord::Base
112
150
  generate_public_uid
113
151
 
114
- def self.find_param(param)
152
+ def self.find_puid(param)
115
153
  find_by! public_uid: param.split('-').first
116
154
  end
117
155
 
@@ -124,7 +162,7 @@ end
124
162
  class UsersController < ActionController::Base
125
163
  # ...
126
164
  def show
127
- @user = User.find_param(param[:id])
165
+ @user = User.find_puid(param[:id])
128
166
  # ...
129
167
  end
130
168
  # ...
@@ -357,3 +395,12 @@ If anyone want to see public_uid v 2.0 to support other data mappers (e.g. mongo
357
395
 
358
396
  Sorry for any inconvenience
359
397
 
398
+ ##### 2022-11-03
399
+
400
+ Version 2.2.0 released
401
+
402
+
403
+ `model.dup` will now not copy the public_uid value (same as `model.id` )
404
+
405
+ more: https://github.com/equivalent/public_uid/pull/21
406
+
@@ -14,6 +14,15 @@ module PublicUid
14
14
  pub_uid.generate self.class.public_uid_generator
15
15
  pub_uid.set
16
16
  end
17
+
18
+ def initialize_dup(*)
19
+ super
20
+ _clear_public_uid_column
21
+ end
22
+
23
+ def _clear_public_uid_column
24
+ self.send("#{self.class.public_uid_column}=", nil)
25
+ end
17
26
  end
18
27
 
19
28
  module ClassMethods
@@ -27,7 +27,7 @@ module PublicUid
27
27
  private
28
28
 
29
29
  def similar_uid_exist?
30
- @klass.where(@column => new_uid).count > 0
30
+ @klass.where(@column => new_uid).exists?
31
31
  end
32
32
 
33
33
  def check_column_existance
@@ -1,3 +1,3 @@
1
1
  module PublicUid
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -3,7 +3,8 @@ require 'test_helper'
3
3
  TestConf.orm_modules.each do |orm_module|
4
4
  describe orm_module.description do
5
5
  context 'Model with custom public uid column' do
6
- let(:user) { "#{orm_module}::CustomPublicUidColumnModel".constantize.new }
6
+ let(:model_class) { "#{orm_module}::CustomPublicUidColumnModel".constantize }
7
+ let(:user) { model_class.new }
7
8
 
8
9
  describe '#custom_uid' do
9
10
  subject{ user.custom_uid }
@@ -31,6 +32,26 @@ TestConf.orm_modules.each do |orm_module|
31
32
  it{ expect(subject.length).must_equal(8) }
32
33
  end
33
34
  end
35
+
36
+ describe '#dup' do
37
+ let(:user) { model_class.new(:custom_uid => 'abcdefg') }
38
+
39
+ it do
40
+ dup_user = user.dup
41
+ expect(user.custom_uid).must_equal('abcdefg')
42
+ expect(dup_user.custom_uid).must_equal(nil)
43
+ end
44
+ end
45
+
46
+ describe '#clone' do
47
+ let(:user) { model_class.new(:custom_uid => 'abcdefg') }
48
+
49
+ it do
50
+ clone_user = user.clone
51
+ expect(user.custom_uid).must_equal('abcdefg')
52
+ expect(clone_user.custom_uid).must_equal('abcdefg')
53
+ end
54
+ end
34
55
  end
35
56
  end
36
57
  end
@@ -3,7 +3,8 @@ require 'test_helper'
3
3
  TestConf.orm_modules.each do |orm_module|
4
4
  describe orm_module.description do
5
5
  context 'Model with default generator' do
6
- let(:user) { "#{orm_module}::ModelWithGeneratorDefaults".constantize.new }
6
+ let(:model_class) { "#{orm_module}::ModelWithGeneratorDefaults".constantize }
7
+ let(:user) { model_class.new }
7
8
 
8
9
  describe '#public_uid' do
9
10
  subject{ user.public_uid }
@@ -31,6 +32,26 @@ TestConf.orm_modules.each do |orm_module|
31
32
  it{ expect(subject.length).must_equal(8) }
32
33
  end
33
34
  end
35
+
36
+ describe '#dup' do
37
+ let(:user) { model_class.new(:public_uid => 'abcdefg') }
38
+
39
+ it do
40
+ dup_user = user.dup
41
+ expect(user.public_uid).must_equal('abcdefg')
42
+ expect(dup_user.public_uid).must_equal(nil)
43
+ end
44
+ end
45
+
46
+ describe '#clone' do
47
+ let(:user) { model_class.new(:public_uid => 'abcdefg') }
48
+
49
+ it do
50
+ clone_user = user.clone
51
+ expect(user.public_uid).must_equal('abcdefg')
52
+ expect(clone_user.public_uid).must_equal('abcdefg')
53
+ end
54
+ end
34
55
  end
35
56
  end
36
57
  end
@@ -44,6 +44,26 @@ TestConf.orm_modules.each do |orm_module|
44
44
  end
45
45
  end
46
46
  end
47
+
48
+ describe '#dup' do
49
+ let(:user) { model_class.new(:public_uid => 'abcdefg') }
50
+
51
+ it do
52
+ dup_user = user.dup
53
+ expect(user.public_uid).must_equal('abcdefg')
54
+ expect(dup_user.public_uid).must_equal(nil)
55
+ end
56
+ end
57
+
58
+ describe '#clone' do
59
+ let(:user) { model_class.new(:public_uid => 'abcdefg') }
60
+
61
+ it do
62
+ clone_user = user.clone
63
+ expect(user.public_uid).must_equal('abcdefg')
64
+ expect(clone_user.public_uid).must_equal('abcdefg')
65
+ end
66
+ end
47
67
  end
48
68
  end
49
69
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: public_uid
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Valent
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-22 00:00:00.000000000 Z
11
+ date: 2022-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -165,7 +165,7 @@ homepage: https://github.com/equivalent/public_uid
165
165
  licenses:
166
166
  - MIT
167
167
  metadata: {}
168
- post_install_message:
168
+ post_install_message:
169
169
  rdoc_options: []
170
170
  require_paths:
171
171
  - lib
@@ -180,8 +180,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
182
  requirements: []
183
- rubygems_version: 3.0.3
184
- signing_key:
183
+ rubygems_version: 3.3.7
184
+ signing_key:
185
185
  specification_version: 4
186
186
  summary: Automatic generates public UID column
187
187
  test_files: