class Crm::Helpers::Validators::CrmTypeValidator

Public Instance Methods

validate(record) click to toggle source
# File lib/crm/helpers/validators/crm_type_validator.rb, line 9
def validate(record)
  crm_type_definition(record).each_pair do |attribute, definition|
    record.validates_presence_of attribute if definition[:mandatory]

    reader = attribute.to_sym
    next unless record.respond_to?(reader)

    value = record.send(reader)
    next if value.blank?

    attribute_type = definition['attribute_type']
    record.validates_with validator(attribute_type),
                          attributes: [attribute]
  end
end

Private Instance Methods

validator(attribute_type) click to toggle source
# File lib/crm/helpers/validators/crm_type_validator.rb, line 27
def validator(attribute_type)
  validator_name = '::Crm::Helpers::Validators' \
                   "::Crm#{attribute_type.camelcase}Validator"
  validator_name.constantize
end