database_validations 0.9.2 → 0.9.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
  SHA256:
3
- metadata.gz: fc082896680bee591fcbf04d9f9fb469c72c9c1e2c259f5093341e2c1448cd15
4
- data.tar.gz: 474977490d71108f408c448bf9b18d404218b2b17f8f3cf7b48a2f945e4535d5
3
+ metadata.gz: 37040902b8f9dd94cbfcc1785129eff2ac2f75eb085f567299852a03f4a83cfa
4
+ data.tar.gz: c6a2a3331225b8344c23244692296a982ed25405be15650c32bc0815e227c20a
5
5
  SHA512:
6
- metadata.gz: eb8e2d716fa3bb7bcb0d04f9e9db16f00587d2a7a3742b3367a0b3bf9bee9c883d0ec4f7144f08f0f7045ebed8a7297f33ea77d4bd192102b56e4087f08cff15
7
- data.tar.gz: 0da69998634da7b5b9250ce11e1fda4f6dd3809c57bdf5e302a85ad5c7c25d728a57c21a00d98d5e948bac206ae2d46dab52aa0d01714628826f8691ab8883a4
6
+ metadata.gz: 27f9f74a6f081f343bbe94da77956cd0c06419dcf321d2576a6238f0b2bddb5dd967861deaffe88ab47929e4b3837825fc9c17bf95b4fba83f5fbfd4b07d04e0
7
+ data.tar.gz: 86f379332348da451c1f2d02e3e4096e528f9e00c25f10fdb8881512d97c10ffdf9ab5cffdc3f4e9ce6ea475e0d3d0a0d75b728fed700f7606ccf07cacfe9bdf
@@ -24,13 +24,19 @@ module DatabaseValidations
24
24
  keys.each do |key|
25
25
  attribute_validator = instance._db_validators[key]
26
26
 
27
- if attribute_validator
28
- attribute_validator.validator.apply_error(instance, attribute_validator.attribute)
29
- return true
30
- end
27
+ next unless attribute_validator
28
+
29
+ return process_validator(instance, attribute_validator)
31
30
  end
32
31
 
33
32
  false
34
33
  end
34
+
35
+ def process_validator(instance, attribute_validator)
36
+ return false unless attribute_validator.validator.perform_db_validation?
37
+
38
+ attribute_validator.validator.apply_error(instance, attribute_validator.attribute)
39
+ true
40
+ end
35
41
  end
36
42
  end
@@ -21,6 +21,10 @@ module DatabaseValidations
21
21
  Checkers::DbPresenceValidator.validate!(self)
22
22
  end
23
23
 
24
+ def perform_db_validation?
25
+ true
26
+ end
27
+
24
28
  # TODO: add support of optional db_belongs_to
25
29
  def validate(record)
26
30
  if record._database_validations_fallback
@@ -1,5 +1,7 @@
1
1
  module DatabaseValidations
2
2
  class DbUniquenessValidator < ActiveRecord::Validations::UniquenessValidator
3
+ DEFAULT_MODE = :optimized
4
+
3
5
  attr_reader :index_name, :where, :klass
4
6
 
5
7
  # Used to make 3rd party libraries work correctly
@@ -19,8 +21,7 @@ module DatabaseValidations
19
21
  options[:conditions] = -> { where(condition) }
20
22
  end
21
23
 
22
- @index_name = options.delete(:index_name) if options.key?(:index_name)
23
- @where = options.delete(:where) if options.key?(:where)
24
+ handle_custom_options(options)
24
25
 
25
26
  super
26
27
 
@@ -28,8 +29,12 @@ module DatabaseValidations
28
29
  Checkers::DbUniquenessValidator.validate!(self)
29
30
  end
30
31
 
32
+ def perform_db_validation?
33
+ @mode != :standard
34
+ end
35
+
31
36
  def validate(record)
32
- super if record._database_validations_fallback
37
+ super if perform_query? || record._database_validations_fallback
33
38
  end
34
39
 
35
40
  def apply_error(instance, attribute)
@@ -38,6 +43,18 @@ module DatabaseValidations
38
43
 
39
44
  instance.errors.add(attribute, :taken, error_options)
40
45
  end
46
+
47
+ private
48
+
49
+ def handle_custom_options(options)
50
+ @index_name = options.delete(:index_name) if options.key?(:index_name)
51
+ @where = options.delete(:where) if options.key?(:where)
52
+ @mode = (options.delete(:mode).presence || DEFAULT_MODE).to_sym
53
+ end
54
+
55
+ def perform_query?
56
+ @mode != :optimized
57
+ end
41
58
  end
42
59
 
43
60
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module DatabaseValidations
2
- VERSION = '0.9.2'.freeze
2
+ VERSION = '0.9.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-16 00:00:00.000000000 Z
11
+ date: 2020-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -223,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  - !ruby/object:Gem::Version
224
224
  version: '0'
225
225
  requirements: []
226
- rubygems_version: 3.1.2
226
+ rubygems_version: 3.0.8
227
227
  signing_key:
228
228
  specification_version: 4
229
229
  summary: Provide compatibility between database constraints and ActiveRecord validations