diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-10-06 16:50:54 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-11-07 22:28:52 +0100 |
commit | 44be82dd18c372862c2421a1934ff816f7cccd97 (patch) | |
tree | 0db110c64a8acd4221e13d48eb25966a9dd8df80 | |
parent | 673b6be1fecedd3a4e7126134f3a764694fcf327 (diff) | |
download | gitlab-ce-44be82dd18c372862c2421a1934ff816f7cccd97.tar.gz |
Refactor User.find_by_any_email
By using SQL::Union we can return a proper ActiveRecord::Relation,
making it possible to select the columns we're interested in (instead of
all of them).
-rw-r--r-- | app/models/user.rb | 18 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 13 |
2 files changed, 22 insertions, 9 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index aa88cda4dc0..8205c3e29b9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -269,16 +269,16 @@ class User < ActiveRecord::Base # Find a User by their primary email or any associated secondary email def find_by_any_email(email) - sql = 'SELECT * - FROM users - WHERE id IN ( - SELECT id FROM users WHERE email = :email - UNION - SELECT emails.user_id FROM emails WHERE email = :email - ) - LIMIT 1;' + by_any_email(email).take + end + + # Returns a relation containing all the users for the given Email address + def by_any_email(email) + users = where(email: email) + emails = joins(:emails).where(emails: { email: email }) + union = Gitlab::SQL::Union.new([users, emails]) - User.find_by_sql([sql, { email: email }]).first + from("(#{union.to_sql}) #{table_name}") end def filter(filter_name) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d2f97009ad9..a3ebf649aa8 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -841,6 +841,19 @@ describe User do end end + describe '.by_any_email' do + it 'returns an ActiveRecord::Relation' do + expect(described_class.by_any_email('foo@example.com')) + .to be_a_kind_of(ActiveRecord::Relation) + end + + it 'returns a relation of users' do + user = create(:user) + + expect(described_class.by_any_email(user.email)).to eq([user]) + end + end + describe '.search' do let!(:user) { create(:user, name: 'user', username: 'usern', email: 'email@gmail.com') } let!(:user2) { create(:user, name: 'user name', username: 'username', email: 'someemail@gmail.com') } |