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
Boxiang Sun
gitlab-ce
Commits
8f0a2b6d
Commit
8f0a2b6d
authored
Jul 04, 2017
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement Ci::NestedUniquenessValidator
parent
6128f286
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
59 additions
and
47 deletions
+59
-47
app/controllers/projects/pipeline_schedules_controller.rb
app/controllers/projects/pipeline_schedules_controller.rb
+2
-1
app/models/ci/pipeline_schedule.rb
app/models/ci/pipeline_schedule.rb
+0
-5
app/services/ci/create_pipeline_schedule_service.rb
app/services/ci/create_pipeline_schedule_service.rb
+11
-2
app/services/ci/update_pipeline_schedule_service.rb
app/services/ci/update_pipeline_schedule_service.rb
+19
-0
app/validators/uniqueness_of_in_memory_validator.rb
app/validators/uniqueness_of_in_memory_validator.rb
+0
-37
lib/ci/nested_uniqueness_validator.rb
lib/ci/nested_uniqueness_validator.rb
+11
-0
spec/controllers/projects/pipeline_schedules_controller_spec.rb
...ontrollers/projects/pipeline_schedules_controller_spec.rb
+16
-2
No files found.
app/controllers/projects/pipeline_schedules_controller.rb
View file @
8f0a2b6d
...
...
@@ -33,7 +33,8 @@ class Projects::PipelineSchedulesController < Projects::ApplicationController
end
def
update
if
schedule
.
update
(
schedule_params
)
if
Ci
::
UpdatePipelineScheduleService
.
new
(
@project
,
current_user
,
schedule_params
).
execute
(
schedule
)
redirect_to
namespace_project_pipeline_schedules_path
(
@project
.
namespace
.
becomes
(
Namespace
),
@project
)
else
render
:edit
...
...
app/models/ci/pipeline_schedule.rb
View file @
8f0a2b6d
...
...
@@ -15,11 +15,6 @@ 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/services/ci/create_pipeline_schedule_service.rb
View file @
8f0a2b6d
module
Ci
class
CreatePipelineScheduleService
<
BaseService
def
execute
project
.
pipeline_schedules
.
create
(
pipeline_schedule_params
)
pipeline_schedule
=
project
.
pipeline_schedules
.
build
(
pipeline_schedule_params
)
if
Ci
::
NestedUniquenessValidator
.
duplicated?
(
pipeline_schedule_params
[
'variables_attributes'
],
'key'
)
pipeline_schedule
.
errors
.
add
(
'variables.key'
,
"keys are duplicated"
)
return
pipeline_schedule
end
pipeline_schedule
.
save
pipeline_schedule
end
private
def
pipeline_schedule_params
params
.
merge
(
owner:
current_user
)
@pipeline_schedule_params
||=
params
.
merge
(
owner:
current_user
)
end
end
end
app/services/ci/update_pipeline_schedule_service.rb
0 → 100644
View file @
8f0a2b6d
module
Ci
class
UpdatePipelineScheduleService
<
BaseService
def
execute
(
pipeline_schedule
)
if
Ci
::
NestedUniquenessValidator
.
duplicated?
(
pipeline_schedule_params
[
'variables_attributes'
],
'key'
)
pipeline_schedule
.
errors
.
add
(
'variables.key'
,
"keys are duplicated"
)
return
false
end
pipeline_schedule
.
update
(
pipeline_schedule_params
)
end
private
def
pipeline_schedule_params
@pipeline_schedule_params
||=
params
.
merge
(
owner:
current_user
)
end
end
end
app/validators/uniqueness_of_in_memory_validator.rb
deleted
100644 → 0
View file @
6128f286
# 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
lib/ci/nested_uniqueness_validator.rb
0 → 100644
View file @
8f0a2b6d
module
Ci
module
NestedUniquenessValidator
class
<<
self
def
duplicated?
(
nested_attributes
,
unique_key
)
return
false
unless
nested_attributes
.
is_a?
(
Array
)
nested_attributes
.
map
{
|
v
|
v
[
unique_key
]
}.
uniq
.
length
!=
nested_attributes
.
length
end
end
end
end
spec/controllers/projects/pipeline_schedules_controller_spec.rb
View file @
8f0a2b6d
...
...
@@ -267,8 +267,7 @@ describe Projects::PipelineSchedulesController do
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
go
expect
(
assigns
(
:schedule
).
errors
[
'variables.key'
]).
not_to
be_empty
end
...
...
@@ -340,6 +339,21 @@ describe Projects::PipelineSchedulesController do
expect
{
go
}.
to
change
{
Ci
::
PipelineScheduleVariable
.
count
}.
by
(
-
1
)
end
end
context
'when deletes and creates the same keys'
do
let
(
:schedule
)
do
basic_param
.
merge
({
variables_attributes:
[{
id:
pipeline_schedule_variable
.
id
,
key:
pipeline_schedule_variable
.
key
,
_destroy:
true
},
{
key:
pipeline_schedule_variable
.
key
,
value:
'new_value'
}]
})
end
it
'returns an error that variables are duplciated'
do
go
expect
(
assigns
(
:schedule
).
errors
[
'variables.key'
]).
not_to
be_empty
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