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
1acaf75d
Commit
1acaf75d
authored
Jul 06, 2017
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement ayufan validator
parent
0c9795a7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
3 additions
and
44 deletions
+3
-44
app/models/ci/pipeline_schedule.rb
app/models/ci/pipeline_schedule.rb
+1
-5
app/validators/uniqueness_of_in_memory_validator.rb
app/validators/uniqueness_of_in_memory_validator.rb
+0
-37
spec/controllers/projects/pipeline_schedules_controller_spec.rb
...ontrollers/projects/pipeline_schedules_controller_spec.rb
+2
-2
No files found.
app/models/ci/pipeline_schedule.rb
View file @
1acaf75d
...
...
@@ -15,11 +15,7 @@ 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'
]
}
validates
:variables
,
variable_duplicates:
true
before_save
:set_next_run_at
...
...
app/validators/uniqueness_of_in_memory_validator.rb
deleted
100644 → 0
View file @
0c9795a7
# 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
View file @
1acaf75d
...
...
@@ -171,7 +171,7 @@ describe Projects::PipelineSchedulesController do
.
to
change
{
Ci
::
PipelineSchedule
.
count
}.
by
(
0
)
.
and
change
{
Ci
::
PipelineScheduleVariable
.
count
}.
by
(
0
)
expect
(
assigns
(
:schedule
).
errors
[
'variables
.key
'
]).
not_to
be_empty
expect
(
assigns
(
:schedule
).
errors
[
'variables'
]).
not_to
be_empty
end
end
end
...
...
@@ -269,7 +269,7 @@ describe Projects::PipelineSchedulesController do
it
'returns an error that variables are duplciated'
do
go
expect
(
assigns
(
:schedule
).
errors
[
'variables
.key
'
]).
not_to
be_empty
expect
(
assigns
(
:schedule
).
errors
[
'variables'
]).
not_to
be_empty
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