Class: DatabaseConsistency::Comparators::PresenceComparator

Inherits:
BaseComparator
  • Object
show all
Defined in:
lib/database_consistency/comparators/presence_comparator.rb

Overview

The comparator class for {ActiveModel{ActiveModel::Validations{ActiveModel::Validations::PresenceValidator}

Constant Summary collapse

WEAK_OPTIONS =
%i[allow_nil allow_blank if unless].freeze
CONSTRAINT_MISSING =

Message templates

'should be required in the database'.freeze
POSSIBLE_NULL =
'is required but possible null value insert'.freeze

Instance Attribute Summary

Attributes inherited from BaseComparator

#column, #model, #validator

Instance Method Summary collapse

Methods inherited from BaseComparator

compare, #initialize, #report

Constructor Details

This class inherits a constructor from DatabaseConsistency::Comparators::BaseComparator

Instance Method Details

#compareObject

Table of possible statuses | allow_nil/allow_blank/if/unless | database | status | | ——————————- | ——– | —— | | at least one provided | required | fail | | at least one provided | optional | ok | | all missed | required | ok | | all missed | optional | fail |



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/database_consistency/comparators/presence_comparator.rb', line 17

def compare
  can_be_null = column.null
  has_weak_option = validator.options.slice(*WEAK_OPTIONS).any?

  if can_be_null == has_weak_option
    result(:ok, message)
  elsif can_be_null
    result(:fail, message(CONSTRAINT_MISSING))
  else
    result(:fail, message(POSSIBLE_NULL))
  end
end