Commit 665c7876 authored by Brett Walker's avatar Brett Walker

added email.confirmed scope and fix email comparison

parent ed99c899
...@@ -7,6 +7,8 @@ class Email < ActiveRecord::Base ...@@ -7,6 +7,8 @@ class Email < ActiveRecord::Base
validates :email, presence: true, uniqueness: true, email: true validates :email, presence: true, uniqueness: true, email: true
validate :unique_email, if: ->(email) { email.email_changed? } 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') } after_commit :update_invalid_gpg_signatures, if: -> { previous_changes.key?('confirmed_at') }
devise :confirmable devise :confirmable
......
...@@ -837,12 +837,13 @@ class User < ActiveRecord::Base ...@@ -837,12 +837,13 @@ class User < ActiveRecord::Base
def verified_emails def verified_emails
verified_emails = [] verified_emails = []
verified_emails << email if primary_email_verified? 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 verified_emails
end end
def verified_email?(check_email) 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 end
def hook_attrs def hook_attrs
......
...@@ -26,4 +26,15 @@ describe Email do ...@@ -26,4 +26,15 @@ describe Email do
email.confirm email.confirm
end end
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 end
...@@ -1173,7 +1173,7 @@ describe User do ...@@ -1173,7 +1173,7 @@ describe User do
user.reload user.reload
expect(user.verified_email?(user.email)).to be_truthy 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 end
it 'returns false when the email is not verified/confirmed' do it 'returns false when the email is not verified/confirmed' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment