module BazaModels::Model::Validations

Public Class Methods

included(base) click to toggle source
# File lib/baza_models/model/validations.rb, line 2
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

valid?() click to toggle source
# File lib/baza_models/model/validations.rb, line 6
def valid?
  fire_callbacks(:before_validation)

  if new_record?
    fire_callbacks(:before_validation_on_create)
  else
    fire_callbacks(:before_validation_on_update)
  end

  reset_errors

  validators = self.class.__validators

  merged_data = @data.merge(@changes)
  merged_data.each do |attribute_name, attribute_value|
    next unless validators.key?(attribute_name)

    validators[attribute_name].each do |validator|
      next unless validator.fire?(self)

      validator.validate(self, attribute_value)
    end
  end

  execute_custom_validations
  fire_callbacks(:after_validation)

  if new_record?
    fire_callbacks(:after_validation_on_create)
  else
    fire_callbacks(:after_validation_on_update)
  end

  @errors.empty?
end