class Idcf::Cli::Validate::Custom::RequireRelationValidator

require relation validator

Constants

MESSAGES

Public Instance Methods

validate(record) click to toggle source
# File lib/idcf/cli/validate/custom/require_relation_validator.rb, line 13
def validate(record)
  return unless process?(record)

  nones = make_nones(record)
  return if nones.empty?

  msg = format(MESSAGES[:message], msg: nones.join('/'))
  record.errors.add(nones.first, msg, {})
end

Protected Instance Methods

make_nones(record) click to toggle source
# File lib/idcf/cli/validate/custom/require_relation_validator.rb, line 37
def make_nones(record)
  result = []
  attributes.each do |attr|
    value = record.read_attribute_for_validation(attr)
    result << attr if value.blank? || value == attr.to_s
  end

  result
end
process?(record) click to toggle source
# File lib/idcf/cli/validate/custom/require_relation_validator.rb, line 25
def process?(record)
  list = []
  op   = options
  attributes.each do |attr|
    value = record.read_attribute_for_validation(attr)
    next if value.blank? && (op[:allow_nil] || op[:allow_blank])
    list << attr
  end

  list.present?
end