Skip to content

Commit

Permalink
Use scope and class method to clean up implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vrajmohan committed Dec 20, 2024
1 parent c2552ea commit e17819d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/models/email_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def mil_email?
email.end_with?('.mil')
end

def self.last_sign_in
order('last_sign_in_at DESC NULLS LAST').first
end


class << self
def find_with_email(email)
return nil if !email.is_a?(String) || email.empty?
Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ class User < ApplicationRecord
attr_accessor :asserted_attributes, :email

def confirmed_email_addresses
email_addresses.where.not(confirmed_at: nil).order('last_sign_in_at DESC NULLS LAST')
email_addresses.confirmed
end

def fully_registered?
!!registration_log&.registered_at
end

def confirmed?
email_addresses.where.not(confirmed_at: nil).any?
confirmed_email_addresses.any?
end

def has_fed_or_mil_email?
Expand Down Expand Up @@ -522,7 +522,7 @@ def reload(...)
end

def last_sign_in_email_address
confirmed_email_addresses.first
email_addresses.confirmed.last_sign_in
end

private
Expand Down
35 changes: 35 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1721,4 +1721,39 @@ def it_should_not_send_survey
end
end
end

describe '#last_sign_in_email_address' do
let(:user) { create(:user) }

let!(:last_sign_in_email_address) do
create(
:email_address,
email: '[email protected]',
user: user,
last_sign_in_at: 1.minute.ago,
)
end

let!(:older_sign_in_email_address) do
create(
:email_address,
email: '[email protected]',
user: user,
last_sign_in_at: 1.hour.ago,
)
end

let!(:never_signed_in_email_address) do
create(
:email_address,
email: '[email protected]',
user: user,
last_sign_in_at: nil,
)
end

it 'returns the last signed in email address' do
expect(user.last_sign_in_email_address).to eq(last_sign_in_email_address)
end
end
end

0 comments on commit e17819d

Please sign in to comment.