Commit 7ef24a47 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Properly handle empty value for chronic duration attribute

parent 14549703
...@@ -10,14 +10,20 @@ module ChronicDurationAttribute ...@@ -10,14 +10,20 @@ module ChronicDurationAttribute
def chronic_duration_attr_reader(virtual_attribute, source_attribute) def chronic_duration_attr_reader(virtual_attribute, source_attribute)
define_method(virtual_attribute) do define_method(virtual_attribute) do
value = self.send(source_attribute) # rubocop:disable GitlabSecurity/PublicSend value = self.send(source_attribute) # rubocop:disable GitlabSecurity/PublicSend
ChronicDuration.output(value, format: :short) unless value.nil?
return '' if value.nil?
ChronicDuration.output(value, format: :short)
end end
end end
def chronic_duration_attr_writer(virtual_attribute, source_attribute) def chronic_duration_attr_writer(virtual_attribute, source_attribute)
define_method("#{virtual_attribute}=") do |value| define_method("#{virtual_attribute}=") do |value|
new_value = ChronicDuration.parse(value).to_i new_value = ChronicDuration.parse(value).to_i
new_value = nil if new_value <= 0
self.send("#{source_attribute}=", new_value) # rubocop:disable GitlabSecurity/PublicSend self.send("#{source_attribute}=", new_value) # rubocop:disable GitlabSecurity/PublicSend
new_value new_value
end end
end end
......
...@@ -5,11 +5,17 @@ shared_examples 'ChronicDurationAttribute reader' do ...@@ -5,11 +5,17 @@ shared_examples 'ChronicDurationAttribute reader' do
expect(subject.class).to be_public_method_defined(virtual_field) expect(subject.class).to be_public_method_defined(virtual_field)
end end
it 'outputs chronic duration formated value' do it 'outputs chronic duration formatted value' do
subject.send("#{source_field}=", 120) subject.send("#{source_field}=", 120)
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
subject.send("#{source_field}=", nil)
expect(subject.send(virtual_field)).to be_empty
end
end end
shared_examples 'ChronicDurationAttribute writer' do shared_examples 'ChronicDurationAttribute writer' do
...@@ -18,10 +24,16 @@ shared_examples 'ChronicDurationAttribute writer' do ...@@ -18,10 +24,16 @@ shared_examples 'ChronicDurationAttribute writer' do
end end
it 'parses chronic duration input' do it 'parses chronic duration input' do
subject.send("#{virtual_field}=", "10m") subject.send("#{virtual_field}=", '10m')
expect(subject.send(source_field)).to eq(600) expect(subject.send(source_field)).to eq(600)
end end
it 'writes null when empty input is used' do
subject.send("#{virtual_field}=", '')
expect(subject.send(source_field)).to be_nil
end
end end
describe 'ChronicDurationAttribute' do describe 'ChronicDurationAttribute' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment