Module: BazaModels::Model::Validations

Included in:
BazaModels::Model
Defined in:
lib/baza_models/model/validations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
# File 'lib/baza_models/model/validations.rb', line 2

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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