Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
c954a11d1186
Commit
c954a11d
authored
Dec 05, 2018
by
Grzegorz Bizon
Browse files
Add custom validation message for chronic duration attribute
parent
be48d58a9899
Changes
5
Hide whitespace changes
Inline
Side-by-side
app/models/ci/runner.rb
View file @
c954a11d
...
...
@@ -114,7 +114,8 @@
cached_attr_reader
:version
,
:revision
,
:platform
,
:architecture
,
:ip_address
,
:contacted_at
chronic_duration_attr
:maximum_timeout_human_readable
,
:maximum_timeout
chronic_duration_attr
:maximum_timeout_human_readable
,
:maximum_timeout
,
error_message:
'Maximum job timeout has a value which could not be accepted'
validates
:maximum_timeout
,
allow_nil:
true
,
numericality:
{
greater_than_or_equal_to:
600
,
...
...
app/models/concerns/chronic_duration_attribute.rb
View file @
c954a11d
...
...
@@ -24,7 +24,7 @@
end
end
validates
virtual_attribute
,
allow_nil:
true
,
duration:
true
validates
virtual_attribute
,
allow_nil:
true
,
duration:
{
message:
parameters
[
:error_message
]
}
end
alias_method
:chronic_duration_attr
,
:chronic_duration_attr_writer
...
...
app/models/project.rb
View file @
c954a11d
...
...
@@ -384,7 +384,8 @@
enum
auto_cancel_pending_pipelines:
{
disabled:
0
,
enabled:
1
}
chronic_duration_attr
:build_timeout_human_readable
,
:build_timeout
,
default:
3600
chronic_duration_attr
:build_timeout_human_readable
,
:build_timeout
,
default:
3600
,
error_message:
'Maximum job timeout has a value which could not be accepted'
validates
:build_timeout
,
allow_nil:
true
,
numericality:
{
greater_than_or_equal_to:
10
.
minutes
,
...
...
app/validators/duration_validator.rb
View file @
c954a11d
...
...
@@ -14,6 +14,10 @@
def
validate_each
(
record
,
attribute
,
value
)
ChronicDuration
.
parse
(
value
)
rescue
ChronicDuration
::
DurationParseError
record
.
errors
.
add
(
attribute
,
"is not a correct duration"
)
if
options
[
:message
]
record
.
errors
.
add
(
:base
,
options
[
:message
])
else
record
.
errors
.
add
(
attribute
,
"is not a correct duration"
)
end
end
end
spec/models/concerns/chronic_duration_attribute_spec.rb
View file @
c954a11d
...
...
@@ -54,7 +54,8 @@
subject
.
send
(
"
#{
virtual_field
}
="
,
'-10m'
)
expect
(
subject
.
valid?
).
to
be_falsey
expect
(
subject
.
errors
&
.
messages
).
to
include
(
virtual_field
=>
[
'is not a correct duration'
])
expect
(
subject
.
errors
&
.
messages
)
.
to
include
(
base:
[
'Maximum job timeout has a value which could not be accepted'
])
end
end
...
...
Write
Preview
Supports
Markdown
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