blind_index 2.0.2 → 2.1.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +30 -6
- data/lib/blind_index/extensions.rb +9 -2
- data/lib/blind_index/mongoid.rb +13 -4
- data/lib/blind_index/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6749c94ee9432d60e3095e77a3948f480c345b793555dadf5b25261247e406b
|
4
|
+
data.tar.gz: e29eb3a994de26b1f67cb31a16bbaef66742aea0cf366ea5830247421c30a5b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a195bd440c17774dcaf46196c34f2965a78ef7a95499501534fba3161cafc82af085a0046f9bc5e422e7b38476040c389ae3440cf0589db190cd9240f4c6521
|
7
|
+
data.tar.gz: b1ba60817538b3fda557a392e136a2a61c80341332f16821a36e60b8f573659574fbfe36d85969f7730b172451de5e0c5a369d6a4a11c0e65608cfe369229c37
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -26,13 +26,13 @@ Add this line to your application’s Gemfile:
|
|
26
26
|
gem 'blind_index'
|
27
27
|
```
|
28
28
|
|
29
|
-
##
|
29
|
+
## Prep
|
30
30
|
|
31
31
|
Your model should already be set up with Lockbox or attr_encrypted. The examples are for a `User` model with `encrypts :email` or `attr_encrypted :email`. See the full examples for [Lockbox](https://ankane.org/securing-user-emails-lockbox) and [attr_encrypted](https://ankane.org/securing-user-emails-in-rails) if needed.
|
32
32
|
|
33
33
|
Also, if you use attr_encrypted, [generate a key](#key-generation).
|
34
34
|
|
35
|
-
|
35
|
+
## Getting Started
|
36
36
|
|
37
37
|
Create a migration to add a column for the blind index
|
38
38
|
|
@@ -69,9 +69,19 @@ And query away
|
|
69
69
|
User.where(email: "test@example.org")
|
70
70
|
```
|
71
71
|
|
72
|
+
## Expressions
|
73
|
+
|
74
|
+
You can apply expressions to attributes before indexing and searching. This gives you the the ability to perform case-insensitive searches and more.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class User < ApplicationRecord
|
78
|
+
blind_index :email, expression: ->(v) { v.downcase }
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
72
82
|
## Validations
|
73
83
|
|
74
|
-
|
84
|
+
You can use blind indexes for uniqueness validations.
|
75
85
|
|
76
86
|
```ruby
|
77
87
|
class User < ApplicationRecord
|
@@ -79,15 +89,27 @@ class User < ApplicationRecord
|
|
79
89
|
end
|
80
90
|
```
|
81
91
|
|
82
|
-
We
|
92
|
+
We recommend adding a unique index to the blind index column through a database migration.
|
83
93
|
|
84
|
-
|
94
|
+
```ruby
|
95
|
+
add_index :users, :email_bidx, unique: true
|
96
|
+
```
|
85
97
|
|
86
|
-
|
98
|
+
For `allow_blank: true`, use:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class User < ApplicationRecord
|
102
|
+
blind_index :email, expression: ->(v) { v.presence }
|
103
|
+
validates :email, uniqueness: {allow_blank: true}
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
For `case_sensitive: false`, use:
|
87
108
|
|
88
109
|
```ruby
|
89
110
|
class User < ApplicationRecord
|
90
111
|
blind_index :email, expression: ->(v) { v.downcase }
|
112
|
+
validates :email, uniqueness: true # for best performance, leave out {case_sensitive: false}
|
91
113
|
end
|
92
114
|
```
|
93
115
|
|
@@ -436,3 +458,5 @@ cd blind_index
|
|
436
458
|
bundle install
|
437
459
|
bundle exec rake test
|
438
460
|
```
|
461
|
+
|
462
|
+
For security issues, send an email to the address on [this page](https://github.com/ankane).
|
@@ -29,10 +29,18 @@ module BlindIndex
|
|
29
29
|
end
|
30
30
|
|
31
31
|
module UniquenessValidator
|
32
|
+
def validate_each(record, attribute, value)
|
33
|
+
klass = record.class
|
34
|
+
if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
|
35
|
+
value = record.read_attribute_for_validation(bi[:bidx_attribute])
|
36
|
+
end
|
37
|
+
super(record, attribute, value)
|
38
|
+
end
|
39
|
+
|
40
|
+
# change attribute name here instead of validate_each for better error message
|
32
41
|
if ActiveRecord::VERSION::STRING >= "5.2"
|
33
42
|
def build_relation(klass, attribute, value)
|
34
43
|
if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
|
35
|
-
value = BlindIndex.generate_bidx(value, **bi)
|
36
44
|
attribute = bi[:bidx_attribute]
|
37
45
|
end
|
38
46
|
super(klass, attribute, value)
|
@@ -40,7 +48,6 @@ module BlindIndex
|
|
40
48
|
else
|
41
49
|
def build_relation(klass, table, attribute, value)
|
42
50
|
if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
|
43
|
-
value = BlindIndex.generate_bidx(value, **bi)
|
44
51
|
attribute = bi[:bidx_attribute]
|
45
52
|
end
|
46
53
|
super(klass, table, attribute, value)
|
data/lib/blind_index/mongoid.rb
CHANGED
@@ -26,9 +26,9 @@ module BlindIndex
|
|
26
26
|
|
27
27
|
criterion[bidx_key] =
|
28
28
|
if value.is_a?(Array)
|
29
|
-
value.map { |v| BlindIndex.generate_bidx(v, bi) }
|
29
|
+
value.map { |v| BlindIndex.generate_bidx(v, **bi) }
|
30
30
|
else
|
31
|
-
BlindIndex.generate_bidx(value, bi)
|
31
|
+
BlindIndex.generate_bidx(value, **bi)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -39,9 +39,18 @@ module BlindIndex
|
|
39
39
|
end
|
40
40
|
|
41
41
|
module UniquenessValidator
|
42
|
+
def validate_each(record, attribute, value)
|
43
|
+
klass = record.class
|
44
|
+
if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
|
45
|
+
value = record.read_attribute_for_validation(bi[:bidx_attribute])
|
46
|
+
end
|
47
|
+
super(record, attribute, value)
|
48
|
+
end
|
49
|
+
|
50
|
+
# change attribute name here instead of validate_each for better error message
|
42
51
|
def create_criteria(base, document, attribute, value)
|
43
|
-
|
44
|
-
|
52
|
+
klass = document.class
|
53
|
+
if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
|
45
54
|
attribute = bi[:bidx_attribute]
|
46
55
|
end
|
47
56
|
super(base, document, attribute, value)
|
data/lib/blind_index/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blind_index
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06
|
11
|
+
date: 2020-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|