database_validations 1.0.0 → 1.1.1

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: 2e13dbc2023a631be57aaacd12ec2c75ca9b7b355e7015737df4b760713b5756
4
- data.tar.gz: 8b84441e52646fba283120a8e33d40bdb43162f31f202e2cd07e6f5fa6dbb03e
3
+ metadata.gz: 397b350ccf27a15694e155a4bb21bae6a01fe94cacceaa14aff89d1db6f542c1
4
+ data.tar.gz: da042f3adbf3b00a1b85e01a8dd740b23dec523379adc1c5500f1a367f2a0391
5
5
  SHA512:
6
- metadata.gz: 324f867954cf0b2660431e6da9b5851f3ccae84b6ff2e5d10de6149b336387adc7c07a4e295748c75663a18822436c88578c6d246e869238f56d0e3563bc1f2e
7
- data.tar.gz: 3d99ab395db1b3b493e372c22674283cd7822bd6de89cbcd71b08218cfec96eee709538dd89a872977edd936434564798a20e1af8e837c1f886fbd3a9f8e345c
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
- validate_foreign_keys! unless ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK']
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! # rubocop:disable Metrics/AbcSize
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
- validate_indexes! unless ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK']
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(validator.klass)
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.perform_db_validation?
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
@@ -21,8 +21,8 @@ module DatabaseValidations
21
21
  Checkers::DbPresenceValidator.validate!(self)
22
22
  end
23
23
 
24
- def perform_db_validation?
25
- true
24
+ def perform_rescue?(validate)
25
+ validate != false
26
26
  end
27
27
 
28
28
  # TODO: add support of optional db_belongs_to
@@ -29,8 +29,8 @@ module DatabaseValidations
29
29
  Checkers::DbUniquenessValidator.validate!(self)
30
30
  end
31
31
 
32
- def perform_db_validation?
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?
@@ -1,3 +1,3 @@
1
1
  module DatabaseValidations
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.1'.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: 1.0.0
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: 2021-02-22 00:00:00.000000000 Z
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.1.2
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.