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
Léo-Paul Géneau
gitlab-ce
Commits
c747d9bc
Commit
c747d9bc
authored
Mar 21, 2018
by
Tomasz Maczukin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add validation for chronic_duration_attr_writer
parent
ed86344f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
19 deletions
+74
-19
app/models/concerns/chronic_duration_attribute.rb
app/models/concerns/chronic_duration_attribute.rb
+23
-4
spec/models/concerns/chronic_duration_attribute_spec.rb
spec/models/concerns/chronic_duration_attribute_spec.rb
+51
-15
No files found.
app/models/concerns/chronic_duration_attribute.rb
View file @
c747d9bc
...
@@ -18,13 +18,32 @@ module ChronicDurationAttribute
...
@@ -18,13 +18,32 @@ module ChronicDurationAttribute
end
end
def
chronic_duration_attr_writer
(
virtual_attribute
,
source_attribute
)
def
chronic_duration_attr_writer
(
virtual_attribute
,
source_attribute
)
virtual_attribute_validator
=
"
#{
virtual_attribute
}
_validator"
.
to_sym
validation_error
=
"
#{
virtual_attribute
}
_error"
.
to_sym
validate
virtual_attribute_validator
attr_accessor
validation_error
define_method
(
"
#{
virtual_attribute
}
="
)
do
|
value
|
define_method
(
"
#{
virtual_attribute
}
="
)
do
|
value
|
new_value
=
ChronicDuration
.
parse
(
value
).
to_i
unless
value
.
nil?
begin
new_value
=
nil
if
!
new_value
.
nil?
&&
new_value
<=
0
self
.
send
(
"
#{
validation_error
}
="
,
''
)
# rubocop:disable GitlabSecurity/PublicSend
new_value
=
if
value
.
blank?
nil
else
ChronicDuration
.
parse
(
value
).
to_i
end
self
.
send
(
"
#{
source_attribute
}
="
,
new_value
)
# rubocop:disable GitlabSecurity/PublicSend
self
.
send
(
"
#{
source_attribute
}
="
,
new_value
)
# rubocop:disable GitlabSecurity/PublicSend
rescue
ChronicDuration
::
DurationParseError
=>
ex
self
.
send
(
"
#{
validation_error
}
="
,
ex
.
message
)
# rubocop:disable GitlabSecurity/PublicSend
end
end
new_value
define_method
(
virtual_attribute_validator
)
do
error
=
self
.
send
(
validation_error
)
# rubocop:disable GitlabSecurity/PublicSend
self
.
send
(
'errors'
).
add
(
source_attribute
,
error
)
unless
error
.
blank?
# rubocop:disable GitlabSecurity/PublicSend
end
end
end
end
end
end
...
...
spec/models/concerns/chronic_duration_attribute_spec.rb
View file @
c747d9bc
...
@@ -11,11 +11,13 @@ shared_examples 'ChronicDurationAttribute reader' do
...
@@ -11,11 +11,13 @@ shared_examples 'ChronicDurationAttribute reader' do
expect
(
subject
.
send
(
virtual_field
)).
to
eq
(
'2m'
)
expect
(
subject
.
send
(
virtual_field
)).
to
eq
(
'2m'
)
end
end
it
'outputs empty string when value set to nil'
do
context
'when value is set to nil'
do
it
'outputs empty string'
do
subject
.
send
(
"
#{
source_field
}
="
,
nil
)
subject
.
send
(
"
#{
source_field
}
="
,
nil
)
expect
(
subject
.
send
(
virtual_field
)).
to
be_empty
expect
(
subject
.
send
(
virtual_field
)).
to
be_empty
end
end
end
end
end
shared_examples
'ChronicDurationAttribute writer'
do
shared_examples
'ChronicDurationAttribute writer'
do
...
@@ -29,29 +31,63 @@ shared_examples 'ChronicDurationAttribute writer' do
...
@@ -29,29 +31,63 @@ shared_examples 'ChronicDurationAttribute writer' do
expect
(
subject
.
send
(
source_field
)).
to
eq
(
600
)
expect
(
subject
.
send
(
source_field
)).
to
eq
(
600
)
end
end
it
'
writes nil when empty input is used
'
do
it
'
passes validation
'
do
subject
.
send
(
"
#{
virtual_field
}
="
,
''
)
subject
.
send
(
"
#{
virtual_field
}
="
,
'
10m
'
)
expect
(
subject
.
send
(
source_field
)).
to
be_nil
expect
(
subject
.
valid?
).
to
be_truthy
end
context
'when negative input is used'
do
before
do
subject
.
send
(
"
#{
source_field
}
="
,
3600
)
end
end
it
'writes nil when negative input is used'
do
it
"doesn't raise exception"
do
allow
(
ChronicDuration
).
to
receive
(
:parse
).
and_return
(
-
10
)
expect
{
subject
.
send
(
"
#{
virtual_field
}
="
,
'-10m'
)
}.
not_to
raise_error
(
ChronicDuration
::
DurationParseError
)
end
it
"doesn't change value"
do
expect
{
subject
.
send
(
"
#{
virtual_field
}
="
,
'-10m'
)
}.
not_to
change
{
subject
.
send
(
source_field
)
}
end
it
"doesn't pass validation"
do
subject
.
send
(
"
#{
virtual_field
}
="
,
'-10m'
)
subject
.
send
(
"
#{
virtual_field
}
="
,
'-10m'
)
expect
(
subject
.
valid?
).
to
be_falsey
end
end
context
'when empty input is used'
do
it
'writes nil'
do
subject
.
send
(
"
#{
virtual_field
}
="
,
''
)
expect
(
subject
.
send
(
source_field
)).
to
be_nil
expect
(
subject
.
send
(
source_field
)).
to
be_nil
end
end
it
'writes nil when nil input is used'
do
it
'passes validation'
do
subject
.
send
(
"
#{
virtual_field
}
="
,
''
)
expect
(
subject
.
valid?
).
to
be_truthy
end
end
context
'when nil input is used'
do
it
'writes nil'
do
subject
.
send
(
"
#{
virtual_field
}
="
,
nil
)
subject
.
send
(
"
#{
virtual_field
}
="
,
nil
)
expect
(
subject
.
send
(
source_field
)).
to
be_nil
expect
(
subject
.
send
(
source_field
)).
to
be_nil
end
end
it
"doesn't raise exception when nil input is used"
do
it
'passes validation'
do
subject
.
send
(
"
#{
virtual_field
}
="
,
nil
)
expect
(
subject
.
valid?
).
to
be_truthy
end
it
"doesn't raise exception"
do
expect
{
subject
.
send
(
"
#{
virtual_field
}
="
,
nil
)
}.
not_to
raise_error
(
NoMethodError
)
expect
{
subject
.
send
(
"
#{
virtual_field
}
="
,
nil
)
}.
not_to
raise_error
(
NoMethodError
)
end
end
end
end
end
describe
'ChronicDurationAttribute'
do
describe
'ChronicDurationAttribute'
do
...
...
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