class ModelValidator::Validator

Validation engine, which fetch, and validate each database records

Public Class Methods

new(handlers: [], skip_models: []) click to toggle source
# File lib/model_validator/validator.rb, line 20
def initialize(handlers: [], skip_models: [])
  @handlers = handlers
  @skip_models = skip_models
end

Public Instance Methods

classes_to_validate() click to toggle source
# File lib/model_validator/validator.rb, line 25
def classes_to_validate
  raise ApplicationRecordNotFound unless defined?(ApplicationRecord)

  Rails.application.try(:eager_load!) if Rails.env.development?
  ApplicationRecord.descendants
                   .reject(&:abstract_class)
                   .select { |type| type.subclasses.empty? }
                   .reject { |type| @skip_models.include? type }
end
run() click to toggle source
# File lib/model_validator/validator.rb, line 35
def run
  classes_to_validate.each do |model_class|
    @handlers.each { |h| h.try(:on_new_class, model_class) }
    model_class.unscoped.find_each do |model|
      @handlers.each { |h| h.try(:on_violation, model) } unless model.valid?
      @handlers.each { |h| h.try(:after_validation, model) }
    end
  end
end