database_consistency 0.6.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1528eadd95e4bdb9511b145d5d8584e4ebde5f9bf30b63de852e5d24fa5a79c5
4
- data.tar.gz: 4b36f7adca734d72d611c12a846f800a507e5b56c4a32c524713b27a646f79e2
3
+ metadata.gz: cf718fbfde9b47e8d76e4e77ff2fc8a70ace19a9761eed13c2576b421fab406b
4
+ data.tar.gz: e212220bdb69019c813686230787ab4cda15a455e997ce37b6b9288067ce36f1
5
5
  SHA512:
6
- metadata.gz: 7a88219644a5da2c07797b6a9b7e2378c4285498576dcd2b23dd877a8404bbb7be81f08c3aa4acd88d259e9d268748a8026ec5047b4f80ac26b0e240e0c62144
7
- data.tar.gz: 15a9f2a8851db00e35b065f5143acb85321839f4a1965800b95d01dc44b741737764bcd261c58f637ad17b65707c298c8f6a4b6abe6874539f726a4ce13ebee3
6
+ metadata.gz: bd5ae1304d58ab4b8ffffbc2408fde5721f6e1026743c49c08ef93566fa5524293d9fb9f181a2a8129dfff7a1883477dcda5fc0264b411cd9d91990d590ae544
7
+ data.tar.gz: 2f5af1682db86a9df202227d8c9ff760930c3ef245d1a1f5a9a4d138f30412d32ccee5589c5ce5d9ba1a1a19a79a6944bbae2ed50679297c055026a0ed14faf5
@@ -4,6 +4,18 @@ module DatabaseConsistency
4
4
  module Checkers
5
5
  # The base class for checkers
6
6
  class BaseChecker
7
+ # @param [DatabaseConsistency::Configuration]
8
+ #
9
+ # @return [Boolean]
10
+ def self.enabled?(configuration)
11
+ configuration.enabled?('DatabaseConsistencyCheckers', checker_name)
12
+ end
13
+
14
+ # @return [String]
15
+ def self.checker_name
16
+ @checker_name ||= name.split('::').last
17
+ end
18
+
7
19
  # @return [Hash, nil]
8
20
  def report
9
21
  return unless preconditions
@@ -11,7 +23,14 @@ module DatabaseConsistency
11
23
  @report ||= check
12
24
  end
13
25
 
26
+ # @return [Hash, nil]
27
+ def report_if_enabled?(configuration)
28
+ report if enabled?(configuration)
29
+ end
30
+
14
31
  # @param [DatabaseConsistency::Configuration] configuration
32
+ #
33
+ # @return [Boolean]
15
34
  def enabled?(configuration)
16
35
  configuration.enabled?(table_or_model_name, column_or_attribute_name, checker_name)
17
36
  end
@@ -30,7 +49,7 @@ module DatabaseConsistency
30
49
 
31
50
  # @return [String]
32
51
  def checker_name
33
- @checker_name ||= self.class.name.split('::').last
52
+ @checker_name ||= self.class.checker_name
34
53
  end
35
54
 
36
55
  def table_or_model_name
@@ -11,13 +11,12 @@ module DatabaseConsistency
11
11
 
12
12
  # We skip check when:
13
13
  # - association isn't a `HasOne` or `HasMany`
14
+ # - association has `through` option
14
15
  def preconditions
15
- [
16
- ActiveRecord::Associations::HasOneAssociation,
17
- ActiveRecord::Associations::HasOneThroughAssociation,
18
- ActiveRecord::Associations::HasManyAssociation,
19
- ActiveRecord::Associations::HasManyThroughAssociation
20
- ].include?(association.association_class)
16
+ %i[
17
+ has_one
18
+ has_many
19
+ ].include?(association.macro) && association.through_reflection.nil?
21
20
  end
22
21
 
23
22
  # Table of possible statuses
@@ -9,7 +9,8 @@ module DatabaseConsistency
9
9
 
10
10
  def initialize(filepath = CONFIGURATION_PATH)
11
11
  @configuration = if filepath && File.exist?(filepath)
12
- YAML.load_file(filepath)
12
+ data = YAML.load_file(filepath)
13
+ data.is_a?(Hash) ? data : {}
13
14
  else
14
15
  {}
15
16
  end
@@ -13,9 +13,9 @@ module DatabaseConsistency
13
13
  def check
14
14
  Helper.models.flat_map do |model|
15
15
  model.reflect_on_all_associations.flat_map do |association|
16
- CHECKERS.map do |checker_class|
16
+ enabled_checkers.map do |checker_class|
17
17
  checker = checker_class.new(model, association)
18
- checker.report if checker.enabled?(configuration)
18
+ checker.report_if_enabled?(configuration)
19
19
  end
20
20
  end
21
21
  end.compact
@@ -27,6 +27,11 @@ module DatabaseConsistency
27
27
  @reports ||= check
28
28
  end
29
29
 
30
+ # @return [Array<Class>]
31
+ def enabled_checkers
32
+ self.class::CHECKERS.select { |checker| checker.enabled?(configuration) }
33
+ end
34
+
30
35
  private
31
36
 
32
37
  def check
@@ -13,9 +13,9 @@ module DatabaseConsistency
13
13
  def check
14
14
  Helper.parent_models.flat_map do |model|
15
15
  model.columns.flat_map do |column|
16
- CHECKERS.map do |checker_class|
16
+ enabled_checkers.map do |checker_class|
17
17
  checker = checker_class.new(model, column)
18
- checker.report if checker.enabled?(configuration)
18
+ checker.report_if_enabled?(configuration)
19
19
  end
20
20
  end
21
21
  end.compact
@@ -17,9 +17,9 @@ module DatabaseConsistency
17
17
  Helper.parent_models.flat_map do |model|
18
18
  model.validators.flat_map do |validator|
19
19
  validator.attributes.flat_map do |attribute|
20
- CHECKERS.map do |checker_class|
20
+ enabled_checkers.map do |checker_class|
21
21
  checker = checker_class.new(model, attribute, validator)
22
- checker.report if checker.enabled?(configuration)
22
+ checker.report_if_enabled?(configuration)
23
23
  end
24
24
  end
25
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '0.6.0'
4
+ VERSION = '0.6.1'
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: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-19 00:00:00.000000000 Z
11
+ date: 2019-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord