database_consistency 1.1.4 → 1.1.5

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: 33bfe05b79b4c641cfa784c2e60894004910bfcbf594d3045ce51b586eec5812
4
- data.tar.gz: 20355a79ba3aa941f85764203474e8a16b5c91397ce008940f4ecb9a2e45d71a
3
+ metadata.gz: 8c43909523b8cb7b8369f55dc151bf84e32752276907a249ada7606edffbbdca
4
+ data.tar.gz: b2a27e403ab36224ab3f432f4bd4d8021e75306c000b8fd20c01ea2cd868d795
5
5
  SHA512:
6
- metadata.gz: 57e588223f73d55f10d00e47c7492de68849f3487a272293beb8eba3547bea679f4c2322a93fc2ac625177426252ba1d97650197515493a17218face6149b493
7
- data.tar.gz: d21a4eab903327f8fd7ea775828c45c72674f6edf58b1f8fb0889cd986a79adaf514a160bb30497b9b46003b180888b97ddfe94c25b68a106ed4e15a97d8d25b
6
+ metadata.gz: a7dac1b50e3e621c62f5bb00c96ef130bee3a862d70c3078dc29dd2538ca5f6a66f6772aa449ffed3771354a0514ab21e2b58df4b125dd62415f163b936667ee
7
+ data.tar.gz: f71af9349ed08f62b295c47cb32dc11a591a59cd44784e8f5ba00390c8011ccbc8597dc6cd63a5db4eee965f1ae0b758c8fa7487503c0f5469c27e492f7bfcd1
@@ -6,6 +6,8 @@ module DatabaseConsistency
6
6
  class NullConstraintChecker < ColumnChecker
7
7
  # Message templates
8
8
  VALIDATOR_MISSING = 'column is required in the database but do not have presence validator'
9
+ ASSOCIATION_VALIDATOR_MISSING = 'column is required in the database but do '\
10
+ 'not have presence validator for association (%a_n)'
9
11
 
10
12
  private
11
13
 
@@ -26,21 +28,23 @@ module DatabaseConsistency
26
28
  # | missing | fail |
27
29
  #
28
30
  # We consider PresenceValidation, InclusionValidation, ExclusionValidation, NumericalityValidator with nil,
29
- # or BelongsTo association using this column
31
+ # or required BelongsTo association using this column
30
32
  def check
31
33
  if valid?
32
34
  report_template(:ok)
35
+ elsif belongs_to_association
36
+ report_template(:fail, ASSOCIATION_VALIDATOR_MISSING.gsub('%a_n', belongs_to_association.name.to_s))
33
37
  else
34
38
  report_template(:fail, VALIDATOR_MISSING)
35
39
  end
36
40
  end
37
41
 
38
42
  def valid?
39
- validator?(ActiveModel::Validations::PresenceValidator) ||
40
- validator?(ActiveModel::Validations::InclusionValidator) ||
43
+ validator?(:presence, column.name) ||
44
+ validator?(:inclusion, column.name) ||
41
45
  numericality_validator_without_allow_nil? ||
42
46
  nil_exclusion_validator? ||
43
- belongs_to_association?
47
+ (belongs_to_association && validator?(:presence, belongs_to_association.name))
44
48
  end
45
49
 
46
50
  def primary_field?
@@ -52,28 +56,30 @@ module DatabaseConsistency
52
56
  end
53
57
 
54
58
  def nil_exclusion_validator?
55
- model.validators.grep(ActiveModel::Validations::ExclusionValidator).any? do |validator|
56
- Helper.check_inclusion?(validator.attributes, column.name) &&
59
+ model.validators.any? do |validator|
60
+ validator.kind == :exclusion &&
61
+ Helper.check_inclusion?(validator.attributes, column.name) &&
57
62
  validator.options[:in].include?(nil)
58
63
  end
59
64
  end
60
65
 
61
66
  def numericality_validator_without_allow_nil?
62
- model.validators.grep(ActiveModel::Validations::NumericalityValidator).any? do |validator|
63
- Helper.check_inclusion?(validator.attributes, column.name) &&
67
+ model.validators.any? do |validator|
68
+ validator.kind == :numericality &&
69
+ Helper.check_inclusion?(validator.attributes, column.name) &&
64
70
  !validator.options[:allow_nil]
65
71
  end
66
72
  end
67
73
 
68
- def validator?(validator_class)
69
- model.validators.grep(validator_class).any? do |validator|
70
- Helper.check_inclusion?(validator.attributes, column.name)
74
+ def validator?(kind, attribute)
75
+ model.validators.any? do |validator|
76
+ validator.kind == kind && Helper.check_inclusion?(validator.attributes, attribute)
71
77
  end
72
78
  end
73
79
 
74
- def belongs_to_association?
75
- model.reflect_on_all_associations.grep(ActiveRecord::Reflection::BelongsToReflection).any? do |r|
76
- Helper.check_inclusion?([r.foreign_key, r.foreign_type], column.name)
80
+ def belongs_to_association
81
+ @belongs_to_association ||= model.reflect_on_all_associations.find do |r|
82
+ r.belongs_to? && Helper.check_inclusion?([r.foreign_key, r.foreign_type], column.name)
77
83
  end
78
84
  end
79
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.1.4'
4
+ VERSION = '1.1.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_consistency
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
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-10-26 00:00:00.000000000 Z
11
+ date: 2021-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord