public_uid 1.0.2 → 1.1.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: 7cd4cccc86e11b76e1a46d7cc471dace3bc5a8c3
4
- data.tar.gz: 815f34a839ea4b11419fd11c44b24a6325a396f9
3
+ metadata.gz: 76143c57285dda08be346c203901c384bc273bf0
4
+ data.tar.gz: 1bf42db639233d908a3bd447c57d9be3582fa572
5
5
  SHA512:
6
- metadata.gz: 6c4c3b9ba948a19a316c4dc24a57b04a65718415de4e7353f964980ec80f3b99e34a4642fb1547d467fcbde377f4e3b62873b67e62914eb7f6e9e37a4595135f
7
- data.tar.gz: ec5c68da6ce0135e8ef1266b847f2d40d3570a34369235050f4be92614aae1729d42e979f109af01cfb7fd0491d1fb9762025ffb753a6928e9e83b337a8c3f67
6
+ metadata.gz: fd980096e936fa5816f1838aeb614d2d51dda724f66f046e45bceab9d072a0103e09a07325e92e800d45217352dc10cfe464c0897888b9711090c98d3a820428
7
+ data.tar.gz: 31f622b8165abae325f8c81921820fc18b91bf8dd0fe6c51a152c989834ef6a31b3841ef9d0a1e9ce40fec2ca57941e99e8f3c73b0591e0c8cad7d7a0cb7b8c3
data/README.md CHANGED
@@ -19,17 +19,18 @@ against you.
19
19
 
20
20
  This is bad:
21
21
 
22
- http://eq8.eu/orders/12/edit
23
- http://eq8.eu/orders/12-order-for-mouse-and-keyboard/edit
22
+ http://www.eq8.eu/orders/12/edit
23
+ http://www.eq8.eu/orders/12-order-for-mouse-and-keyboard/edit
24
24
 
25
25
  However if you generate random unique identifier and use that as a public
26
26
  identifier, you won't have to worry about that.
27
27
 
28
28
  This is how it should be:
29
29
 
30
- http://eq8.eu/orders/8395820/edit
31
- http://eq8.eu/orders/abaoeule/edit
32
- http://eq8.eu/orders/aZc3/edit
30
+ http://www.eq8.eu/orders/8395820/edit
31
+ http://www.eq8.eu/orders/8395820-order-for-mouse-and-keyboard/edit
32
+ http://www.eq8.eu/orders/abaoeule/edit
33
+ http://www.eq8.eu/orders/aZc3/edit
33
34
 
34
35
  So keep `record.id` for your internal relationships and show `public_id`
35
36
  to the world :smile:
@@ -47,7 +48,7 @@ And then execute:
47
48
 
48
49
  ## Usage
49
50
 
50
- Create database column for public unique id. It have to be string.
51
+ Create database column for public unique ID.
51
52
 
52
53
  ```ruby
53
54
  class AddPublicUidToUsers < ActiveRecord::Migration
@@ -65,7 +66,7 @@ class User < ActiveRecord::Base
65
66
  end
66
67
  ```
67
68
 
68
- This will automatically generate 7 digit random unique number for
69
+ This will automatically generate unique 8 char downcase string for
69
70
  column `public_uid`.
70
71
 
71
72
 
@@ -73,7 +74,7 @@ column `public_uid`.
73
74
  u = User.new
74
75
  u.public_uid #=> nil
75
76
  u.save! #=> true
76
- u.public_uid #=> 9392049
77
+ u.public_uid #=> "aeuhsthi"
77
78
  ```
78
79
 
79
80
  If you want to use different column just specify column option:
@@ -88,15 +89,15 @@ end
88
89
  u = User.new
89
90
  u.guid #=> nil
90
91
  u.save! #=> true
91
- u.guid #=> 8392049
92
+ u.guid #=> "troxuroh"
92
93
  ```
93
94
 
94
- If you want to generate random string you can use built-in string
95
+ If you want to generate random Integer you can use built-in number
95
96
  generator:
96
97
 
97
98
  ```ruby
98
99
  class User < ActiveRecord::Base
99
- generate_public_uid generator: PublicUid::Generators::RangeString.new
100
+ generate_public_uid generator: PublicUid::Generators::NumberRandom.new
100
101
  end
101
102
  ```
102
103
 
@@ -104,9 +105,14 @@ end
104
105
  u = User.new
105
106
  u.public_uid #=> nil
106
107
  u.save! #=> true
107
- u.public_uid #=> "azuberdc"
108
+ u.public_uid #=> 4567123
108
109
  ```
109
110
 
111
+ **Note** Warning **PostgreSQL** have built in type safety meaning that this
112
+ generator wont work if your public uniq ID column is a String (as the
113
+ gem would try to set Integer on a String). If you really want a number
114
+ like string you can specify number range for Range String Generator
115
+
110
116
  ### Customizing generated string
111
117
 
112
118
  ```ruby
@@ -123,6 +129,17 @@ u.save! #=> true
123
129
  u.public_uid #=> "aZ3e"
124
130
  ```
125
131
 
132
+ To generate number format String you can specify number range
133
+
134
+ ```
135
+ class User < ActiveRecord::Base
136
+ UID_RANGE = ('1'..'9').to_a
137
+ generate_public_uid generator: PublicUid::Generators::RangeString.new(4, UID_RANGE)
138
+ end
139
+
140
+ # User.create.public_uid == "1234"
141
+ ```
142
+
126
143
  ### Customizing randomized number
127
144
 
128
145
  ```ruby
@@ -16,7 +16,7 @@ module PublicUid
16
16
 
17
17
  def generate_public_uid(options={})
18
18
  @public_uid_column = options[:column] || :public_uid
19
- @public_uid_generator = options[:generator] || Generators::NumberRandom.new
19
+ @public_uid_generator = options[:generator] || Generators::RangeString.new
20
20
  before_create :generate_uid, unless: @public_uid_column
21
21
  end
22
22
  end
@@ -1,5 +1,10 @@
1
1
  module PublicUid
2
2
  class SetPublicUid
3
+ NewUidNotSetYet = Class.new(StandardError)
4
+ PublicUidColumnDoesNotExist = Class.new(StandardError)
5
+ NoPublicUidColumnSpecified = Class.new(StandardError)
6
+ NoRecordSpecified = Class.new(StandardError)
7
+
3
8
  attr_reader :new_uid
4
9
 
5
10
  def initialize(options)
@@ -22,16 +27,11 @@ module PublicUid
22
27
  private
23
28
 
24
29
  def similar_uid_exist?
25
- @klass.where(public_uid: new_uid.to_s).count > 0
30
+ @klass.where(public_uid: new_uid).count > 0
26
31
  end
27
32
 
28
33
  def check_column_existance
29
34
  raise PublicUidColumnDoesNotExist if @klass.column_names.include?(@column)
30
35
  end
31
-
32
- class NewUidNotSetYet < StandardError; end
33
- class PublicUidColumnDoesNotExist < StandardError; end
34
- class NoPublicUidColumnSpecified < StandardError; end
35
- class NoRecordSpecified < StandardError; end
36
36
  end
37
37
  end
@@ -1,3 +1,3 @@
1
1
  module PublicUid
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -76,10 +76,13 @@ TestConf.orm_modules.each do |orm_module|
76
76
 
77
77
  before { mock(instance).new_uid { 567 } }
78
78
 
79
- # Due to PostgreSQL type check feature
80
- it 'must look for integer generated numbers as a string' do
81
- count_mock = stub(record_class).count { 123 }
82
- stub(record_class).where( { public_uid: "567" } ) { count_mock }
79
+ # There is an issue with passing integer to PostgreSQL type check
80
+ # in previous version application deal with this issue by converting
81
+ # everything given by generator to string which is wrong. If the output
82
+ # of generator is not supported by DB don't use it.
83
+ it 'must pass exact type of generator to model' do
84
+ count_mock = stub(record_class).count { 10 }
85
+ stub(record_class).where( { public_uid: 567 } ) { count_mock }
83
86
 
84
87
  trigger.must_equal true
85
88
  end
@@ -2,23 +2,25 @@ require 'test_helper'
2
2
 
3
3
  TestConf.orm_modules.each do |orm_module|
4
4
  describe orm_module.description do
5
- let(:user) { "#{orm_module}::CustomPublicUidColumnModel".constantize.new }
5
+ context 'Model with custom public uid column' do
6
+ let(:user) { "#{orm_module}::CustomPublicUidColumnModel".constantize.new }
6
7
 
7
- describe '#custom_uid' do
8
- subject{ user.custom_uid }
8
+ describe '#custom_uid' do
9
+ subject{ user.custom_uid }
9
10
 
10
- context 'in new record' do
11
- it{ subject.must_be_nil }
12
- end
13
-
14
- context 'after save' do
15
- before do
16
- user.save
17
- user.reload
11
+ context 'in new record' do
12
+ it{ subject.must_be_nil }
18
13
  end
19
- it{ subject.wont_be_nil }
20
- it 'by default should generate 7 digit number string' do
21
- subject.to_i.to_s.length.must_equal(7)
14
+
15
+ context 'after save' do
16
+
17
+ before do
18
+ user.save
19
+ user.reload
20
+ end
21
+
22
+ it{ subject.must_be_kind_of(String) }
23
+ it{ subject.length.must_equal(8) }
22
24
  end
23
25
  end
24
26
  end
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  TestConf.orm_modules.each do |orm_module|
4
4
  describe orm_module.description do
5
- describe 'ModelWithCustomGererator' do
5
+ context 'Model with custom uid generator' do
6
6
  let(:user) { "#{orm_module}::ModelWithCustomGererator".constantize.new }
7
7
 
8
8
  describe '#public_uid' do
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  TestConf.orm_modules.each do |orm_module|
4
4
  describe orm_module.description do
5
- describe 'ModelWithGeneratorDefaults' do
5
+ context 'Model with default generator' do
6
6
  let(:user) { "#{orm_module}::ModelWithGeneratorDefaults".constantize.new }
7
7
 
8
8
  describe '#public_uid' do
@@ -13,14 +13,14 @@ TestConf.orm_modules.each do |orm_module|
13
13
  end
14
14
 
15
15
  context 'after save' do
16
+
16
17
  before do
17
18
  user.save
18
19
  user.reload
19
20
  end
20
- it{ subject.wont_be_nil }
21
- it 'by default should generate 7 digit number string' do
22
- subject.to_i.to_s.length.must_equal(7)
23
- end
21
+
22
+ it{ subject.must_be_kind_of(String) }
23
+ it{ subject.length.must_equal(8) }
24
24
  end
25
25
  end
26
26
  end
@@ -1,5 +1,5 @@
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 INTEGER);})
2
+ ActiveRecord::Base.connection.execute(%{CREATE TABLE users (id INTEGER PRIMARY KEY, public_uid VARCHAR, custom_uid VARCHAR);})
3
3
 
4
4
  module ActRec
5
5
  def self.description
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: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Valent
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-03 00:00:00.000000000 Z
11
+ date: 2014-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: orm_adapter