Module: ZohoHub::WithValidations

Included in:
BaseRecord
Defined in:
lib/zoho_hub/with_validations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/zoho_hub/with_validations.rb', line 8

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

Instance Method Details

#add_error(field, message) ⇒ Object



51
52
53
# File 'lib/zoho_hub/with_validations.rb', line 51

def add_error(field, message)
  @errors << { field: field, message: message }
end

#errorsObject



39
40
41
# File 'lib/zoho_hub/with_validations.rb', line 39

def errors
  @errors
end

#validate!Object



31
32
33
34
35
36
37
# File 'lib/zoho_hub/with_validations.rb', line 31

def validate!
  @errors = []

  self.class.validations.each { |validation| validate_field!(validation) }

  @errors
end

#validate_field!(params = {}) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/zoho_hub/with_validations.rb', line 43

def validate_field!(params = {})
  options = params.dup
  validate = options.delete(:validate)

  validator = Module.const_get("Validations::Validate#{validate.downcase.capitalize}")
  validator.new(self, options[:field]).validate(options)
end