class DatabaseValidations::DbPresenceValidator

Constants

REFLECTION_MESSAGE

Attributes

klass[R]

Public Class Methods

kind() click to toggle source

Used to make 3rd party libraries work correctly

@return [Symbol]

# File lib/database_validations/lib/validators/db_presence_validator.rb, line 10
def self.kind
  :presence
end
new(options) click to toggle source

@param [Hash] options

Calls superclass method
# File lib/database_validations/lib/validators/db_presence_validator.rb, line 15
def initialize(options)
  @klass = options[:class]

  super

  Injector.inject(klass)
  Checkers::DbPresenceValidator.validate!(self)
end

Public Instance Methods

apply_error(instance, attribute) click to toggle source
# File lib/database_validations/lib/validators/db_presence_validator.rb, line 43
def apply_error(instance, attribute)
  # Helps to avoid querying the database when attribute is association
  instance.send("#{attribute}=", nil)
  instance.errors.add(attribute, :blank, message: REFLECTION_MESSAGE)
end
perform_db_validation?() click to toggle source
# File lib/database_validations/lib/validators/db_presence_validator.rb, line 24
def perform_db_validation?
  true
end
validate(record) click to toggle source

TODO: add support of optional db_belongs_to

Calls superclass method
# File lib/database_validations/lib/validators/db_presence_validator.rb, line 29
def validate(record)
  if record._database_validations_fallback
    super
  else
    attributes.each do |attribute|
      reflection = record.class._reflect_on_association(attribute)

      next if reflection && record.public_send(reflection.foreign_key).present?

      validate_each(record, attribute, record.public_send(attribute))
    end
  end
end