refactors management/validations/email validations
This commit is contained in:
@@ -8,6 +8,7 @@ class Verification::Management::Email
|
|||||||
validates :document_type, :document_number, :email, presence: true
|
validates :document_type, :document_number, :email, presence: true
|
||||||
validates :email, format: { with: Devise.email_regexp }, allow_blank: true
|
validates :email, format: { with: Devise.email_regexp }, allow_blank: true
|
||||||
validate :validate_user
|
validate :validate_user
|
||||||
|
validate :validate_document_number
|
||||||
|
|
||||||
delegate :username, to: :user, allow_nil: true
|
delegate :username, to: :user, allow_nil: true
|
||||||
|
|
||||||
@@ -32,25 +33,29 @@ class Verification::Management::Email
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def already_verified?
|
private
|
||||||
user? && user.level_three_verified?
|
|
||||||
end
|
|
||||||
|
|
||||||
def document_number_mismatch?
|
def validate_user
|
||||||
user? && user.document_number.present? &&
|
return if errors.count > 0
|
||||||
(user.document_number != document_number || user.document_type != document_type)
|
if !user?
|
||||||
end
|
errors.add(:email, I18n.t('errors.messages.user_not_found'))
|
||||||
|
elsif user.level_three_verified?
|
||||||
def validate_user
|
errors.add(:email, I18n.t('management.email_verifications.already_verified'))
|
||||||
return if errors.count > 0
|
end
|
||||||
errors.add(:email, I18n.t('errors.messages.user_not_found')) unless user?
|
end
|
||||||
if already_verified?
|
|
||||||
errors.add(:email, I18n.t('management.email_verifications.already_verified'))
|
def validate_document_number
|
||||||
elsif document_number_mismatch?
|
return if errors.count > 0
|
||||||
errors.add(:email,
|
if document_number_mismatch?
|
||||||
I18n.t('management.email_verifications.document_mismatch',
|
errors.add(:email,
|
||||||
document_type: ApplicationController.helpers.humanize_document_type(user.document_type),
|
I18n.t('management.email_verifications.document_mismatch',
|
||||||
document_number: user.document_number))
|
document_type: ApplicationController.helpers.humanize_document_type(user.document_type),
|
||||||
|
document_number: user.document_number))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def document_number_mismatch?
|
||||||
|
user? && user.document_number.present? &&
|
||||||
|
(user.document_number != document_number || user.document_type != document_type)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user