summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-06-17 16:07:09 -0400
committerRobert Speicher <rspeicher@gmail.com>2015-06-17 16:37:11 -0400
commit7e31a369f55764a6bccab340b8a3827710623793 (patch)
tree308c155f8f76d54a2bc565278b117dea60570bb8 /app/models
parent2efb0b6e935a9da206134a61d344b4d0334392a3 (diff)
downloadgitlab-ce-7e31a369f55764a6bccab340b8a3827710623793.tar.gz
Spec and refactor User.find_for_commit
Now it executes a single query instead of a possible three at the cost of some scary-looking ARel calls.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/user.rb24
1 files changed, 20 insertions, 4 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 982c05212ce..4ef8259049d 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -220,10 +220,26 @@ class User < ActiveRecord::Base
end
def find_for_commit(email, name)
- # Prefer email match over name match
- User.where(email: email).first ||
- User.joins(:emails).where(emails: { email: email }).first ||
- User.where(name: name).first
+ user_table = arel_table
+ email_table = Email.arel_table
+
+ # Use ARel to build a query:
+ query = user_table.
+ # SELECT "users".* FROM "users"
+ project(user_table[Arel.star]).
+ # LEFT OUTER JOIN "emails"
+ join(email_table, Arel::Nodes::OuterJoin).
+ # ON "users"."id" = "emails"."user_id"
+ on(user_table[:id].eq(email_table[:user_id])).
+ # WHERE ("user"."email" = '<email>' OR "user"."name" = '<name>')
+ # OR "emails"."email" = '<email>'
+ where(
+ user_table[:email].eq(email).
+ or(user_table[:name].eq(name)).
+ or(email_table[:email].eq(email))
+ )
+
+ find_by_sql(query.to_sql).first
end
def filter(filter_name)