idy 0.1.3 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +40 -9
- data/README.md +48 -8
- data/lib/idy/extension.rb +1 -19
- data/lib/idy/version.rb +1 -1
- metadata +26 -76
- data/spec/lib/idy/extension/find_by_spec.rb +0 -13
- data/spec/lib/idy/extension/find_spec.rb +0 -87
- data/spec/lib/idy/extension/findy_bump_spec.rb +0 -24
- data/spec/lib/idy/extension/findy_spec.rb +0 -24
- data/spec/lib/idy/extension/idy_decode_spec.rb +0 -37
- data/spec/lib/idy/extension/idy_default_salt_spec.rb +0 -11
- data/spec/lib/idy/extension/idy_encode_spec.rb +0 -50
- data/spec/lib/idy/extension/idy_spec.rb +0 -11
- data/spec/lib/idy/extension/lock_spec.rb +0 -13
- data/spec/lib/idy/extension/options_spec.rb +0 -17
- data/spec/lib/idy/extension/salt_spec.rb +0 -23
- data/spec/lib/idy/extension/to_param_spec.rb +0 -51
- data/spec/rails_helper.rb +0 -27
- data/spec/support/common.rb +0 -11
- data/spec/support/database_cleaner.rb +0 -21
- data/spec/support/db/migrate/create_tables.rb +0 -29
- data/spec/support/migrate.rb +0 -3
- data/spec/support/models/abcdefghijklm.rb +0 -4
- data/spec/support/models/article.rb +0 -7
- data/spec/support/models/clean.rb +0 -4
- data/spec/support/models/comment.rb +0 -7
- data/spec/support/models/post.rb +0 -5
- data/spec/support/models/user.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5c1011342e7f8209b6c228fd8942f1d4e5b95032a2e30d541d0440c9edc9c9b8
|
4
|
+
data.tar.gz: a3033d0cc45ddde615174dc662e1c33f21e24a33739d1e4ae7b7cfa454a993d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4972508e537e35f40bcc2126f738ffb31b1181e7d322d80375f1f7fce30dca31ca732130384017c22af25a7d1f74bcb0c344588adbaaffdd38ddd932910a1ea
|
7
|
+
data.tar.gz: b42199aa35fb78fb891e893ee3975964f1b02f84675ac0be0b7da6f9031732c8d1af5762fa8ab25d13b25070e9d22c1754da2e078285c42437465bc93e35e1dc
|
data/CHANGELOG.md
CHANGED
@@ -1,21 +1,52 @@
|
|
1
|
+
## v1.2.0
|
2
|
+
|
3
|
+
### Fixes
|
4
|
+
|
5
|
+
- Allows namespaced class to be obfuscated. [#10](https://github.com/wbotelhos/idy/pull/10) by [vitobotta](https://github.com/vitobotta)
|
6
|
+
|
7
|
+
### Updates
|
8
|
+
|
9
|
+
- Adds all previous version on the [Releases page](https://github.com/wbotelhos/idy/releases);
|
10
|
+
- Adds coverage via [Codecov](https://app.codecov.io/gh/wbotelhos/idy);
|
11
|
+
- Moves the CI to [Github Actions](https://github.com/wbotelhos/idy/actions);
|
12
|
+
- Removes spec files from the gem to keep the package smaller;
|
13
|
+
|
14
|
+
## v1.1.0
|
15
|
+
|
16
|
+
### Updates
|
17
|
+
|
18
|
+
- Does not restrict Rails version;
|
19
|
+
- Does not restrict Hashids version;
|
20
|
+
- Bump Ruby version to 2.6.4;
|
21
|
+
|
22
|
+
## v1.0.0
|
23
|
+
|
24
|
+
### Break Changes
|
25
|
+
|
26
|
+
- Drops `find` override method to avoid finding on a hash that looks like a number issue [#1](https://github.com/wbotelhos/idy/issues/1)
|
27
|
+
|
1
28
|
## v0.1.3
|
2
29
|
|
3
|
-
|
4
|
-
|
5
|
-
|
30
|
+
### Features
|
31
|
+
|
32
|
+
- Added `find` method to find directly via idy returning `nil` when the record is not found;
|
33
|
+
- Added `find!` method to find directly via idy raising an error when the record is not found.
|
6
34
|
|
7
|
-
|
8
|
-
|
35
|
+
### Fixes
|
36
|
+
|
37
|
+
- String that is integer-like is not passed to the original find method.
|
9
38
|
|
10
39
|
## v0.1.2
|
11
40
|
|
12
|
-
|
13
|
-
|
41
|
+
### Fixes
|
42
|
+
|
43
|
+
- Set options on a prefixed var to avoid conflict.
|
14
44
|
|
15
45
|
## v0.1.1
|
16
46
|
|
17
|
-
|
18
|
-
|
47
|
+
### Fixes
|
48
|
+
|
49
|
+
- v0.1.0 had a problem with `push`.
|
19
50
|
|
20
51
|
## v0.1.0
|
21
52
|
|
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# Idy
|
2
2
|
|
3
|
-
[![
|
3
|
+
[![CI](https://github.com/wbotelhos/idy/workflows/CI/badge.svg)](https://github.com/wbotelhos/idy/actions)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/idy.svg)](https://badge.fury.io/rb/idy)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/f312587b4f126bb13e85/maintainability)](https://codeclimate.com/github/wbotelhos/idy/maintainability)
|
6
|
+
[![Coverage](https://codecov.io/gh/wbotelhos/idy/branch/main/graph/badge.svg)](https://codecov.io/gh/wbotelhos/idy)
|
7
|
+
[![Sponsor](https://img.shields.io/badge/sponsor-%3C3-green)](https://www.patreon.com/wbotelhos)
|
5
8
|
|
6
9
|
An ID obfuscator for ActiveRecord.
|
7
10
|
|
@@ -45,7 +48,7 @@ Article.new(id: 1).idy
|
|
45
48
|
# My
|
46
49
|
```
|
47
50
|
|
48
|
-
It will build
|
51
|
+
It will build your Rails URL with that ID too:
|
49
52
|
|
50
53
|
```ruby
|
51
54
|
Article.new(id: 1).to_param
|
@@ -60,7 +63,7 @@ If you want a *unbreakable* hash, it is not for you.
|
|
60
63
|
## Collision
|
61
64
|
|
62
65
|
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/
|
66
|
+
by default, the class name is used as a [Salt](https://en.wikipedia.org/wiki/Salt_cryptography).
|
64
67
|
|
65
68
|
```ruby
|
66
69
|
Article.new(id: 1).idy
|
@@ -85,6 +88,25 @@ Article.new(id: 1).idy
|
|
85
88
|
# 9A
|
86
89
|
```
|
87
90
|
|
91
|
+
## Idy
|
92
|
+
|
93
|
+
As you could see, the method `idy`, returns the hash representation of your ID:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
Article.new(id: 1).idy
|
97
|
+
# My
|
98
|
+
```
|
99
|
+
|
100
|
+
If you want get all idys from a collection, just map it:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
Article.create
|
104
|
+
Article.create
|
105
|
+
|
106
|
+
Article.select(:id).map(&:idy)
|
107
|
+
# ["My", "aL"]
|
108
|
+
```
|
109
|
+
|
88
110
|
## Find
|
89
111
|
|
90
112
|
Since you add the `idy` callback to your model, `find` method will be decorated:
|
@@ -125,13 +147,31 @@ Article.findy! 'missing'
|
|
125
147
|
# ActiveRecord::RecordNotFound: Couldn't find Article with 'idy'="missing"
|
126
148
|
```
|
127
149
|
|
150
|
+
## Functions
|
151
|
+
|
152
|
+
You can encode a number manually:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
Model.idy_encode(idy)
|
156
|
+
```
|
157
|
+
|
158
|
+
You can decode an idy in case you want to use the ActiveRecord methods with the original ID:
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
Model.idy_decode(idy)
|
162
|
+
```
|
163
|
+
|
164
|
+
## Testing
|
165
|
+
Check if your model responds to idy method:
|
166
|
+
|
167
|
+
### RSpec
|
168
|
+
```ruby
|
169
|
+
it { is_expected.to respond_to(:idy) }
|
170
|
+
```
|
171
|
+
|
128
172
|
## Inspiration
|
129
173
|
|
130
|
-
It was
|
174
|
+
It was inspired and improved from:
|
131
175
|
|
132
176
|
- [obfuscate_id](https://github.com/namick/obfuscate_id)
|
133
177
|
- [hashids_uri](https://github.com/danieldraper/hashids_uri)
|
134
|
-
|
135
|
-
## Love it!
|
136
|
-
|
137
|
-
Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=idy) or [Gratipay](https://gratipay.com/~wbotelhos). Thanks! (:
|
data/lib/idy/extension.rb
CHANGED
@@ -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
|
@@ -58,7 +46,7 @@ module Idy
|
|
58
46
|
def idy_default_salt
|
59
47
|
alphabet = Array('a'..'z')
|
60
48
|
|
61
|
-
indexes = name.downcase.split('').map do |char|
|
49
|
+
indexes = name.downcase.tr(':', '').split('').map do |char|
|
62
50
|
alphabet.index(char) + 1
|
63
51
|
end
|
64
52
|
|
@@ -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
|
data/lib/idy/version.rb
CHANGED
metadata
CHANGED
@@ -1,51 +1,45 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: idy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Botelho
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: hashids
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
34
|
-
- - "<"
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: '6'
|
33
|
+
version: '0'
|
37
34
|
type: :runtime
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
38
|
- - ">="
|
42
39
|
- !ruby/object:Gem::Version
|
43
|
-
version: '
|
44
|
-
- - "<"
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: '6'
|
40
|
+
version: '0'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
42
|
+
name: codecov
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
50
44
|
requirements:
|
51
45
|
- - ">="
|
@@ -59,7 +53,7 @@ dependencies:
|
|
59
53
|
- !ruby/object:Gem::Version
|
60
54
|
version: '0'
|
61
55
|
- !ruby/object:Gem::Dependency
|
62
|
-
name:
|
56
|
+
name: database_cleaner
|
63
57
|
requirement: !ruby/object:Gem::Requirement
|
64
58
|
requirements:
|
65
59
|
- - ">="
|
@@ -73,7 +67,7 @@ dependencies:
|
|
73
67
|
- !ruby/object:Gem::Version
|
74
68
|
version: '0'
|
75
69
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
70
|
+
name: pry-byebug
|
77
71
|
requirement: !ruby/object:Gem::Requirement
|
78
72
|
requirements:
|
79
73
|
- - ">="
|
@@ -87,7 +81,7 @@ dependencies:
|
|
87
81
|
- !ruby/object:Gem::Version
|
88
82
|
version: '0'
|
89
83
|
- !ruby/object:Gem::Dependency
|
90
|
-
name:
|
84
|
+
name: rspec-rails
|
91
85
|
requirement: !ruby/object:Gem::Requirement
|
92
86
|
requirements:
|
93
87
|
- - ">="
|
@@ -101,7 +95,7 @@ dependencies:
|
|
101
95
|
- !ruby/object:Gem::Version
|
102
96
|
version: '0'
|
103
97
|
- !ruby/object:Gem::Dependency
|
104
|
-
name: rubocop
|
98
|
+
name: rubocop-performance
|
105
99
|
requirement: !ruby/object:Gem::Requirement
|
106
100
|
requirements:
|
107
101
|
- - ">="
|
@@ -115,7 +109,7 @@ dependencies:
|
|
115
109
|
- !ruby/object:Gem::Version
|
116
110
|
version: '0'
|
117
111
|
- !ruby/object:Gem::Dependency
|
118
|
-
name:
|
112
|
+
name: rubocop-rspec
|
119
113
|
requirement: !ruby/object:Gem::Requirement
|
120
114
|
requirements:
|
121
115
|
- - ">="
|
@@ -129,7 +123,7 @@ dependencies:
|
|
129
123
|
- !ruby/object:Gem::Version
|
130
124
|
version: '0'
|
131
125
|
- !ruby/object:Gem::Dependency
|
132
|
-
name:
|
126
|
+
name: sqlite3
|
133
127
|
requirement: !ruby/object:Gem::Requirement
|
134
128
|
requirements:
|
135
129
|
- - ">="
|
@@ -146,7 +140,10 @@ description: An ID obfuscator for ActiveRecord.
|
|
146
140
|
email: wbotelhos@gmail.com
|
147
141
|
executables: []
|
148
142
|
extensions: []
|
149
|
-
extra_rdoc_files:
|
143
|
+
extra_rdoc_files:
|
144
|
+
- CHANGELOG.md
|
145
|
+
- LICENSE
|
146
|
+
- README.md
|
150
147
|
files:
|
151
148
|
- CHANGELOG.md
|
152
149
|
- LICENSE
|
@@ -154,34 +151,11 @@ files:
|
|
154
151
|
- lib/idy.rb
|
155
152
|
- lib/idy/extension.rb
|
156
153
|
- lib/idy/version.rb
|
157
|
-
- spec/lib/idy/extension/find_by_spec.rb
|
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
|
161
|
-
- spec/lib/idy/extension/idy_decode_spec.rb
|
162
|
-
- spec/lib/idy/extension/idy_default_salt_spec.rb
|
163
|
-
- spec/lib/idy/extension/idy_encode_spec.rb
|
164
|
-
- spec/lib/idy/extension/idy_spec.rb
|
165
|
-
- spec/lib/idy/extension/lock_spec.rb
|
166
|
-
- spec/lib/idy/extension/options_spec.rb
|
167
|
-
- spec/lib/idy/extension/salt_spec.rb
|
168
|
-
- spec/lib/idy/extension/to_param_spec.rb
|
169
|
-
- spec/rails_helper.rb
|
170
|
-
- spec/support/common.rb
|
171
|
-
- spec/support/database_cleaner.rb
|
172
|
-
- spec/support/db/migrate/create_tables.rb
|
173
|
-
- spec/support/migrate.rb
|
174
|
-
- spec/support/models/abcdefghijklm.rb
|
175
|
-
- spec/support/models/article.rb
|
176
|
-
- spec/support/models/clean.rb
|
177
|
-
- spec/support/models/comment.rb
|
178
|
-
- spec/support/models/post.rb
|
179
|
-
- spec/support/models/user.rb
|
180
154
|
homepage: https://github.com/wbotelhos/idy
|
181
155
|
licenses:
|
182
156
|
- MIT
|
183
157
|
metadata: {}
|
184
|
-
post_install_message:
|
158
|
+
post_install_message:
|
185
159
|
rdoc_options: []
|
186
160
|
require_paths:
|
187
161
|
- lib
|
@@ -196,32 +170,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
196
170
|
- !ruby/object:Gem::Version
|
197
171
|
version: '0'
|
198
172
|
requirements: []
|
199
|
-
|
200
|
-
|
201
|
-
signing_key:
|
173
|
+
rubygems_version: 3.2.22
|
174
|
+
signing_key:
|
202
175
|
specification_version: 4
|
203
176
|
summary: An ID obfuscator for ActiveRecord.
|
204
|
-
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
|
-
- spec/support/migrate.rb
|
222
|
-
- spec/support/models/abcdefghijklm.rb
|
223
|
-
- spec/support/models/article.rb
|
224
|
-
- spec/support/models/clean.rb
|
225
|
-
- spec/support/models/comment.rb
|
226
|
-
- spec/support/models/post.rb
|
227
|
-
- spec/support/models/user.rb
|
177
|
+
test_files: []
|
@@ -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,24 +0,0 @@
|
|
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
|
@@ -1,24 +0,0 @@
|
|
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
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe '#idy_decode' do
|
6
|
-
context 'with no given salt' do
|
7
|
-
it 'undos the obfuscation of id with default class salt' do
|
8
|
-
expect(Article.idy_decode('My')).to eq 1
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'with empty salt' do
|
13
|
-
let!(:salt) { '' }
|
14
|
-
|
15
|
-
it 'undos the obfuscation of id with given salt' do
|
16
|
-
expect(Article.idy_decode('jR', salt: salt)).to eq 1
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'with salt' do
|
21
|
-
context 'as string' do
|
22
|
-
let!(:salt) { 'salt' }
|
23
|
-
|
24
|
-
it 'undos the obfuscation of id with given salt' do
|
25
|
-
expect(Article.idy_decode('XG', salt: salt)).to eq 1
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'as number' do
|
30
|
-
let!(:salt) { 1 }
|
31
|
-
|
32
|
-
it 'undos the obfuscation of id with given salt as string' do
|
33
|
-
expect(Article.idy_decode('kL', salt: 1)).to eq 1
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe '#idy_default_salt' do
|
6
|
-
context 'when class name has a big length' do
|
7
|
-
it 'is bases on the first 10 class letter position index on alphabet' do
|
8
|
-
expect(Abcdefghijklm.idy_default_salt).to eq '12345678910'
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe '#idy_encode' do
|
6
|
-
context 'with no given salt' do
|
7
|
-
let!(:model) { Article.new id: 1 }
|
8
|
-
|
9
|
-
it 'obfuscates the id with default class salt' do
|
10
|
-
expect(model.class.idy_encode(model.id)).to eq 'My'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'with empty salt' do
|
15
|
-
let!(:model) { Article.new id: 1 }
|
16
|
-
let!(:salt) { '' }
|
17
|
-
|
18
|
-
it 'obfuscates the id with empty salt' do
|
19
|
-
expect(model.class.idy_encode(model.id, salt: salt)).to eq 'jR'
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'with salt' do
|
24
|
-
let!(:model) { Article.new id: 1 }
|
25
|
-
|
26
|
-
context 'as string' do
|
27
|
-
let!(:salt) { 'salt' }
|
28
|
-
|
29
|
-
it 'obfuscates the id with given salt' do
|
30
|
-
expect(model.class.idy_encode(model.id, salt: salt)).to eq 'XG'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'as number' do
|
35
|
-
let!(:salt) { 1 }
|
36
|
-
|
37
|
-
it 'obfuscates the id with given salt as string' do
|
38
|
-
expect(model.class.idy_encode(model.id, salt: salt)).to eq 'kL'
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context 'with nil id' do
|
44
|
-
let!(:model) { Article.new }
|
45
|
-
|
46
|
-
it 'returns nil' do
|
47
|
-
expect(model.class.idy_encode(model.id)).to be_nil
|
48
|
-
end
|
49
|
-
end
|
50
|
-
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
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe '.idy_options' do
|
6
|
-
context 'when options is not given' do
|
7
|
-
it 'returns a default options with a salt generated based on model name' do
|
8
|
-
expect(Article.idy_options).to eq(salt: article_default_salt)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'when salt is given' do
|
13
|
-
it 'returns a default options with a salt equal the given one' do
|
14
|
-
expect(Comment.idy_options).to eq(salt: 'salt')
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe ':salt' do
|
6
|
-
describe '#salt' do
|
7
|
-
before do
|
8
|
-
allow(Clean).to receive(:idy_options) { { salt: :salty } }
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'fetchs the salt options' do
|
12
|
-
expect(Clean.salt).to eq(:salty)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe '.salt' do
|
17
|
-
before { allow(Clean).to receive(:salt) { :salt } }
|
18
|
-
|
19
|
-
it 'delegates to class method' do
|
20
|
-
expect(Clean.new.salt).to eq :salt
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails_helper'
|
4
|
-
|
5
|
-
RSpec.describe '.to_param' do
|
6
|
-
context 'when object does not acts as idy' do
|
7
|
-
subject { Clean.new id: 1 }
|
8
|
-
|
9
|
-
it 'behaves as default' do
|
10
|
-
expect(subject.to_param).to eq '1'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'when object acts as idy' do
|
15
|
-
context 'with no given salt' do
|
16
|
-
let!(:model_1) { Article.new id: 1 }
|
17
|
-
|
18
|
-
it 'generates a hash' do
|
19
|
-
expect(model_1.to_param).to eq 'My'
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'with comparison' do
|
23
|
-
let!(:model_2) { Post.new }
|
24
|
-
|
25
|
-
context 'with other object with same id' do
|
26
|
-
before { model_2.id = 1 }
|
27
|
-
|
28
|
-
it 'generates different hash' do
|
29
|
-
expect(model_1.to_param).not_to eq model_2.to_param
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when object is not persisted' do
|
35
|
-
context 'and has no id' do
|
36
|
-
subject { Article.new }
|
37
|
-
|
38
|
-
specify { expect(subject.to_param).to be_nil }
|
39
|
-
end
|
40
|
-
|
41
|
-
context 'and has id' do
|
42
|
-
subject { Article.new id: 1 }
|
43
|
-
|
44
|
-
it 'generates a hash' do
|
45
|
-
expect(model_1.to_param).to eq 'My'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/spec/rails_helper.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
ENV['RAILS_ENV'] ||= 'test'
|
4
|
-
|
5
|
-
require 'active_record/railtie'
|
6
|
-
require 'idy'
|
7
|
-
require 'pry-byebug'
|
8
|
-
|
9
|
-
ActiveRecord::Base.establish_connection adapter: :sqlite3, database: ':memory:'
|
10
|
-
|
11
|
-
Dir[File.expand_path('support/**/*.rb', __dir__)].each { |file| require file }
|
12
|
-
|
13
|
-
def article_default_salt
|
14
|
-
'1182093125'
|
15
|
-
end
|
16
|
-
|
17
|
-
def clean_default_salt
|
18
|
-
'3125114'
|
19
|
-
end
|
20
|
-
|
21
|
-
def comment_default_salt
|
22
|
-
''
|
23
|
-
end
|
24
|
-
|
25
|
-
def user_default_salt
|
26
|
-
'1182093125'
|
27
|
-
end
|
data/spec/support/common.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'database_cleaner'
|
4
|
-
|
5
|
-
RSpec.configure do |config|
|
6
|
-
config.before :suite do
|
7
|
-
DatabaseCleaner.strategy = :transaction
|
8
|
-
|
9
|
-
DatabaseCleaner.clean_with :transaction
|
10
|
-
end
|
11
|
-
|
12
|
-
config.before :each do
|
13
|
-
DatabaseCleaner.start
|
14
|
-
end
|
15
|
-
|
16
|
-
config.around do |spec|
|
17
|
-
DatabaseCleaner.cleaning do
|
18
|
-
spec.run
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class CreateTables < ActiveRecord::Migration[5.0]
|
4
|
-
def change
|
5
|
-
create_table :abcdefghijklms do |t|
|
6
|
-
end
|
7
|
-
|
8
|
-
create_table :articles do |t|
|
9
|
-
t.string :title
|
10
|
-
end
|
11
|
-
|
12
|
-
create_table :cleans do |t|
|
13
|
-
end
|
14
|
-
|
15
|
-
create_table :comments do |t|
|
16
|
-
t.string :body
|
17
|
-
|
18
|
-
t.references :article, index: true, foreign_key: true
|
19
|
-
end
|
20
|
-
|
21
|
-
create_table :posts do |t|
|
22
|
-
t.string :title
|
23
|
-
end
|
24
|
-
|
25
|
-
create_table :users do |t|
|
26
|
-
t.string :name
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/spec/support/migrate.rb
DELETED
data/spec/support/models/post.rb
DELETED
data/spec/support/models/user.rb
DELETED