diff options
author | Brett Walker <brett@digitalmoksha.com> | 2017-09-20 14:24:10 +0300 |
---|---|---|
committer | Brett Walker <brett@digitalmoksha.com> | 2017-09-23 16:26:04 +0300 |
commit | 665c7876faadc41e1c7a77bb423b951cbcb020ce (patch) | |
tree | 6250727aee6c97f50e4f254da3c1bc0717206fee | |
parent | ed99c899a28134e8d9a1a8a8c4677a6ee65bbd2b (diff) |
added email.confirmed scope and fix email comparison
-rw-r--r-- | app/models/email.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 5 | ||||
-rw-r--r-- | spec/models/email_spec.rb | 11 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 2 |
4 files changed, 18 insertions, 4 deletions
diff --git a/app/models/email.rb b/app/models/email.rb index 085663a4fef..041fbbfbe44 100644 --- a/app/models/email.rb +++ b/app/models/email.rb @@ -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? diff --git a/app/models/user.rb b/app/models/user.rb index c75107472d6..3be3b205ea8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/spec/models/email_spec.rb b/spec/models/email_spec.rb index 8ae5ccd89ed..8e8ef411f23 100644 --- a/spec/models/email_spec.rb +++ b/spec/models/email_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c6df8028072..83addbb809e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 |