added email.confirmed scope and fix email comparison
This commit is contained in:
parent
ed99c899a2
commit
665c7876fa
4 changed files with 18 additions and 4 deletions
|
@ -7,6 +7,8 @@ class Email < ActiveRecord::Base
|
|||
validates :email, presence: true, uniqueness: true, email: true
|
||||
validate :unique_email, if: ->(email) { email.email_changed? }
|
||||
|
||||
scope :confirmed, -> { where.not(confirmed_at: nil) }
|
||||
|
||||
after_commit :update_invalid_gpg_signatures, if: -> { previous_changes.key?('confirmed_at') }
|
||||
|
||||
devise :confirmable
|
||||
|
@ -19,7 +21,7 @@ class Email < ActiveRecord::Base
|
|||
def unique_email
|
||||
self.errors.add(:email, 'has already been taken') if User.exists?(email: self.email)
|
||||
end
|
||||
|
||||
|
||||
# once email is confirmed, update the gpg signatures
|
||||
def update_invalid_gpg_signatures
|
||||
user.update_invalid_gpg_signatures if confirmed?
|
||||
|
|
|
@ -837,12 +837,13 @@ class User < ActiveRecord::Base
|
|||
def verified_emails
|
||||
verified_emails = []
|
||||
verified_emails << email if primary_email_verified?
|
||||
verified_emails.concat(emails.where.not(confirmed_at: nil).pluck(:email))
|
||||
verified_emails.concat(emails.confirmed.pluck(:email))
|
||||
verified_emails
|
||||
end
|
||||
|
||||
def verified_email?(check_email)
|
||||
(email == check_email && primary_email_verified?) || verified_emails.include?(check_email)
|
||||
downcased = check_email.downcase
|
||||
(email == downcased && primary_email_verified?) || emails.confirmed.where(email: downcased).exists?
|
||||
end
|
||||
|
||||
def hook_attrs
|
||||
|
|
|
@ -26,4 +26,15 @@ describe Email do
|
|||
email.confirm
|
||||
end
|
||||
end
|
||||
|
||||
describe 'scopes' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
it 'scopes confirmed emails' do
|
||||
create(:email, :confirmed, user: user)
|
||||
expect(user.emails.count).to eq 1
|
||||
expect(user.emails.unconfirmed.count).to eq 0
|
||||
expect(user.emails.confirmed.count).to eq 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1173,7 +1173,7 @@ describe User do
|
|||
user.reload
|
||||
|
||||
expect(user.verified_email?(user.email)).to be_truthy
|
||||
expect(user.verified_email?(email_confirmed.email)).to be_truthy
|
||||
expect(user.verified_email?(email_confirmed.email.titlecase)).to be_truthy
|
||||
end
|
||||
|
||||
it 'returns false when the email is not verified/confirmed' do
|
||||
|
|
Loading…
Reference in a new issue