Add validation for chronic_duration_attr_writer

This commit is contained in:
Tomasz Maczukin 2018-03-21 17:22:52 +01:00
parent ed86344f49
commit c747d9bc0b
No known key found for this signature in database
GPG key ID: 7E9EB2E4B0F625CD
2 changed files with 74 additions and 19 deletions

View file

@ -18,13 +18,32 @@ module ChronicDurationAttribute
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
define_method("#{virtual_attribute}=") do |value|
new_value = ChronicDuration.parse(value).to_i unless value.nil?
new_value = nil if !new_value.nil? && new_value <= 0
begin
self.send("#{validation_error}=", '') # rubocop:disable GitlabSecurity/PublicSend
self.send("#{source_attribute}=", new_value) # rubocop:disable GitlabSecurity/PublicSend
new_value =
if value.blank?
nil
else
ChronicDuration.parse(value).to_i
end
new_value
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
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

View file

@ -11,10 +11,12 @@ shared_examples 'ChronicDurationAttribute reader' do
expect(subject.send(virtual_field)).to eq('2m')
end
it 'outputs empty string when value set to nil' do
subject.send("#{source_field}=", nil)
context 'when value is set to nil' do
it 'outputs empty string' do
subject.send("#{source_field}=", nil)
expect(subject.send(virtual_field)).to be_empty
expect(subject.send(virtual_field)).to be_empty
end
end
end
@ -29,28 +31,62 @@ shared_examples 'ChronicDurationAttribute writer' do
expect(subject.send(source_field)).to eq(600)
end
it 'writes nil when empty input is used' do
subject.send("#{virtual_field}=", '')
it 'passes validation' do
subject.send("#{virtual_field}=", '10m')
expect(subject.send(source_field)).to be_nil
expect(subject.valid?).to be_truthy
end
it 'writes nil when negative input is used' do
allow(ChronicDuration).to receive(:parse).and_return(-10)
context 'when negative input is used' do
before do
subject.send("#{source_field}=", 3600)
end
subject.send("#{virtual_field}=", '-10m')
it "doesn't raise exception" do
expect { subject.send("#{virtual_field}=", '-10m') }.not_to raise_error(ChronicDuration::DurationParseError)
end
expect(subject.send(source_field)).to be_nil
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')
expect(subject.valid?).to be_falsey
end
end
it 'writes nil when nil input is used' do
subject.send("#{virtual_field}=", nil)
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
it 'passes validation' do
subject.send("#{virtual_field}=", '')
expect(subject.valid?).to be_truthy
end
end
it "doesn't raise exception when nil input is used" do
expect { subject.send("#{virtual_field}=", nil) }.not_to raise_error(NoMethodError)
context 'when nil input is used' do
it 'writes nil' do
subject.send("#{virtual_field}=", nil)
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
it "doesn't raise exception" do
expect { subject.send("#{virtual_field}=", nil) }.not_to raise_error(NoMethodError)
end
end
end