BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
7e31a369
Commit
7e31a369
authored
Jun 17, 2015
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
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.
parent
2efb0b6e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
4 deletions
+45
-4
user.rb
app/models/user.rb
+20
-4
user_spec.rb
spec/models/user_spec.rb
+25
-0
No files found.
app/models/user.rb
View file @
7e31a369
...
...
@@ -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
)
...
...
spec/models/user_spec.rb
View file @
7e31a369
...
...
@@ -340,6 +340,31 @@ describe User do
end
end
describe
'.find_for_commit'
do
it
'finds by primary email'
do
user
=
create
(
:user
,
email:
'foo@example.com'
)
expect
(
User
.
find_for_commit
(
user
.
email
,
''
)).
to
eq
user
end
it
'finds by secondary email'
do
email
=
create
(
:email
,
email:
'foo@example.com'
)
user
=
email
.
user
expect
(
User
.
find_for_commit
(
email
.
email
,
''
)).
to
eq
user
end
it
'finds by name'
do
user
=
create
(
:user
,
name:
'Joey JoJo'
)
expect
(
User
.
find_for_commit
(
''
,
'Joey JoJo'
)).
to
eq
user
end
it
'returns nil when nothing found'
do
expect
(
User
.
find_for_commit
(
''
,
''
)).
to
be_nil
end
end
describe
'search'
do
let
(
:user1
)
{
create
(
:user
,
username:
'James'
,
email:
'james@testing.com'
)
}
let
(
:user2
)
{
create
(
:user
,
username:
'jameson'
,
email:
'jameson@example.com'
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment