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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06112a25e062c04740b0a01173300d37c364c05ef3bef0de0fa214e3dfbca61e
4
- data.tar.gz: 41c534b7fd3266559821bba50d122a08adbf3215212304dc7c911cb62e204596
3
+ metadata.gz: f6749c94ee9432d60e3095e77a3948f480c345b793555dadf5b25261247e406b
4
+ data.tar.gz: e29eb3a994de26b1f67cb31a16bbaef66742aea0cf366ea5830247421c30a5b0
5
5
  SHA512:
6
- metadata.gz: 7f3369a1b5ba9b36be21e18c3a41ea9060c779f7cb2b92c2b2539144acd5fb01d99a797b123f6f7b1a97dd53b92c6ff45ba4c5517e43bf99f3e8363875ade522
7
- data.tar.gz: 0e5a857dd66532f72635c5ec13f56c4e864f8197533a3d300a5630cda8e9dc2d62b3c3bf771c794df2af668a2249830f6d0ba4e06a396f303131465f17dbd6a2
6
+ metadata.gz: 4a195bd440c17774dcaf46196c34f2965a78ef7a95499501534fba3161cafc82af085a0046f9bc5e422e7b38476040c389ae3440cf0589db190cd9240f4c6521
7
+ data.tar.gz: b1ba60817538b3fda557a392e136a2a61c80341332f16821a36e60b8f573659574fbfe36d85969f7730b172451de5e0c5a369d6a4a11c0e65608cfe369229c37
@@ -1,3 +1,8 @@
1
+ ## 2.1.0 (2020-07-06)
2
+
3
+ - Improved performance of uniqueness validations
4
+ - Fixed deprecation warnings in Ruby 2.7 with Mongoid
5
+
1
6
  ## 2.0.2 (2020-06-01)
2
7
 
3
8
  - Improved error message for bad key length
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
- ## Getting Started
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
- To prevent duplicates, use:
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 also recommend adding a unique index to the blind index column through a database migration.
92
+ We recommend adding a unique index to the blind index column through a database migration.
83
93
 
84
- ## Expressions
94
+ ```ruby
95
+ add_index :users, :email_bidx, unique: true
96
+ ```
85
97
 
86
- You can apply expressions to attributes before indexing and searching. This gives you the the ability to perform case-insensitive searches and more.
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)
@@ -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
- if base.respond_to?(:blind_indexes) && (bi = base.blind_indexes[attribute])
44
- value = BlindIndex.generate_bidx(value, bi)
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)
@@ -1,3 +1,3 @@
1
1
  module BlindIndex
2
- VERSION = "2.0.2"
2
+ VERSION = "2.1.0"
3
3
  end
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.2
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-01 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport