class EmailValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value, options) record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.email')) end end private def valid_format?(value) value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i end def valid_domain?(value, options) value_downcased = value.downcase options.empty? || options.any? { |domain| value_downcased.end_with?(".#{domain.downcase}") } end def valid?(email, options) valid_format?(email) && valid_domain?(email, [*(options[:domain])]) end end