Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
c99f8e88
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,发现更多精彩内容 >>
提交
c99f8e88
编写于
7月 03, 2017
作者:
S
Shinya Maeda
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement uniqueness_of_in_memory_validator
上级
cd6aa922
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
72 addition
and
37 deletion
+72
-37
app/models/ci/pipeline_schedule.rb
app/models/ci/pipeline_schedule.rb
+5
-0
app/validators/uniqueness_of_in_memory_validator.rb
app/validators/uniqueness_of_in_memory_validator.rb
+37
-0
spec/controllers/projects/pipeline_schedules_controller_spec.rb
...ontrollers/projects/pipeline_schedules_controller_spec.rb
+30
-37
未找到文件。
app/models/ci/pipeline_schedule.rb
浏览文件 @
c99f8e88
...
...
@@ -15,6 +15,11 @@ module Ci
validates
:cron_timezone
,
cron_timezone:
true
,
presence:
{
unless: :importing?
}
validates
:ref
,
presence:
{
unless: :importing?
}
validates
:description
,
presence:
true
validates
:variables
,
uniqueness_of_in_memory:
{
:collection
=>
:variables
,
:attrs
=>
[
:pipeline_schedule_id
,
:key
],
:message
=>
[
'variables.key'
,
'keys are duplicated'
]
}
before_save
:set_next_run_at
...
...
app/validators/uniqueness_of_in_memory_validator.rb
0 → 100644
浏览文件 @
c99f8e88
# UniquenessOfInMemoryValidator
#
# This validtor is designed for especially the following condition
# - Use `accepts_nested_attributes_for :xxx` in a parent model
# - Use `validates :xxx, uniqueness: { scope: :xxx_id }` in a child model
#
# Inspired by https://stackoverflow.com/a/2883129/2522666
module
ActiveRecord
class
Base
# Validate that the the objects in +collection+ are unique
# when compared against all their non-blank +attrs+. If not
# add +message+ to the base errors.
def
validate_uniqueness_of_in_memory
(
collection
,
attrs
,
message
)
hashes
=
collection
.
inject
({})
do
|
hash
,
record
|
key
=
attrs
.
map
{
|
a
|
record
.
send
(
a
).
to_s
}.
join
if
key
.
blank?
||
record
.
marked_for_destruction?
key
=
record
.
object_id
end
hash
[
key
]
=
record
unless
hash
[
key
]
hash
end
if
collection
.
length
>
hashes
.
length
self
.
errors
.
add
(
*
message
)
end
end
end
end
class
UniquenessOfInMemoryValidator
<
ActiveModel
::
Validator
def
validate
(
record
)
record
.
validate_uniqueness_of_in_memory
(
record
.
public_send
(
options
[
:collection
]),
options
[
:attrs
],
options
[
:message
])
end
end
spec/controllers/projects/pipeline_schedules_controller_spec.rb
浏览文件 @
c99f8e88
...
...
@@ -150,22 +150,21 @@ describe Projects::PipelineSchedulesController do
end
end
# This test no longer passes, since we removed a custom validation
# context 'when variables_attributes has two variables and duplicted' do
# let(:schedule) do
# basic_param.merge({
# variables_attributes: [{ key: 'AAA', value: 'AAA123' }, { key: 'AAA', value: 'BBB123' }]
# })
# end
# it 'returns an error that the keys of variable are duplicated' do
# expect { go }
# .to change { Ci::PipelineSchedule.count }.by(0)
# .and change { Ci::PipelineScheduleVariable.count }.by(0)
# expect(assigns(:schedule).errors['variables.key']).not_to be_empty
# end
# end
context
'when variables_attributes has two variables and duplicted'
do
let
(
:schedule
)
do
basic_param
.
merge
({
variables_attributes:
[{
key:
'AAA'
,
value:
'AAA123'
},
{
key:
'AAA'
,
value:
'BBB123'
}]
})
end
it
'returns an error that the keys of variable are duplicated'
do
expect
{
go
}
.
to
change
{
Ci
::
PipelineSchedule
.
count
}.
by
(
0
)
.
and
change
{
Ci
::
PipelineScheduleVariable
.
count
}.
by
(
0
)
expect
(
assigns
(
:schedule
).
errors
[
'variables.key'
]).
not_to
be_empty
end
end
end
describe
'security'
do
...
...
@@ -194,7 +193,6 @@ describe Projects::PipelineSchedulesController do
before
do
project
.
add_developer
(
user
)
sign_in
(
user
)
end
...
...
@@ -210,7 +208,6 @@ describe Projects::PipelineSchedulesController do
go
pipeline_schedule
.
reload
expect
(
response
).
to
have_http_status
(
:found
)
expect
(
pipeline_schedule
.
description
).
to
eq
(
'updated_desc'
)
expect
(
pipeline_schedule
.
cron
).
to
eq
(
'0 1 * * *'
)
...
...
@@ -232,7 +229,6 @@ describe Projects::PipelineSchedulesController do
expect
{
go
}.
to
change
{
Ci
::
PipelineScheduleVariable
.
count
}.
by
(
1
)
pipeline_schedule
.
reload
expect
(
response
).
to
have_http_status
(
:found
)
expect
(
pipeline_schedule
.
variables
.
last
.
key
).
to
eq
(
'AAA'
)
expect
(
pipeline_schedule
.
variables
.
last
.
value
).
to
eq
(
'AAA123'
)
...
...
@@ -250,7 +246,6 @@ describe Projects::PipelineSchedulesController do
expect
{
go
}.
to
change
{
Ci
::
PipelineScheduleVariable
.
count
}.
by
(
2
)
pipeline_schedule
.
reload
expect
(
response
).
to
have_http_status
(
:found
)
expect
(
pipeline_schedule
.
variables
.
first
.
key
).
to
eq
(
'AAA'
)
expect
(
pipeline_schedule
.
variables
.
first
.
value
).
to
eq
(
'AAA123'
)
...
...
@@ -259,21 +254,20 @@ describe Projects::PipelineSchedulesController do
end
end
# This test no longer passes, since we removed a custom validation
# context 'when params include two duplicated variables' do
# let(:schedule) do
# basic_param.merge({
# variables_attributes: [{ key: 'AAA', value: 'AAA123' }, { key: 'AAA', value: 'BBB123' }]
# })
# end
# it 'returns an error that variables are duplciated' do
# put :update, namespace_id: project.namespace.to_param,
# project_id: project, id: pipeline_schedule, schedule: schedule
# expect(assigns(:schedule).errors['variables.key']).not_to be_empty
# end
# end
context
'when params include two duplicated variables'
do
let
(
:schedule
)
do
basic_param
.
merge
({
variables_attributes:
[{
key:
'AAA'
,
value:
'AAA123'
},
{
key:
'AAA'
,
value:
'BBB123'
}]
})
end
it
'returns an error that variables are duplciated'
do
put
:update
,
namespace_id:
project
.
namespace
.
to_param
,
project_id:
project
,
id:
pipeline_schedule
,
schedule:
schedule
expect
(
assigns
(
:schedule
).
errors
[
'variables.key'
]).
not_to
be_empty
end
end
end
context
'when a pipeline schedule has one variable'
do
...
...
@@ -293,7 +287,6 @@ describe Projects::PipelineSchedulesController do
go
pipeline_schedule
.
reload
expect
(
response
).
to
have_http_status
(
:found
)
expect
(
pipeline_schedule
.
description
).
to
eq
(
'updated_desc'
)
expect
(
pipeline_schedule
.
cron
).
to
eq
(
'0 1 * * *'
)
...
...
@@ -316,6 +309,7 @@ describe Projects::PipelineSchedulesController do
it
'adds the new variable'
do
expect
{
go
}.
to
change
{
Ci
::
PipelineScheduleVariable
.
count
}.
by
(
1
)
pipeline_schedule
.
reload
expect
(
pipeline_schedule
.
variables
.
last
.
key
).
to
eq
(
'AAA'
)
end
end
...
...
@@ -331,7 +325,6 @@ describe Projects::PipelineSchedulesController do
expect
{
go
}.
not_to
change
{
Ci
::
PipelineScheduleVariable
.
count
}
pipeline_schedule_variable
.
reload
expect
(
pipeline_schedule_variable
.
value
).
to
eq
(
'new_value'
)
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录