Module: ActiveRecordDoctor

Defined in:
lib/active_record_doctor.rb,
lib/active_record_doctor/help.rb,
lib/active_record_doctor/utils.rb,
lib/active_record_doctor/config.rb,
lib/active_record_doctor/errors.rb,
lib/active_record_doctor/logger.rb,
lib/active_record_doctor/runner.rb,
lib/active_record_doctor/railtie.rb,
lib/active_record_doctor/version.rb,
lib/active_record_doctor/printers.rb,
lib/active_record_doctor/detectors.rb,
lib/active_record_doctor/rake/task.rb,
lib/active_record_doctor/logger/dummy.rb,
lib/active_record_doctor/config/loader.rb,
lib/active_record_doctor/detectors/base.rb,
lib/active_record_doctor/logger/hierarchical.rb,
lib/active_record_doctor/detectors/extraneous_indexes.rb,
lib/active_record_doctor/detectors/missing_foreign_keys.rb,
lib/active_record_doctor/detectors/unindexed_deleted_at.rb,
lib/active_record_doctor/detectors/missing_unique_indexes.rb,
lib/active_record_doctor/detectors/short_primary_key_type.rb,
lib/active_record_doctor/detectors/unindexed_foreign_keys.rb,
lib/active_record_doctor/detectors/table_without_primary_key.rb,
lib/active_record_doctor/detectors/incorrect_dependent_option.rb,
lib/active_record_doctor/detectors/undefined_table_references.rb,
lib/active_record_doctor/detectors/incorrect_length_validation.rb,
lib/active_record_doctor/detectors/mismatched_foreign_key_type.rb,
lib/active_record_doctor/detectors/missing_non_null_constraint.rb,
lib/active_record_doctor/detectors/missing_presence_validation.rb,
lib/generators/active_record_doctor/add_indexes/add_indexes_generator.rb,
lib/active_record_doctor/detectors/incorrect_boolean_presence_validation.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Detectors, Logger, Printers, Rake, Utils Classes: AddIndexesGenerator, Config, Error, Help, Loader, Railtie, Runner

Constant Summary collapse

VERSION =
"1.15.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_configObject (readonly)

The config file that’s currently being processed by .load_config.



6
7
8
# File 'lib/active_record_doctor/config/loader.rb', line 6

def current_config
  @current_config
end

Class Method Details

.configure(&block) ⇒ Object

This method is part of the public API that is intended for use by active_record_doctor users. The remaining methods are considered to be public-not-published.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record_doctor/config/loader.rb', line 11

def configure(&block)
  # If current_config is set it means that .configure was already called
  # so we must raise an error.
  raise ActiveRecordDoctor::Error::ConfigureCalledTwice if current_config

  # Determine the recognized global and detector settings based on detector
  # metadata. recognizedd_detectors maps detector names to setting names.
  # recognized_globals contains global setting names.
  recognized_detectors = {}
  recognized_globals = []

  ActiveRecordDoctor.detectors.each do |name, detector|
    locals, globals = detector.locals_and_globals

    recognized_detectors[name] = locals
    recognized_globals.concat(globals)
  end

  # The same global can be used by multiple detectors so we must remove
  # duplicates to ensure they aren't reported multiple times via the user
  # interface (e.g. in error messages).
  recognized_globals.uniq!

  # Prepare an empty configuration and call the loader. After .new returns
  # @current_config will contain the configuration provided by the block.
  @current_config = Config.new({}, {})
  Loader.new(current_config, recognized_globals, recognized_detectors, &block)

  # This method is part of the public API expected to be called by users.
  # In order to avoid leaking internal objects, we return an explicit nil.
  nil
end

.detectorsObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/active_record_doctor/detectors.rb', line 7

def self.detectors
  @detectors ||=
    begin
      detectors = {}
      ActiveRecordDoctor::Detectors::Base.subclasses.each do |detector|
        detectors[detector.underscored_name] = detector
      end
      detectors
    end
end

.handle_exceptionObject



4
5
6
7
8
9
# File 'lib/active_record_doctor/errors.rb', line 4

def self.handle_exception
  yield
rescue ActiveRecordDoctor::Error => e
  $stderr.puts(e.user_message)
  exit(1)
end

.load_config(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_record_doctor/config/loader.rb', line 44

def load_config(path)
  begin
    load(path)
  rescue ActiveRecordDoctor::Error
    raise
  rescue LoadError
    raise ActiveRecordDoctor::Error::ConfigurationFileMissing
  rescue StandardError => e
    raise ActiveRecordDoctor::Error::ConfigurationError[e]
  end
  raise ActiveRecordDoctor::Error::ConfigureNotCalled if current_config.nil?

  # Store the configuration and reset @current_config. We cannot reset
  # @current_config in .configure because that would prevent us from
  # detecting multiple calls to that method.
  config = @current_config
  @current_config = nil

  config
rescue ActiveRecordDoctor::Error => e
  e.config_path = path
  raise e
end

.load_config_with_defaults(path) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/active_record_doctor/config/loader.rb', line 71

def load_config_with_defaults(path)
  default_config = load_config(DEFAULT_CONFIG_PATH)
  return default_config if path.nil?

  config = load_config(path)
  default_config.merge(config)
end