Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
87b51c59
Commit
87b51c59
authored
Aug 29, 2017
by
Hiroyuki Sato
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move the logic to a concern
parent
866aab7f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
32 deletions
+26
-32
app/models/user.rb
app/models/user.rb
+2
-1
lib/gitlab/sql/pattern.rb
lib/gitlab/sql/pattern.rb
+16
-23
spec/lib/gitlab/sql/pattern_spec.rb
spec/lib/gitlab/sql/pattern_spec.rb
+8
-8
No files found.
app/models/user.rb
View file @
87b51c59
...
...
@@ -5,6 +5,7 @@ class User < ActiveRecord::Base
include
Gitlab
::
ConfigHelper
include
Gitlab
::
CurrentSettings
include
Gitlab
::
SQL
::
Pattern
include
Avatarable
include
Referable
include
Sortable
...
...
@@ -303,7 +304,7 @@ class User < ActiveRecord::Base
# Returns an ActiveRecord::Relation.
def
search
(
query
)
table
=
arel_table
pattern
=
Gitlab
::
SQL
::
Pattern
.
new
(
query
).
to_sql
pattern
=
User
.
to_pattern
(
query
)
order
=
<<~
SQL
CASE
...
...
lib/gitlab/sql/pattern.rb
View file @
87b51c59
module
Gitlab
module
SQL
class
Pattern
MIN_CHARS_FOR_PARTIAL_MATCHING
=
3
attr_reader
:query
module
Pattern
extend
ActiveSupport
::
Concern
def
initialize
(
query
)
@query
=
query
end
MIN_CHARS_FOR_PARTIAL_MATCHING
=
3
def
to_sql
if
exact_matching?
sanitized_query
else
"%
#{
sanitized_query
}
%"
class_methods
do
def
to_pattern
(
query
)
if
exact_matching?
(
query
)
sanitize_sql_like
(
query
)
else
"%
#{
sanitize_sql_like
(
query
)
}
%"
end
end
end
def
exact_matching?
!
partial_matching?
end
def
partial_matching?
@query
.
length
>=
MIN_CHARS_FOR_PARTIAL_MATCHING
end
def
exact_matching?
(
query
)
query
.
length
<
MIN_CHARS_FOR_PARTIAL_MATCHING
end
def
sanitized_query
# Note: ActiveRecord::Base.sanitize_sql_like is a protected method
ActiveRecord
::
Base
.
__send__
(
:sanitize_sql_like
,
query
)
def
partial_matching?
(
query
)
query
.
length
>=
MIN_CHARS_FOR_PARTIAL_MATCHING
end
end
end
end
...
...
spec/lib/gitlab/sql/pattern_spec.rb
View file @
87b51c59
require
'spec_helper'
describe
Gitlab
::
SQL
::
Pattern
do
describe
'#to_
sql
'
do
subject
(
:to_
sql
)
{
described_class
.
new
(
query
).
to_sql
}
describe
'#to_
pattern
'
do
subject
(
:to_
pattern
)
{
User
.
to_pattern
(
query
)
}
context
'when a query is shorter than 3 chars'
do
let
(
:query
)
{
'12'
}
it
'returns exact matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'12'
)
expect
(
to_
pattern
).
to
eq
(
'12'
)
end
end
...
...
@@ -16,7 +16,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'_2'
}
it
'returns sanitized exact matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'\_2'
)
expect
(
to_
pattern
).
to
eq
(
'\_2'
)
end
end
...
...
@@ -24,7 +24,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'123'
}
it
'returns partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%123%'
)
expect
(
to_
pattern
).
to
eq
(
'%123%'
)
end
end
...
...
@@ -32,7 +32,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'_23'
}
it
'returns partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%\_23%'
)
expect
(
to_
pattern
).
to
eq
(
'%\_23%'
)
end
end
...
...
@@ -40,7 +40,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'1234'
}
it
'returns partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%1234%'
)
expect
(
to_
pattern
).
to
eq
(
'%1234%'
)
end
end
...
...
@@ -48,7 +48,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'_234'
}
it
'returns sanitized partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%\_234%'
)
expect
(
to_
pattern
).
to
eq
(
'%\_234%'
)
end
end
end
...
...
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