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
c2bc1533
Commit
c2bc1533
authored
Mar 23, 2018
by
Tomasz Maczukin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactorize ChronicDurationAttribute concern
parent
973e4030
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
42 deletions
+33
-42
app/models/concerns/chronic_duration_attribute.rb
app/models/concerns/chronic_duration_attribute.rb
+20
-31
spec/models/concerns/chronic_duration_attribute_spec.rb
spec/models/concerns/chronic_duration_attribute_spec.rb
+13
-11
No files found.
app/models/concerns/chronic_duration_attribute.rb
View file @
c2bc1533
...
...
@@ -2,49 +2,38 @@ module ChronicDurationAttribute
extend
ActiveSupport
::
Concern
class_methods
do
def
chronic_duration_attr
(
virtual_attribute
,
source_attribute
)
chronic_duration_attr_reader
(
virtual_attribute
,
source_attribute
)
chronic_duration_attr_writer
(
virtual_attribute
,
source_attribute
)
end
def
chronic_duration_attr_reader
(
virtual_attribute
,
source_attribute
)
define_method
(
virtual_attribute
)
do
value
=
self
.
send
(
source_attribute
)
# rubocop:disable GitlabSecurity/PublicSend
return
''
if
value
.
nil?
ChronicDuration
.
output
(
value
,
format: :short
)
chronic_duration_attributes
[
virtual_attribute
]
||
output_chronic_duration_attribute
(
source_attribute
)
end
end
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
chronic_duration_attr_reader
(
virtual_attribute
,
source_attribute
)
define_method
(
"
#{
virtual_attribute
}
="
)
do
|
value
|
chronic_duration_attributes
[
virtual_attribute
]
=
value
.
presence
||
''
begin
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
rescue
ChronicDuration
::
DurationParseError
=>
ex
self
.
send
(
"
#{
validation_error
}
="
,
ex
.
message
)
# rubocop:disable GitlabSecurity/PublicSend
new_value
=
ChronicDuration
.
parse
(
value
).
to_i
if
value
.
present?
assign_attributes
(
source_attribute
=>
new_value
)
rescue
ChronicDuration
::
DurationParseError
# ignore error as it will be caught by validation
end
end
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
validates
virtual_attribute
,
allow_nil:
true
,
duration:
true
end
alias_method
:chronic_duration_attr
,
:chronic_duration_attr_writer
end
def
chronic_duration_attributes
@chronic_duration_attributes
||=
{}
end
def
output_chronic_duration_attribute
(
source_attribute
)
value
=
attributes
[
source_attribute
.
to_s
]
ChronicDuration
.
output
(
value
,
format: :short
)
if
value
end
end
spec/models/concerns/chronic_duration_attribute_spec.rb
View file @
c2bc1533
...
...
@@ -12,10 +12,10 @@ shared_examples 'ChronicDurationAttribute reader' do
end
context
'when value is set to nil'
do
it
'outputs
empty string
'
do
it
'outputs
nil
'
do
subject
.
send
(
"
#{
source_field
}
="
,
nil
)
expect
(
subject
.
send
(
virtual_field
)).
to
be_
empty
expect
(
subject
.
send
(
virtual_field
)).
to
be_
nil
end
end
end
...
...
@@ -25,15 +25,15 @@ shared_examples 'ChronicDurationAttribute writer' do
expect
(
subject
.
class
).
to
be_public_method_defined
(
"
#{
virtual_field
}
="
)
end
it
'parses chronic duration input'
do
before
do
subject
.
send
(
"
#{
virtual_field
}
="
,
'10m'
)
end
it
'parses chronic duration input'
do
expect
(
subject
.
send
(
source_field
)).
to
eq
(
600
)
end
it
'passes validation'
do
subject
.
send
(
"
#{
virtual_field
}
="
,
'10m'
)
expect
(
subject
.
valid?
).
to
be_truthy
end
...
...
@@ -54,33 +54,34 @@ shared_examples 'ChronicDurationAttribute writer' do
subject
.
send
(
"
#{
virtual_field
}
="
,
'-10m'
)
expect
(
subject
.
valid?
).
to
be_falsey
expect
(
subject
.
errors
&
.
messages
).
to
include
(
virtual_field
=>
[
'is not a correct duration'
])
end
end
context
'when empty input is used'
do
it
'writes nil'
do
before
do
subject
.
send
(
"
#{
virtual_field
}
="
,
''
)
end
it
'writes nil'
do
expect
(
subject
.
send
(
source_field
)).
to
be_nil
end
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
before
do
subject
.
send
(
"
#{
virtual_field
}
="
,
nil
)
end
it
'writes nil'
do
expect
(
subject
.
send
(
source_field
)).
to
be_nil
end
it
'passes validation'
do
subject
.
send
(
"
#{
virtual_field
}
="
,
nil
)
expect
(
subject
.
valid?
).
to
be_truthy
end
...
...
@@ -103,6 +104,7 @@ end
describe
'ChronicDurationAttribute - reader'
do
let
(
:source_field
)
{
:timeout
}
let
(
:virtual_field
)
{
:timeout_human_readable
}
subject
{
Ci
::
BuildMetadata
.
new
}
it
"doesn't contain dynamically created writer method"
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