diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models/application_setting.rb | 2 | ||||
-rw-r--r-- | app/models/email.rb | 2 | ||||
-rw-r--r-- | app/models/member.rb | 2 | ||||
-rw-r--r-- | app/models/user.rb | 6 | ||||
-rw-r--r-- | app/validators/devise_email_validator.rb | 36 | ||||
-rw-r--r-- | app/validators/email_validator.rb | 7 |
6 files changed, 42 insertions, 13 deletions
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb index c5035797621..cd36c963ee5 100644 --- a/app/models/application_setting.rb +++ b/app/models/application_setting.rb @@ -69,7 +69,7 @@ class ApplicationSetting < ActiveRecord::Base url: true validates :admin_notification_email, - email: true, + devise_email: true, allow_blank: true validates :two_factor_grace_period, diff --git a/app/models/email.rb b/app/models/email.rb index 3ce6e792fa8..7c33c5c7e64 100644 --- a/app/models/email.rb +++ b/app/models/email.rb @@ -7,7 +7,7 @@ class Email < ActiveRecord::Base belongs_to :user validates :user_id, presence: true - validates :email, presence: true, uniqueness: true, email: true + validates :email, presence: true, uniqueness: true, devise_email: true validate :unique_email, if: ->(email) { email.email_changed? } scope :confirmed, -> { where.not(confirmed_at: nil) } diff --git a/app/models/member.rb b/app/models/member.rb index 8e071a8ff21..5dbc0c2eec9 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -28,7 +28,7 @@ class Member < ActiveRecord::Base presence: { if: :invite? }, - email: { + devise_email: { allow_nil: true }, uniqueness: { diff --git a/app/models/user.rb b/app/models/user.rb index 778c9e631bd..0ebfb9a0ccb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -162,9 +162,9 @@ class User < ApplicationRecord validates :name, presence: true validates :email, confirmation: true validates :notification_email, presence: true - validates :notification_email, email: true, if: ->(user) { user.notification_email != user.email } - validates :public_email, presence: true, uniqueness: true, email: true, allow_blank: true - validates :commit_email, email: true, allow_nil: true, if: ->(user) { user.commit_email != user.email } + validates :notification_email, devise_email: true, if: ->(user) { user.notification_email != user.email } + validates :public_email, presence: true, uniqueness: true, devise_email: true, allow_blank: true + validates :commit_email, devise_email: true, allow_nil: true, if: ->(user) { user.commit_email != user.email } validates :bio, length: { maximum: 255 }, allow_blank: true validates :projects_limit, presence: true, diff --git a/app/validators/devise_email_validator.rb b/app/validators/devise_email_validator.rb new file mode 100644 index 00000000000..6ca921ca7fa --- /dev/null +++ b/app/validators/devise_email_validator.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# DeviseEmailValidator +# +# Custom validator for email formats. It asserts that there are no +# @ symbols or whitespaces in either the localpart or the domain, and that +# there is a single @ symbol separating the localpart and the domain. +# +# The available options are: +# - regexp: Email regular expression used to validate email formats as instance of Regexp class. +# If provided value has different type then a new Rexexp class instance is created using the value. +# Default: +Devise.email_regexp+ +# +# Example: +# class User < ActiveRecord::Base +# validates :personal_email, devise_email: true +# +# validates :public_email, devise_email: { regexp: Devise.email_regexp } +# end +class DeviseEmailValidator < ActiveModel::EachValidator + DEFAULT_OPTIONS = { + regexp: Devise.email_regexp + }.freeze + + def initialize(options) + options.reverse_merge!(DEFAULT_OPTIONS) + + raise ArgumentError, "Expected 'regexp' argument of type class Regexp" unless options[:regexp].is_a?(Regexp) + + super(options) + end + + def validate_each(record, attribute, value) + record.errors.add(attribute, :invalid) unless value =~ options[:regexp] + end +end diff --git a/app/validators/email_validator.rb b/app/validators/email_validator.rb deleted file mode 100644 index 9459edb7515..00000000000 --- a/app/validators/email_validator.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class EmailValidator < ActiveModel::EachValidator - def validate_each(record, attribute, value) - record.errors.add(attribute, :invalid) unless value =~ Devise.email_regexp - end -end |