idy 0.1.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f991a3fc046a1c984dd2ea37acf5f2881614ab7d
4
- data.tar.gz: 437136e2d1abb67b41d882cfbb60ec4c579dd6ec
2
+ SHA256:
3
+ metadata.gz: def5f180e468e7b690506b0c44629f0f8ee2f1f8c43faef0c3da2dd4d84bca7c
4
+ data.tar.gz: a75a3403bb5564350d46db9655f9f4acec21d3f7079820a60442a9e0cfc8ec10
5
5
  SHA512:
6
- metadata.gz: a93e16c6d3ebdeaff9b27e5e264995d62cca1ba0301a80bbbc66faa080514aaf7b67df46bbaa429ecf6e11aca237e09d439adc1265b42a55efd53b958e0b7c87
7
- data.tar.gz: f820c5618fe37624fe239d4b18c277acda0cd0db54114aad24e7ba6cfc09633861f6d6b83e82c1dd700accb76e75ca8aee937d1279c5d855d8f9f6e08d65538f
6
+ metadata.gz: 86b8b4eaddbc6acc12c7c5a9ee72c31d884a2835d66a0bbff5a1c90ab0a473ebf50f42a5fec0802c9bcd675f19a3ef028ec68ba0938554e3dcdf945360f9bc66
7
+ data.tar.gz: 539a92ad5de1baf061e736250e12978bc4c1dc2f24f0aa36c1b0cdea61440eec75b75e2958f1b49b6600df277337e9b6749c75fb27e089bd1a1f5842437ae96f
@@ -1,3 +1,8 @@
1
+ ## v1.0.0
2
+
3
+ - News
4
+ - Drops overrided `find` method to avoid find on a hash that looks like a number issue [#1](https://github.com/wbotelhos/idy/issues/1)
5
+
1
6
  ## v0.1.3
2
7
 
3
8
  - features
data/README.md CHANGED
@@ -45,7 +45,7 @@ Article.new(id: 1).idy
45
45
  # My
46
46
  ```
47
47
 
48
- It will build you Rals URL with that ID too:
48
+ It will build your Rails URL with that ID too:
49
49
 
50
50
  ```ruby
51
51
  Article.new(id: 1).to_param
@@ -60,7 +60,7 @@ If you want a *unbreakable* hash, it is not for you.
60
60
  ## Collision
61
61
 
62
62
  To avoid two differents models to generates the same hash for the same ID,
63
- by default, the class name is used as a [Salt](https://en.wikipedia.org/wiki/Salt_(cryptography).
63
+ by default, the class name is used as a [Salt](https://en.wikipedia.org/wiki/Salt_cryptography).
64
64
 
65
65
  ```ruby
66
66
  Article.new(id: 1).idy
@@ -85,6 +85,25 @@ Article.new(id: 1).idy
85
85
  # 9A
86
86
  ```
87
87
 
88
+ ## Idy
89
+
90
+ As you could see, the method `idy`, returns the hash representation of your ID:
91
+
92
+ ```ruby
93
+ Article.new(id: 1).idy
94
+ # My
95
+ ```
96
+
97
+ If you want get all idys from a collection, just map it:
98
+
99
+ ```ruby
100
+ Article.create
101
+ Article.create
102
+
103
+ Article.select(:id).map(&:idy)
104
+ # ["My", "aL"]
105
+ ```
106
+
88
107
  ## Find
89
108
 
90
109
  Since you add the `idy` callback to your model, `find` method will be decorated:
@@ -134,4 +153,4 @@ It was inpired and improved from:
134
153
 
135
154
  ## Love it!
136
155
 
137
- Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=idy) or [Gratipay](https://gratipay.com/~wbotelhos). Thanks! (:
156
+ Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=idy) or [Gratipay](https://liberapay.com/idy/donate). Thanks! (:
@@ -21,18 +21,6 @@ module Idy
21
21
  end
22
22
 
23
23
  module ClassMethods
24
- def find(*args)
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
24
  def findy(hash)
37
25
  find_by id: idy_decode(hash)
38
26
  end
@@ -85,12 +73,6 @@ module Idy
85
73
  Hashids.new salt.to_s
86
74
  end
87
75
 
88
- def integer?(id)
89
- Integer id
90
- rescue
91
- false
92
- end
93
-
94
76
  def not_found!(hash)
95
77
  raise ActiveRecord::RecordNotFound, "Couldn't find User with 'idy'=#{hash.inspect}"
96
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Idy
4
- VERSION = '0.1.3'.freeze
4
+ VERSION = '1.0.0'
5
5
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '#findy!' do
5
+ RSpec.describe Article, '#findy!' do
6
6
  context 'when a hash is given' do
7
- context 'and record is found' do
7
+ context 'when record is found' do
8
8
  let!(:record) { Article.create id: 1 }
9
9
  let!(:hash) { 'My' }
10
10
 
@@ -13,11 +13,13 @@ RSpec.describe '#findy!' do
13
13
  end
14
14
  end
15
15
 
16
- context 'and record is not found' do
16
+ context 'when record is not found' do
17
17
  let!(:hash) { 'My' }
18
18
 
19
19
  it 'raises' do
20
- expect { Article.findy!(hash) }.to raise_error ActiveRecord::RecordNotFound, %(Couldn't find User with 'idy'="My")
20
+ message = %(Couldn't find User with 'idy'="My")
21
+
22
+ expect { Article.findy!(hash) }.to raise_error ActiveRecord::RecordNotFound, message
21
23
  end
22
24
  end
23
25
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '#findy' do
5
+ RSpec.describe Article, '#findy' do
6
6
  context 'when a hash is given' do
7
- context 'and record is found' do
7
+ context 'when record is found' do
8
8
  let!(:record) { Article.create id: 1 }
9
9
  let!(:hash) { 'My' }
10
10
 
@@ -13,7 +13,7 @@ RSpec.describe '#findy' do
13
13
  end
14
14
  end
15
15
 
16
- context 'and record is not found' do
16
+ context 'when record is not found' do
17
17
  let!(:hash) { 'My' }
18
18
 
19
19
  it 'returns nil' do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '#idy_decode' do
5
+ RSpec.describe Article, '#idy_decode' do
6
6
  context 'with no given salt' do
7
7
  it 'undos the obfuscation of id with default class salt' do
8
8
  expect(Article.idy_decode('My')).to eq 1
@@ -18,7 +18,7 @@ RSpec.describe '#idy_decode' do
18
18
  end
19
19
 
20
20
  context 'with salt' do
21
- context 'as string' do
21
+ context 'when is string' do
22
22
  let!(:salt) { 'salt' }
23
23
 
24
24
  it 'undos the obfuscation of id with given salt' do
@@ -26,7 +26,7 @@ RSpec.describe '#idy_decode' do
26
26
  end
27
27
  end
28
28
 
29
- context 'as number' do
29
+ context 'when is number' do
30
30
  let!(:salt) { 1 }
31
31
 
32
32
  it 'undos the obfuscation of id with given salt as string' do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '#idy_default_salt' do
5
+ RSpec.describe Abcdefghijklm, '#idy_default_salt' do
6
6
  context 'when class name has a big length' do
7
7
  it 'is bases on the first 10 class letter position index on alphabet' do
8
8
  expect(Abcdefghijklm.idy_default_salt).to eq '12345678910'
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '#idy_encode' do
5
+ RSpec.describe Article, '#idy_encode' do
6
6
  context 'with no given salt' do
7
7
  let!(:model) { Article.new id: 1 }
8
8
 
@@ -23,7 +23,7 @@ RSpec.describe '#idy_encode' do
23
23
  context 'with salt' do
24
24
  let!(:model) { Article.new id: 1 }
25
25
 
26
- context 'as string' do
26
+ context 'when is string' do
27
27
  let!(:salt) { 'salt' }
28
28
 
29
29
  it 'obfuscates the id with given salt' do
@@ -31,7 +31,7 @@ RSpec.describe '#idy_encode' do
31
31
  end
32
32
  end
33
33
 
34
- context 'as number' do
34
+ context 'when is number' do
35
35
  let!(:salt) { 1 }
36
36
 
37
37
  it 'obfuscates the id with given salt as string' do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '.idy' do
5
+ RSpec.describe Article, '.idy' do
6
6
  subject { Article.new id: 1 }
7
7
 
8
8
  it 'returns the encoded id' do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '.idy_options' do
5
+ RSpec.describe Article, '.idy_options' do
6
6
  context 'when options is not given' do
7
7
  it 'returns a default options with a salt generated based on model name' do
8
8
  expect(Article.idy_options).to eq(salt: article_default_salt)
@@ -2,11 +2,9 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe ':salt' do
5
+ RSpec.describe Clean, ':salt' do
6
6
  describe '#salt' do
7
- before do
8
- allow(Clean).to receive(:idy_options) { { salt: :salty } }
9
- end
7
+ before { allow(Clean).to receive(:idy_options).and_return(salt: :salty) }
10
8
 
11
9
  it 'fetchs the salt options' do
12
10
  expect(Clean.salt).to eq(:salty)
@@ -14,7 +12,7 @@ RSpec.describe ':salt' do
14
12
  end
15
13
 
16
14
  describe '.salt' do
17
- before { allow(Clean).to receive(:salt) { :salt } }
15
+ before { allow(Clean).to receive(:salt).and_return :salt }
18
16
 
19
17
  it 'delegates to class method' do
20
18
  expect(Clean.new.salt).to eq :salt
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe '.to_param' do
5
+ RSpec.describe Clean, '.to_param' do
6
6
  context 'when object does not acts as idy' do
7
7
  subject { Clean.new id: 1 }
8
8
 
@@ -32,13 +32,13 @@ RSpec.describe '.to_param' do
32
32
  end
33
33
 
34
34
  context 'when object is not persisted' do
35
- context 'and has no id' do
35
+ context 'when has no id' do
36
36
  subject { Article.new }
37
37
 
38
38
  specify { expect(subject.to_param).to be_nil }
39
39
  end
40
40
 
41
- context 'and has id' do
41
+ context 'when has id' do
42
42
  subject { Article.new id: 1 }
43
43
 
44
44
  it 'generates a hash' do
@@ -9,7 +9,7 @@ RSpec.configure do |config|
9
9
  DatabaseCleaner.clean_with :transaction
10
10
  end
11
11
 
12
- config.before :each do
12
+ config.before do
13
13
  DatabaseCleaner.start
14
14
  end
15
15
 
@@ -1,3 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'support/db/migrate/create_tables'
4
+
3
5
  CreateTables.new.change
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Washington Botelho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-06 00:00:00.000000000 Z
11
+ date: 2017-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashids
@@ -59,7 +59,7 @@ dependencies:
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
61
  - !ruby/object:Gem::Dependency
62
- name: guard-rspec
62
+ name: pry-byebug
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - ">="
@@ -86,20 +86,6 @@ dependencies:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
- - !ruby/object:Gem::Dependency
90
- name: rubocop-rspec
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- version: '0'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- version: '0'
103
89
  - !ruby/object:Gem::Dependency
104
90
  name: rubocop
105
91
  requirement: !ruby/object:Gem::Requirement
@@ -115,7 +101,7 @@ dependencies:
115
101
  - !ruby/object:Gem::Version
116
102
  version: '0'
117
103
  - !ruby/object:Gem::Dependency
118
- name: sqlite3
104
+ name: rubocop-rspec
119
105
  requirement: !ruby/object:Gem::Requirement
120
106
  requirements:
121
107
  - - ">="
@@ -129,7 +115,7 @@ dependencies:
129
115
  - !ruby/object:Gem::Version
130
116
  version: '0'
131
117
  - !ruby/object:Gem::Dependency
132
- name: pry-byebug
118
+ name: sqlite3
133
119
  requirement: !ruby/object:Gem::Requirement
134
120
  requirements:
135
121
  - - ">="
@@ -154,15 +140,12 @@ files:
154
140
  - lib/idy.rb
155
141
  - lib/idy/extension.rb
156
142
  - lib/idy/version.rb
157
- - spec/lib/idy/extension/find_by_spec.rb
158
- - spec/lib/idy/extension/find_spec.rb
159
143
  - spec/lib/idy/extension/findy_bump_spec.rb
160
144
  - spec/lib/idy/extension/findy_spec.rb
161
145
  - spec/lib/idy/extension/idy_decode_spec.rb
162
146
  - spec/lib/idy/extension/idy_default_salt_spec.rb
163
147
  - spec/lib/idy/extension/idy_encode_spec.rb
164
148
  - spec/lib/idy/extension/idy_spec.rb
165
- - spec/lib/idy/extension/lock_spec.rb
166
149
  - spec/lib/idy/extension/options_spec.rb
167
150
  - spec/lib/idy/extension/salt_spec.rb
168
151
  - spec/lib/idy/extension/to_param_spec.rb
@@ -197,31 +180,28 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
180
  version: '0'
198
181
  requirements: []
199
182
  rubyforge_project:
200
- rubygems_version: 2.6.11
183
+ rubygems_version: 2.7.3
201
184
  signing_key:
202
185
  specification_version: 4
203
186
  summary: An ID obfuscator for ActiveRecord.
204
187
  test_files:
205
- - spec/lib/idy/extension/find_by_spec.rb
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
209
- - spec/lib/idy/extension/idy_decode_spec.rb
210
- - spec/lib/idy/extension/idy_default_salt_spec.rb
211
- - spec/lib/idy/extension/idy_encode_spec.rb
212
- - spec/lib/idy/extension/idy_spec.rb
213
- - spec/lib/idy/extension/lock_spec.rb
214
- - spec/lib/idy/extension/options_spec.rb
215
- - spec/lib/idy/extension/salt_spec.rb
216
- - spec/lib/idy/extension/to_param_spec.rb
217
- - spec/rails_helper.rb
218
- - spec/support/common.rb
219
- - spec/support/database_cleaner.rb
220
- - spec/support/db/migrate/create_tables.rb
221
188
  - spec/support/migrate.rb
222
- - spec/support/models/abcdefghijklm.rb
223
189
  - spec/support/models/article.rb
190
+ - spec/support/models/abcdefghijklm.rb
224
191
  - spec/support/models/clean.rb
225
192
  - spec/support/models/comment.rb
226
193
  - spec/support/models/post.rb
227
194
  - spec/support/models/user.rb
195
+ - spec/support/common.rb
196
+ - spec/support/db/migrate/create_tables.rb
197
+ - spec/support/database_cleaner.rb
198
+ - spec/lib/idy/extension/idy_spec.rb
199
+ - spec/lib/idy/extension/to_param_spec.rb
200
+ - spec/lib/idy/extension/salt_spec.rb
201
+ - spec/lib/idy/extension/options_spec.rb
202
+ - spec/lib/idy/extension/idy_encode_spec.rb
203
+ - spec/lib/idy/extension/idy_decode_spec.rb
204
+ - spec/lib/idy/extension/findy_spec.rb
205
+ - spec/lib/idy/extension/findy_bump_spec.rb
206
+ - spec/lib/idy/extension/idy_default_salt_spec.rb
207
+ - spec/rails_helper.rb
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Article, '.find_by' do
6
- subject { described_class.create title: 'title' }
7
-
8
- it 'works with normal id' do
9
- record = described_class.find_by(id: subject.id)
10
-
11
- expect(subject).to eq record
12
- end
13
- end
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Article do
6
- context 'with idy enabled' do
7
- context 'on reload' do
8
- subject { described_class.create id: 42, title: 'title' }
9
-
10
- it 'reloads' do
11
- record = described_class.find(subject.to_param)
12
-
13
- record.update_attribute :title, 'title.updated'
14
-
15
- expect(subject.title).to eq 'title'
16
- expect(subject.reload.title).to eq 'title.updated'
17
- end
18
-
19
- context 'while locking' do
20
- it 'does not throw an error' do
21
- expect(-> { subject.lock! }).not_to raise_error
22
- end
23
- end
24
- end
25
-
26
- context 'finding one record as id' do
27
- subject { described_class.find record.id }
28
-
29
- let!(:record) { described_class.create id: 42, title: 'title-1' }
30
-
31
- specify { expect(subject).to eq record }
32
- end
33
-
34
- context 'finding one record as idy' do
35
- subject { described_class.find record.to_param }
36
-
37
- let!(:record) { described_class.create id: 42, title: 'title-1' }
38
-
39
- specify { expect(subject).to eq record }
40
- end
41
-
42
- context 'finding multiple records' do
43
- subject { described_class.find [record_1.to_param, record_2.to_param] }
44
-
45
- let!(:record_1) { described_class.create id: 42, title: 'title-1' }
46
- let!(:record_2) { described_class.create id: 1 , title: 'title-2' }
47
-
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 }
58
- end
59
-
60
- context 'when id is an integer like' do
61
- let!(:record) { described_class.create id: 1 }
62
- let!(:idy) { '1a' }
63
-
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
68
- end
69
-
70
- context 'with idy disabled' do
71
- let!(:record) { Clean.create }
72
-
73
- context 'of one record' do
74
- it 'finds by normal id' do
75
- expect(Clean.find(record.id)).to eq record
76
- end
77
- end
78
-
79
- context 'of multiple record' do
80
- let!(:record_2) { Clean.create }
81
-
82
- it 'finds by normal id' do
83
- expect(Clean.find([record.id, record_2.id])).to eq [record, record_2]
84
- end
85
- end
86
- end
87
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Article, '.lock!' do
6
- subject { described_class.create title: 'title' }
7
-
8
- it 'works' do
9
- record = described_class.find(subject.to_param)
10
-
11
- expect { record.lock! }.not_to raise_error
12
- end
13
- end