Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
87b51c59
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
87b51c59
编写于
8月 29, 2017
作者:
H
Hiroyuki Sato
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move the logic to a concern
上级
866aab7f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
26 addition
and
32 deletion
+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
未找到文件。
app/models/user.rb
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录