Sha256: 2676a48614589d6679b8638f630d5a5bb72e0f3ca2421121771ea5f784462999

Contents?: true

Size: 991 Bytes

Versions: 2

Compression:

Stored size: 991 Bytes

Contents

# global namespace so you can use the rails 3 "validate :field, email: [options]"
class EmailValidator < ActiveModel::EachValidator
  def validate_each(record,attribute,value)
    return if value.blank? && options[:allow_blank]

    case value
    when EmailAttribute::SingleAddress
      validate_single_address_model(record, attribute, value.address)
    when String
      validate_single_address_model(record, attribute, value)
    when EmailAttribute::List
      value.each do |address|
        validate_single_address_model(record, attribute, address)
      end
    end
  end

  protected

  # model should be a Mail::Address model
  def validate_single_address_model(record, attribute, model)
    begin
      model = EmailAttribute::SingleAddress.new(model).address if model.is_a?(String)
      r = model.domain && model.domain.split(".").length > 1
    rescue Exception => e
      r = false
    end
    record.errors.add(attribute, (options[:message] || :invalid)) unless r
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
email_attribute-0.0.2 lib/email_attribute/email_validator.rb
email_attribute-0.0.1 lib/email_attribute/email_validator.rb