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
1
Merge Requests
1
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
nexedi
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
Show 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
...
@@ -5,6 +5,7 @@ class User < ActiveRecord::Base
include
Gitlab
::
ConfigHelper
include
Gitlab
::
ConfigHelper
include
Gitlab
::
CurrentSettings
include
Gitlab
::
CurrentSettings
include
Gitlab
::
SQL
::
Pattern
include
Avatarable
include
Avatarable
include
Referable
include
Referable
include
Sortable
include
Sortable
...
@@ -303,7 +304,7 @@ class User < ActiveRecord::Base
...
@@ -303,7 +304,7 @@ class User < ActiveRecord::Base
# Returns an ActiveRecord::Relation.
# Returns an ActiveRecord::Relation.
def
search
(
query
)
def
search
(
query
)
table
=
arel_table
table
=
arel_table
pattern
=
Gitlab
::
SQL
::
Pattern
.
new
(
query
).
to_sql
pattern
=
User
.
to_pattern
(
query
)
order
=
<<~
SQL
order
=
<<~
SQL
CASE
CASE
...
...
lib/gitlab/sql/pattern.rb
View file @
87b51c59
module
Gitlab
module
Gitlab
module
SQL
module
SQL
class
Pattern
module
Pattern
MIN_CHARS_FOR_PARTIAL_MATCHING
=
3
extend
ActiveSupport
::
Concern
attr_reader
:query
def
initialize
(
query
)
MIN_CHARS_FOR_PARTIAL_MATCHING
=
3
@query
=
query
end
def
to_sql
class_methods
do
if
exact_matching?
def
to_pattern
(
query
)
sanitized_query
if
exact_matching?
(
query
)
sanitize_sql_like
(
query
)
else
else
"%
#{
sanitized_query
}
%"
"%
#{
sanitize_sql_like
(
query
)
}
%"
end
end
end
end
def
exact_matching?
def
exact_matching?
(
query
)
!
partial_matching?
query
.
length
<
MIN_CHARS_FOR_PARTIAL_MATCHING
end
end
def
partial_matching?
def
partial_matching?
(
query
)
@
query
.
length
>=
MIN_CHARS_FOR_PARTIAL_MATCHING
query
.
length
>=
MIN_CHARS_FOR_PARTIAL_MATCHING
end
end
def
sanitized_query
# Note: ActiveRecord::Base.sanitize_sql_like is a protected method
ActiveRecord
::
Base
.
__send__
(
:sanitize_sql_like
,
query
)
end
end
end
end
end
end
...
...
spec/lib/gitlab/sql/pattern_spec.rb
View file @
87b51c59
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
SQL
::
Pattern
do
describe
Gitlab
::
SQL
::
Pattern
do
describe
'#to_
sql
'
do
describe
'#to_
pattern
'
do
subject
(
:to_
sql
)
{
described_class
.
new
(
query
).
to_sql
}
subject
(
:to_
pattern
)
{
User
.
to_pattern
(
query
)
}
context
'when a query is shorter than 3 chars'
do
context
'when a query is shorter than 3 chars'
do
let
(
:query
)
{
'12'
}
let
(
:query
)
{
'12'
}
it
'returns exact matching pattern'
do
it
'returns exact matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'12'
)
expect
(
to_
pattern
).
to
eq
(
'12'
)
end
end
end
end
...
@@ -16,7 +16,7 @@ describe Gitlab::SQL::Pattern do
...
@@ -16,7 +16,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'_2'
}
let
(
:query
)
{
'_2'
}
it
'returns sanitized exact matching pattern'
do
it
'returns sanitized exact matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'\_2'
)
expect
(
to_
pattern
).
to
eq
(
'\_2'
)
end
end
end
end
...
@@ -24,7 +24,7 @@ describe Gitlab::SQL::Pattern do
...
@@ -24,7 +24,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'123'
}
let
(
:query
)
{
'123'
}
it
'returns partial matching pattern'
do
it
'returns partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%123%'
)
expect
(
to_
pattern
).
to
eq
(
'%123%'
)
end
end
end
end
...
@@ -32,7 +32,7 @@ describe Gitlab::SQL::Pattern do
...
@@ -32,7 +32,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'_23'
}
let
(
:query
)
{
'_23'
}
it
'returns partial matching pattern'
do
it
'returns partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%\_23%'
)
expect
(
to_
pattern
).
to
eq
(
'%\_23%'
)
end
end
end
end
...
@@ -40,7 +40,7 @@ describe Gitlab::SQL::Pattern do
...
@@ -40,7 +40,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'1234'
}
let
(
:query
)
{
'1234'
}
it
'returns partial matching pattern'
do
it
'returns partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%1234%'
)
expect
(
to_
pattern
).
to
eq
(
'%1234%'
)
end
end
end
end
...
@@ -48,7 +48,7 @@ describe Gitlab::SQL::Pattern do
...
@@ -48,7 +48,7 @@ describe Gitlab::SQL::Pattern do
let
(
:query
)
{
'_234'
}
let
(
:query
)
{
'_234'
}
it
'returns sanitized partial matching pattern'
do
it
'returns sanitized partial matching pattern'
do
expect
(
to_
sql
).
to
eq
(
'%\_234%'
)
expect
(
to_
pattern
).
to
eq
(
'%\_234%'
)
end
end
end
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