class TowerdataEmail::Validators::TowerdataEmailValidations

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/towerdata_email/validators.rb, line 36
def validate_each(record, attribute, value)
  return true if handle_nil?(record, attribute, value) || handle_blank?(record, attribute, value)

  if eligible_for_validation?(record, attribute)
    e = TowerdataEmail.validate_email(value)
    if e.incorrect?
      if e.corrections && auto_accept_corrections?
        record.send(:"#{attribute}=",  e.corrections.first)
      else
        record.errors[attribute] << custom_error_message(default_failure_message(e), e)
      end
    end
  end
end

Private Instance Methods

auto_accept_corrections?() click to toggle source
# File lib/towerdata_email/validators.rb, line 69
def auto_accept_corrections?
  # could be false, so can't use ||
  if options.include?(:auto_accept_corrections)
    options[:auto_accept_corrections]
  else
    TowerdataEmail.config.auto_accept_corrections
  end
end
default_failure_message(e) click to toggle source
# File lib/towerdata_email/validators.rb, line 59
def default_failure_message(e)
  rv = "did not pass TowerData validation: #{e.status_desc}"

  if show_corrections? && e.corrections
    rv << ".  Did you mean #{e.corrections.join(" or ")}?"
  end

  rv
end
eligible_for_validation?(record, attribute) click to toggle source
# File lib/towerdata_email/validators.rb, line 53
def eligible_for_validation?(record, attribute)
  !TowerdataEmailEmail.config.only_validate_on_change ||
      !record.respond_to?(:"#{attribute}_changed?") ||
      record.send("#{attribute}_changed?")
end
show_corrections?() click to toggle source
# File lib/towerdata_email/validators.rb, line 78
def show_corrections?
  # could be false, so can't use ||
  if options.include?(:show_corrections)
    options[:show_corrections]
  else
    TowerdataEmail.config.show_corrections
  end
end