database_validations 1.0.1 → 1.1.0

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: 84632667aa6aec235e306d5443ca94804d19541330fe13ec52cfcc80c44e3719
4
- data.tar.gz: f7619dd8fd5093302a959f978839433a38fa0735b4d6f105110b6042758eb64a
3
+ metadata.gz: d4f713f4e6cffee98b4e7825fa18de17fdd25780659d0bea75f8fbc90ada6ecd
4
+ data.tar.gz: 73d3f1d54aaea77a61fbc3fcb221348be0ffcb6430eaeec98c09d6cd1e4ca9a7
5
5
  SHA512:
6
- metadata.gz: b38c529569458eac14defeec0216018f5db85f9a1a82dbf9793b3c32fc4d378dceafb3fbbb9a2094374db898a49f36fbef8be2d09f54b10c0f22fed036945a73
7
- data.tar.gz: d6f93d5f638ee921f8d01980310866f0bc811909fd7963e7c901c9bc7e0122a891ed2813af37653e4c9ccb7bac92084593794f2d396f66fa4fb9b10ee9faca9d
6
+ metadata.gz: 84927dd603f9f8bf8de7fbe98b5414e40de5c30f3d6adb95f5e1c77eef85c8fb7caef58c618e70ce3272feb6407cba159ce36b294d871eb793b30042b3bc5c28
7
+ data.tar.gz: b51be17960b5f2ad13ca0c6e128dc3352d5bfc53b94ecd1ea40edf1448da300ba546cf1c94e8965b839f29ea693bb1369a4bee7b1ba92f90e7d256544eec906c
@@ -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)
@@ -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.1'.freeze
2
+ VERSION = '1.1.0'.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.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-15 00:00:00.000000000 Z
11
+ date: 2021-11-19 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.2.22
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