database_validations 1.0.0 → 1.1.1
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/lib/database_validations/lib/checkers/db_presence_validator.rb +4 -4
- data/lib/database_validations/lib/checkers/db_uniqueness_validator.rb +5 -3
- data/lib/database_validations/lib/rescuer.rb +7 -7
- data/lib/database_validations/lib/validations.rb +3 -8
- data/lib/database_validations/lib/validators/db_presence_validator.rb +2 -2
- data/lib/database_validations/lib/validators/db_uniqueness_validator.rb +4 -3
- data/lib/database_validations/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 397b350ccf27a15694e155a4bb21bae6a01fe94cacceaa14aff89d1db6f542c1
|
4
|
+
data.tar.gz: da042f3adbf3b00a1b85e01a8dd740b23dec523379adc1c5500f1a367f2a0391
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf357e666a546058018322985ffe4a342d3004d9105c11bcd9e9eeb9b904ce8ecc915960783987b6b57cab8cca26c63eb7d6a788e74f581dc2e8e566f3a32bf9
|
7
|
+
data.tar.gz: 8c9d658eff8041b6237c3b554b82dee7ca390a833aafdfd5d0fc4c7fa6f730241f5a9412c0a8b171ae4df1323e53e1fed796d6782df83b166a76cb78a26ab5e7
|
@@ -14,14 +14,14 @@ module DatabaseValidations
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def validate!
|
17
|
-
|
17
|
+
return if ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK']
|
18
|
+
|
19
|
+
validate_foreign_keys! unless validator.klass.abstract_class?
|
18
20
|
end
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
|
-
def validate_foreign_keys!
|
23
|
-
adapter = Adapters::BaseAdapter.new(validator.klass)
|
24
|
-
|
24
|
+
def validate_foreign_keys!(adapter = Adapters::BaseAdapter.new(validator.klass))
|
25
25
|
validator.attributes.each do |attribute|
|
26
26
|
reflection = validator.klass._reflect_on_association(attribute)
|
27
27
|
|
@@ -16,7 +16,9 @@ module DatabaseValidations
|
|
16
16
|
def validate!
|
17
17
|
validate_index_usage!
|
18
18
|
|
19
|
-
|
19
|
+
return if ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK']
|
20
|
+
|
21
|
+
validate_indexes!(validator.klass) unless validator.klass.abstract_class?
|
20
22
|
end
|
21
23
|
|
22
24
|
private
|
@@ -33,8 +35,8 @@ module DatabaseValidations
|
|
33
35
|
raise ArgumentError, "When index_name is provided validator can have only one attribute. See #{validator.inspect}"
|
34
36
|
end
|
35
37
|
|
36
|
-
def validate_indexes! # rubocop:disable Metrics/AbcSize
|
37
|
-
adapter = Adapters::BaseAdapter.new(
|
38
|
+
def validate_indexes!(klass) # rubocop:disable Metrics/AbcSize
|
39
|
+
adapter = Adapters::BaseAdapter.new(klass)
|
38
40
|
|
39
41
|
validator.attributes.map do |attribute|
|
40
42
|
columns = KeyGenerator.unify_columns(attribute, validator.options[:scope])
|
@@ -2,19 +2,19 @@ module DatabaseValidations
|
|
2
2
|
module Rescuer
|
3
3
|
module_function
|
4
4
|
|
5
|
-
def handled?(instance, error)
|
5
|
+
def handled?(instance, error, validate)
|
6
6
|
Storage.prepare(instance.class) unless Storage.prepared?(instance.class)
|
7
7
|
|
8
8
|
case error
|
9
9
|
when ActiveRecord::RecordNotUnique
|
10
|
-
process(instance, error, for_unique_index: :unique_index_name, for_db_uniqueness: :unique_error_columns)
|
10
|
+
process(validate, instance, error, for_unique_index: :unique_index_name, for_db_uniqueness: :unique_error_columns)
|
11
11
|
when ActiveRecord::InvalidForeignKey
|
12
|
-
process(instance, error, for_db_presence: :foreign_key_error_column)
|
12
|
+
process(validate, instance, error, for_db_presence: :foreign_key_error_column)
|
13
13
|
else false
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
def process(instance, error, key_types)
|
17
|
+
def process(validate, instance, error, key_types)
|
18
18
|
adapter = Adapters.factory(instance.class)
|
19
19
|
|
20
20
|
keys = key_types.map do |key_generator, error_processor|
|
@@ -26,14 +26,14 @@ module DatabaseValidations
|
|
26
26
|
|
27
27
|
next unless attribute_validator
|
28
28
|
|
29
|
-
return process_validator(instance, attribute_validator)
|
29
|
+
return process_validator(validate, instance, attribute_validator)
|
30
30
|
end
|
31
31
|
|
32
32
|
false
|
33
33
|
end
|
34
34
|
|
35
|
-
def process_validator(instance, attribute_validator)
|
36
|
-
return false unless attribute_validator.validator.
|
35
|
+
def process_validator(validate, instance, attribute_validator)
|
36
|
+
return false unless attribute_validator.validator.perform_rescue?(validate)
|
37
37
|
|
38
38
|
attribute_validator.validator.apply_error(instance, attribute_validator.attribute)
|
39
39
|
true
|
@@ -15,21 +15,16 @@ module DatabaseValidations
|
|
15
15
|
|
16
16
|
def create_or_update(*args, &block)
|
17
17
|
options = args.extract_options!
|
18
|
-
|
19
|
-
if options[:validate] == false
|
20
|
-
super
|
21
|
-
else
|
22
|
-
rescue_from_database_exceptions { super }
|
23
|
-
end
|
18
|
+
rescue_from_database_exceptions(options[:validate]) { super }
|
24
19
|
end
|
25
20
|
|
26
21
|
private
|
27
22
|
|
28
|
-
def rescue_from_database_exceptions(&block)
|
23
|
+
def rescue_from_database_exceptions(validate, &block)
|
29
24
|
self._database_validations_fallback = false
|
30
25
|
self.class.connection.transaction(requires_new: true, &block)
|
31
26
|
rescue ActiveRecord::InvalidForeignKey, ActiveRecord::RecordNotUnique => e
|
32
|
-
raise e unless Rescuer.handled?(self, e)
|
27
|
+
raise e unless Rescuer.handled?(self, e, validate)
|
33
28
|
|
34
29
|
raise ActiveRecord::RecordInvalid, self
|
35
30
|
end
|
@@ -29,8 +29,8 @@ module DatabaseValidations
|
|
29
29
|
Checkers::DbUniquenessValidator.validate!(self)
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
33
|
-
@mode != :standard
|
32
|
+
def perform_rescue?(validate)
|
33
|
+
(validate != false && @mode != :standard) || @rescue == :always
|
34
34
|
end
|
35
35
|
|
36
36
|
def validate(record)
|
@@ -41,7 +41,7 @@ module DatabaseValidations
|
|
41
41
|
error_options = options.except(:case_sensitive, :scope, :conditions)
|
42
42
|
error_options[:value] = instance.public_send(attribute)
|
43
43
|
|
44
|
-
instance.errors.add(attribute, :taken, error_options)
|
44
|
+
instance.errors.add(attribute, :taken, **error_options)
|
45
45
|
end
|
46
46
|
|
47
47
|
private
|
@@ -50,6 +50,7 @@ module DatabaseValidations
|
|
50
50
|
@index_name = options.delete(:index_name) if options.key?(:index_name)
|
51
51
|
@where = options.delete(:where) if options.key?(:where)
|
52
52
|
@mode = (options.delete(:mode).presence || DEFAULT_MODE).to_sym
|
53
|
+
@rescue = (options.delete(:rescue).presence || :default).to_sym
|
53
54
|
end
|
54
55
|
|
55
56
|
def perform_query?
|
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: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -208,7 +208,7 @@ homepage: https://github.com/toptal/database_validations
|
|
208
208
|
licenses:
|
209
209
|
- MIT
|
210
210
|
metadata: {}
|
211
|
-
post_install_message:
|
211
|
+
post_install_message:
|
212
212
|
rdoc_options: []
|
213
213
|
require_paths:
|
214
214
|
- lib
|
@@ -223,8 +223,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
223
223
|
- !ruby/object:Gem::Version
|
224
224
|
version: '0'
|
225
225
|
requirements: []
|
226
|
-
rubygems_version: 3.
|
227
|
-
signing_key:
|
226
|
+
rubygems_version: 3.0.8
|
227
|
+
signing_key:
|
228
228
|
specification_version: 4
|
229
229
|
summary: Provide compatibility between database constraints and ActiveRecord validations
|
230
230
|
with better performance and consistency.
|