idy 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -8
- data/README.md +28 -0
- data/lib/idy/extension.rb +31 -11
- data/lib/idy/version.rb +1 -1
- data/spec/lib/idy/extension/find_spec.rb +49 -36
- data/spec/lib/idy/extension/findy_bump_spec.rb +24 -0
- data/spec/lib/idy/extension/findy_spec.rb +24 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f991a3fc046a1c984dd2ea37acf5f2881614ab7d
|
4
|
+
data.tar.gz: 437136e2d1abb67b41d882cfbb60ec4c579dd6ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a93e16c6d3ebdeaff9b27e5e264995d62cca1ba0301a80bbbc66faa080514aaf7b67df46bbaa429ecf6e11aca237e09d439adc1265b42a55efd53b958e0b7c87
|
7
|
+
data.tar.gz: f820c5618fe37624fe239d4b18c277acda0cd0db54114aad24e7ba6cfc09633861f6d6b83e82c1dd700accb76e75ca8aee937d1279c5d855d8f9f6e08d65538f
|
data/CHANGELOG.md
CHANGED
@@ -1,17 +1,22 @@
|
|
1
|
-
## v0.1.
|
1
|
+
## v0.1.3
|
2
2
|
|
3
|
-
|
3
|
+
- features
|
4
|
+
- Added `find` method to find directly via idy returning `nil` when record is not found;
|
5
|
+
- Added `find!` method to find directly via idy raising error when record is not found.
|
4
6
|
|
5
|
-
-
|
7
|
+
- bug fixes
|
8
|
+
- String that is integer like is not passed to original find method.
|
6
9
|
|
7
|
-
## v0.1.
|
10
|
+
## v0.1.2
|
8
11
|
|
9
|
-
|
12
|
+
- bug fixes
|
13
|
+
- Set options on a prefixed var to avoid conflict.
|
10
14
|
|
11
|
-
|
15
|
+
## v0.1.1
|
12
16
|
|
13
|
-
|
17
|
+
- bug fixes
|
18
|
+
- v0.1.0 had problem on `push`.
|
14
19
|
|
15
|
-
|
20
|
+
## v0.1.0
|
16
21
|
|
17
22
|
- First release.
|
data/README.md
CHANGED
@@ -97,6 +97,34 @@ Article.find('My').id
|
|
97
97
|
Keep in mind that if you have some internal code, that you cannot change,
|
98
98
|
using `find`, the hash version of the id, `idy`, will be mandatory to correct find the record.
|
99
99
|
|
100
|
+
## Findy and Findy!
|
101
|
+
|
102
|
+
We encourage you to use this methods and avoid tweak `find` Rails method. As you expect, it will find directly via idy, so a normal integer will be not found, even if it exists on database.
|
103
|
+
|
104
|
+
### Findy
|
105
|
+
|
106
|
+
The bumpless version returns `nil` when record is not found.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
Article.findy('My').id
|
110
|
+
# 1
|
111
|
+
|
112
|
+
Article.findy 'missing'
|
113
|
+
# nil
|
114
|
+
```
|
115
|
+
|
116
|
+
### Findy!
|
117
|
+
|
118
|
+
The bump `!` version raises an error when record is not found.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
Article.findy!('My').id
|
122
|
+
# 1
|
123
|
+
|
124
|
+
Article.findy! 'missing'
|
125
|
+
# ActiveRecord::RecordNotFound: Couldn't find Article with 'idy'="missing"
|
126
|
+
```
|
127
|
+
|
100
128
|
## Inspiration
|
101
129
|
|
102
130
|
It was inpired and improved from:
|
data/lib/idy/extension.rb
CHANGED
@@ -22,17 +22,27 @@ module Idy
|
|
22
22
|
|
23
23
|
module ClassMethods
|
24
24
|
def find(*args)
|
25
|
-
|
26
|
-
|
27
|
-
if
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
id = args.first
|
26
|
+
|
27
|
+
return super if args.count != 1 || integer?(id)
|
28
|
+
|
29
|
+
scope = try(:idy?) ? [id].flatten.map { |id| idy_decode(id) } : id
|
30
|
+
|
31
|
+
not_found!(id) if scope.compact.blank?
|
32
|
+
|
33
|
+
super scope.size == 1 ? scope[0] : scope
|
34
|
+
end
|
35
|
+
|
36
|
+
def findy(hash)
|
37
|
+
find_by id: idy_decode(hash)
|
38
|
+
end
|
39
|
+
|
40
|
+
def findy!(hash)
|
41
|
+
record = find_by(id: idy_decode(hash))
|
42
|
+
|
43
|
+
not_found!(hash) if record.nil?
|
44
|
+
|
45
|
+
record
|
36
46
|
end
|
37
47
|
|
38
48
|
def idy(options = {})
|
@@ -74,6 +84,16 @@ module Idy
|
|
74
84
|
def encoder(salt)
|
75
85
|
Hashids.new salt.to_s
|
76
86
|
end
|
87
|
+
|
88
|
+
def integer?(id)
|
89
|
+
Integer id
|
90
|
+
rescue
|
91
|
+
false
|
92
|
+
end
|
93
|
+
|
94
|
+
def not_found!(hash)
|
95
|
+
raise ActiveRecord::RecordNotFound, "Couldn't find User with 'idy'=#{hash.inspect}"
|
96
|
+
end
|
77
97
|
end
|
78
98
|
end
|
79
99
|
end
|
data/lib/idy/version.rb
CHANGED
@@ -3,70 +3,83 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Article do
|
6
|
-
context '
|
7
|
-
|
6
|
+
context 'with idy enabled' do
|
7
|
+
context 'on reload' do
|
8
|
+
subject { described_class.create id: 42, title: 'title' }
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
it 'reloads' do
|
11
|
+
record = described_class.find(subject.to_param)
|
11
12
|
|
12
|
-
|
13
|
+
record.update_attribute :title, 'title.updated'
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
expect(subject.title).to eq 'title'
|
16
|
+
expect(subject.reload.title).to eq 'title.updated'
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
context 'while locking' do
|
20
|
+
it 'does not throw an error' do
|
21
|
+
expect(-> { subject.lock! }).not_to raise_error
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
23
|
-
end
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
context 'finding one record as id' do
|
27
|
+
subject { described_class.find record.id }
|
27
28
|
|
28
|
-
|
29
|
+
let!(:record) { described_class.create id: 42, title: 'title-1' }
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
specify { expect(subject).to eq record }
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
context 'finding one record as idy' do
|
35
|
+
subject { described_class.find record.to_param }
|
35
36
|
|
36
|
-
|
37
|
+
let!(:record) { described_class.create id: 42, title: 'title-1' }
|
37
38
|
|
38
|
-
|
39
|
-
|
39
|
+
specify { expect(subject).to eq record }
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
-
|
42
|
+
context 'finding multiple records' do
|
43
|
+
subject { described_class.find [record_1.to_param, record_2.to_param] }
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
let!(:record_1) { described_class.create id: 42, title: 'title-1' }
|
46
|
+
let!(:record_2) { described_class.create id: 1 , title: 'title-2' }
|
46
47
|
|
47
|
-
|
48
|
-
|
48
|
+
it 'finds all' do
|
49
|
+
expect(subject).to eq [record_1, record_2]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'finding via has_many' do
|
54
|
+
let!(:article) { described_class.create }
|
55
|
+
let!(:comment) { Comment.create article: article }
|
56
|
+
|
57
|
+
specify { expect(article.comments.find(comment.id)).to eq comment }
|
49
58
|
end
|
50
|
-
end
|
51
59
|
|
52
|
-
|
53
|
-
|
54
|
-
|
60
|
+
context 'when id is an integer like' do
|
61
|
+
let!(:record) { described_class.create id: 1 }
|
62
|
+
let!(:idy) { '1a' }
|
55
63
|
|
56
|
-
|
64
|
+
it 'does not tries to use the integer part on normal find' do
|
65
|
+
expect { described_class.find(idy) }.to raise_error ActiveRecord::RecordNotFound, %(Couldn't find User with 'idy'="1a")
|
66
|
+
end
|
67
|
+
end
|
57
68
|
end
|
58
69
|
|
59
|
-
context '
|
70
|
+
context 'with idy disabled' do
|
60
71
|
let!(:record) { Clean.create }
|
61
72
|
|
62
73
|
context 'of one record' do
|
63
|
-
|
74
|
+
it 'finds by normal id' do
|
75
|
+
expect(Clean.find(record.id)).to eq record
|
76
|
+
end
|
64
77
|
end
|
65
78
|
|
66
79
|
context 'of multiple record' do
|
67
80
|
let!(:record_2) { Clean.create }
|
68
81
|
|
69
|
-
|
82
|
+
it 'finds by normal id' do
|
70
83
|
expect(Clean.find([record.id, record_2.id])).to eq [record, record_2]
|
71
84
|
end
|
72
85
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
RSpec.describe '#findy!' do
|
6
|
+
context 'when a hash is given' do
|
7
|
+
context 'and record is found' do
|
8
|
+
let!(:record) { Article.create id: 1 }
|
9
|
+
let!(:hash) { 'My' }
|
10
|
+
|
11
|
+
it 'finds the record' do
|
12
|
+
expect(Article.findy!(hash)).to eq record
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'and record is not found' do
|
17
|
+
let!(:hash) { 'My' }
|
18
|
+
|
19
|
+
it 'raises' do
|
20
|
+
expect { Article.findy!(hash) }.to raise_error ActiveRecord::RecordNotFound, %(Couldn't find User with 'idy'="My")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
RSpec.describe '#findy' do
|
6
|
+
context 'when a hash is given' do
|
7
|
+
context 'and record is found' do
|
8
|
+
let!(:record) { Article.create id: 1 }
|
9
|
+
let!(:hash) { 'My' }
|
10
|
+
|
11
|
+
it 'finds the record' do
|
12
|
+
expect(Article.findy(hash)).to eq record
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'and record is not found' do
|
17
|
+
let!(:hash) { 'My' }
|
18
|
+
|
19
|
+
it 'returns nil' do
|
20
|
+
expect(Article.findy(hash)).to be_nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: idy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Botelho
|
@@ -156,6 +156,8 @@ files:
|
|
156
156
|
- lib/idy/version.rb
|
157
157
|
- spec/lib/idy/extension/find_by_spec.rb
|
158
158
|
- spec/lib/idy/extension/find_spec.rb
|
159
|
+
- spec/lib/idy/extension/findy_bump_spec.rb
|
160
|
+
- spec/lib/idy/extension/findy_spec.rb
|
159
161
|
- spec/lib/idy/extension/idy_decode_spec.rb
|
160
162
|
- spec/lib/idy/extension/idy_default_salt_spec.rb
|
161
163
|
- spec/lib/idy/extension/idy_encode_spec.rb
|
@@ -202,6 +204,8 @@ summary: An ID obfuscator for ActiveRecord.
|
|
202
204
|
test_files:
|
203
205
|
- spec/lib/idy/extension/find_by_spec.rb
|
204
206
|
- spec/lib/idy/extension/find_spec.rb
|
207
|
+
- spec/lib/idy/extension/findy_bump_spec.rb
|
208
|
+
- spec/lib/idy/extension/findy_spec.rb
|
205
209
|
- spec/lib/idy/extension/idy_decode_spec.rb
|
206
210
|
- spec/lib/idy/extension/idy_default_salt_spec.rb
|
207
211
|
- spec/lib/idy/extension/idy_encode_spec.rb
|