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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6349ce9069910d1380b20fc75f10ac110a18abb1
4
- data.tar.gz: af38de63a88d7dd6ba36b8c872f9e195eea5f0fc
3
+ metadata.gz: f991a3fc046a1c984dd2ea37acf5f2881614ab7d
4
+ data.tar.gz: 437136e2d1abb67b41d882cfbb60ec4c579dd6ec
5
5
  SHA512:
6
- metadata.gz: 44256a6ec3fd85dfbac624cbd9c25ab87963e737db3fc503a7dfd1b446fb6aa035c7cad59c64954159de273d6ad154e9cadfa5902779b7b0e3cb2951efb6cbd9
7
- data.tar.gz: f774c785912c30529bea0961ed8e347f8002da7f63c3547cce19d5c1ff651c2757ac33e3a3cad9249d74c5587198fc9ae7d6a3529a1d15bb547765f7778b83d1
6
+ metadata.gz: a93e16c6d3ebdeaff9b27e5e264995d62cca1ba0301a80bbbc66faa080514aaf7b67df46bbaa429ecf6e11aca237e09d439adc1265b42a55efd53b958e0b7c87
7
+ data.tar.gz: f820c5618fe37624fe239d4b18c277acda0cd0db54114aad24e7ba6cfc09633861f6d6b83e82c1dd700accb76e75ca8aee937d1279c5d855d8f9f6e08d65538f
@@ -1,17 +1,22 @@
1
- ## v0.1.2
1
+ ## v0.1.3
2
2
 
3
- #### Fix
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
- - set options on a prefixed var to avoid conflict.
7
+ - bug fixes
8
+ - String that is integer like is not passed to original find method.
6
9
 
7
- ## v0.1.1
10
+ ## v0.1.2
8
11
 
9
- #### Fix
12
+ - bug fixes
13
+ - Set options on a prefixed var to avoid conflict.
10
14
 
11
- - v0.1.0 had problem on `push`.
15
+ ## v0.1.1
12
16
 
13
- ## v0.1.0
17
+ - bug fixes
18
+ - v0.1.0 had problem on `push`.
14
19
 
15
- #### Feature
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:
@@ -22,17 +22,27 @@ module Idy
22
22
 
23
23
  module ClassMethods
24
24
  def find(*args)
25
- query = [args.first].flatten
26
-
27
- if try(:idy?)
28
- if query.size == 1
29
- super idy_decode(query[0].to_s) || query[0]
30
- else
31
- super [query].flatten.map { |hash| idy_decode hash }
32
- end
33
- else
34
- super [query].flatten.size == 1 ? query[0] : query
35
- end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Idy
4
- VERSION = '0.1.2'.freeze
4
+ VERSION = '0.1.3'.freeze
5
5
  end
@@ -3,70 +3,83 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Article do
6
- context 'on reload' do
7
- subject { described_class.create id: 42, title: 'title' }
6
+ context 'with idy enabled' do
7
+ context 'on reload' do
8
+ subject { described_class.create id: 42, title: 'title' }
8
9
 
9
- it 'reloads' do
10
- record = described_class.find(subject.to_param)
10
+ it 'reloads' do
11
+ record = described_class.find(subject.to_param)
11
12
 
12
- record.update_attribute :title, 'title.updated'
13
+ record.update_attribute :title, 'title.updated'
13
14
 
14
- expect(subject.title).to eq 'title'
15
- expect(subject.reload.title).to eq 'title.updated'
16
- end
15
+ expect(subject.title).to eq 'title'
16
+ expect(subject.reload.title).to eq 'title.updated'
17
+ end
17
18
 
18
- context 'while locking' do
19
- it 'does not throw an error' do
20
- expect(-> { subject.lock! }).not_to raise_error
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
- context 'finding one record as id' do
26
- subject { described_class.find record.id }
26
+ context 'finding one record as id' do
27
+ subject { described_class.find record.id }
27
28
 
28
- let!(:record) { described_class.create id: 42, title: 'title-1' }
29
+ let!(:record) { described_class.create id: 42, title: 'title-1' }
29
30
 
30
- specify { expect(subject).to eq record }
31
- end
31
+ specify { expect(subject).to eq record }
32
+ end
32
33
 
33
- context 'finding one record as idy' do
34
- subject { described_class.find record.to_param }
34
+ context 'finding one record as idy' do
35
+ subject { described_class.find record.to_param }
35
36
 
36
- let!(:record) { described_class.create id: 42, title: 'title-1' }
37
+ let!(:record) { described_class.create id: 42, title: 'title-1' }
37
38
 
38
- specify { expect(subject).to eq record }
39
- end
39
+ specify { expect(subject).to eq record }
40
+ end
40
41
 
41
- context 'finding multiple records' do
42
- subject { described_class.find [record_1.to_param, record_2.to_param] }
42
+ context 'finding multiple records' do
43
+ subject { described_class.find [record_1.to_param, record_2.to_param] }
43
44
 
44
- let!(:record_1) { described_class.create id: 42, title: 'title-1' }
45
- let!(:record_2) { described_class.create id: 1 , title: 'title-2' }
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
- it 'finds all' do
48
- expect(subject).to eq [record_1, record_2]
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
- context 'finding via has_many' do
53
- let!(:article) { described_class.create }
54
- let!(:comment) { Comment.create article: article }
60
+ context 'when id is an integer like' do
61
+ let!(:record) { described_class.create id: 1 }
62
+ let!(:idy) { '1a' }
55
63
 
56
- specify { expect(article.comments.find(comment.id)).to eq comment }
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 'finding a class with no callback declared' do
70
+ context 'with idy disabled' do
60
71
  let!(:record) { Clean.create }
61
72
 
62
73
  context 'of one record' do
63
- specify { expect(Clean.find(record.id)).to eq record }
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
- specify do
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.2
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