class DatabaseValidations::Checkers::DbPresenceValidator

Attributes

validator[R]

Public Class Methods

new(validator) click to toggle source

@param [DatabaseValidations::DbPresenceValidator]

# File lib/database_validations/lib/checkers/db_presence_validator.rb, line 12
def initialize(validator)
  @validator = validator
end
validate!(validator) click to toggle source

@param [DatabaseValidations::DbPresenceValidator]

# File lib/database_validations/lib/checkers/db_presence_validator.rb, line 7
def self.validate!(validator)
  new(validator).validate!
end

Public Instance Methods

validate!() click to toggle source
# File lib/database_validations/lib/checkers/db_presence_validator.rb, line 16
def validate!
  validate_foreign_keys! unless ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK']
end

Private Instance Methods

validate_foreign_keys!() click to toggle source
# File lib/database_validations/lib/checkers/db_presence_validator.rb, line 22
def validate_foreign_keys! # rubocop:disable Metrics/AbcSize
  adapter = Adapters::BaseAdapter.new(validator.klass)

  validator.attributes.each do |attribute|
    reflection = validator.klass._reflect_on_association(attribute)

    next unless reflection
    next if adapter.find_foreign_key_by_column(reflection.foreign_key)

    raise Errors::ForeignKeyNotFound.new(reflection.foreign_key, adapter.foreign_keys)
  end
end